Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Events form
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:
 
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:
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 6-Aug-12 2:34am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

When i saw the small codes in the link from Unareshraju i found my solution here: Passing Data Between Forms[^]
 
Thanks!
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
pieterjann at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
 
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();
         }
    }
 
----------------------------------------------------
  Permalink  

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

  Print Answers RSS
0 DamithSL 425
1 OriginalGriff 184
2 BillWoodruff 170
3 Zoltán Zörgő 165
4 Maciej Los 163
0 OriginalGriff 7,854
1 DamithSL 5,949
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,084
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 6 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100