Click here to Skip to main content
16,015,218 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I'm working on a program where I open a window with some text, a barcode and some images in it which I want to print. This is my problem: I want to enter a string in the main form, and convert it and display it in the second form. All these actions should be executed on the event Form_Load, or in my case: Printform_Load But I can not find why my code is not working.

Code in the code printing window:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Printform : Form
    {
        public Printform()
        {
            InitializeComponent();
        }
        private void BtnProgram_Click(object sender, EventArgs e)
        {
            SSP_MainFrom MF = new SSP_MainFrom();
            MF.IdentityUpdated += new 
// Its going wrong here: this is underlined red:
SSP_MainFrom.IdentityUpdateHandler(Printform_Load);
            MF.Show();
        }

        private void Printform_Load(object sender,  e)
        {
            LblSerialNumberCoded = e.TestData;
        }
    }
}


code in the main window:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using BarCode;

namespace WindowsFormsApplication1
{
    public partial class SSP_MainFrom : Form
    {
        public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);
        public event IdentityUpdateHandler IdentityUpdated;

        public SSP_MainFrom()
        {
            InitializeComponent();
        }



        private void BtnProgram_Click(object sender, EventArgs e)
        {
            string sTestData = "testdata";
            IdentityUpdateEventArgs args = new IdentityUpdateEventArgs(sTestData);
            IdentityUpdated(this, args);

            new Printform().Show();
        }

        public class IdentityUpdateEventArgs : System.EventArgs
        {
            private string mTestData;
            public IdentityUpdateEventArgs(string sTestData)
            {
                this.mTestData = sTestData;
            }

            public string TestData
            {
                get
                {
                    return mTestData;
                }
            }

        }
    }
Posted

First of all, the event Form.Load is a "fake" one: it is called after the form is constructed before all other events. You cannot solve any problems by handling it. With the same success, you can call some function in your constructor, at the very end. Probably, this even is added to support the Designer, to introduce an entry point for some custom initialization right after construction in the same style as all other user methods added through the Designer, the event handlers. I personally do not add any event handlers in Designer and do not advise doing so.

Now, your problem is not related to this. This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

See all other solutions to this problem in the discussion referenced above, of course.

—SA
 
Share this answer
 
hi pieterjann,
working for me,
do like this,,,,,,,,,,

1)in form1
place a textbox with Id as textbox1
place a button with Id as passingButton

now add another form with name as form2

coding in form1

C#
private void <pre lang="xml">passingButton_Click(object sender, RoutedEventArgs e)
       {

           Form2 sForm = new Form2(textBox1.Text);
           sForm.Show();
       }</pre>
----------------------------------------------------
in form2 add a textbox with id as richTextBox1

 public partial class Form2 : Form
    {
        public Form2(string form1_textbox) //here form1_textbox as any name
        {
            InitializeComponent();
            richTextBox1.Text = form1_textbox.ToString();
         }
    }

----------------------------------------------------
 
Share this answer
 
When i saw the small codes in the link from Unareshraju i found my solution here: Passing Data Between Forms[^]

Thanks!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Aug-12 12:39pm    
I up-voted this answer with 4. This article is not bad and reviews useful methods, but is not quite comprehensive. In particular, it misses important method I suggest, which is more robust (but, for simpler applications, simple property approach could be easier). Please see my answer.
--SA
Hi pieterjann,
As i understood you have two form
let us form1,form2
when your application executed first it will load form1 with out any clicking (we know this ),suppose you have a textbox in form tou entered somthing or else you have loaded data ,fine.
now these data you passed into form2 by the button click event right!
then while loading the form two those data will displayed.
for that weneed to have amediator between form1 and form2 two pass that data.for that we need to use passing parameters objects, for that we have sample code here
please see here: http://codeincsharp.blogspot.in/2008/04/how-to-pass-values-to-another-form.html[^]
 
Share this answer
 
Comments
pieterjann 6-Aug-12 8:15am    
indeed, i want to pass data from form 1 to form 2. From 2 has no buttons, the only event it has is form_load. I tried both options in the link you posted but both wont work... for the second option it wont recognize the textBox1 in the line:

Form2 sForm = new Form2(textBox1.Text);

any tips on fixing this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900