Click here to Skip to main content
15,903,033 members
Home / Discussions / C#
   

C#

 
AnswerRe: Customizing a Windows Form Pin
pelnor25-Aug-09 3:44
pelnor25-Aug-09 3:44 
Questiondata gridview error Pin
myinstincts25-Aug-09 1:26
myinstincts25-Aug-09 1:26 
AnswerRe: data gridview error Pin
padmanabhan N25-Aug-09 1:53
padmanabhan N25-Aug-09 1:53 
QuestionBinding 2 data tables to a single data grid Pin
myinstincts25-Aug-09 0:43
myinstincts25-Aug-09 0:43 
AnswerRe: Binding 2 data tables to a single data grid Pin
padmanabhan N25-Aug-09 0:51
padmanabhan N25-Aug-09 0:51 
Questionhow to pass the values between the datagridview's in different pages Pin
Anjani Poornima25-Aug-09 0:20
Anjani Poornima25-Aug-09 0:20 
AnswerRe: how to pass the values between the datagridview's in different pages Pin
stancrm25-Aug-09 1:12
stancrm25-Aug-09 1:12 
GeneralRe: how to pass the values between the datagridview's in different pages Pin
DaveyM6925-Aug-09 2:53
professionalDaveyM6925-Aug-09 2:53 
This is NOT a good way to do it as now Form2 is coupled to Form1. Also, what if there are hundreds of objects that need the data? Are you going to have hundreds of constructor overloads?

The correct way to do this is to raise an event in Form2 that Form1 (and any other interested parties) subscribes to. If any data needs to be passed, then a custom class derived from event args should be used with that data being passed as a property. This is the way all the classes provided by Microsoft do it, with good reason.

A quick example:
using System;
using System.Windows.Forms;

public class Form1 : Form
{
    public Form1() { }

    public void ShowNewForm2()
    {
        Form2 form2 = new Form2();
        form2.CustomEvent += new EventHandler<CustomEventArgs>(form2_CustomEvent);
        form2.Show();
    }

    void form2_CustomEvent(object sender, CustomEventArgs e)
    {
        // data is in: e.YourData
    }
}

public class Form2 : Form
{
    public event EventHandler<CustomEventArgs> CustomEvent;

    public Form2() { }

    protected virtual void OnCustomEvent(CustomEventArgs e)
    {
        EventHandler<CustomEventArgs> eh = CustomEvent;
        if (eh != null)
            eh(this, e);
    }

    public void PerformCustomEvent()
    {
        OnCustomEvent(new CustomEventArgs(123));
    }
}

public class CustomEventArgs : EventArgs
{
    public CustomEventArgs(int yourData)
    {
        YourData = yourData;
    }

    public int YourData
    {
        get;
        private set;
    }
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)

GeneralRe: how to pass the values between the datagridview's in different pages Pin
Anjani Poornima25-Aug-09 3:05
Anjani Poornima25-Aug-09 3:05 
AnswerRe: how to pass the values between the datagridview's in different pages Pin
DaveyM6925-Aug-09 2:54
professionalDaveyM6925-Aug-09 2:54 
GeneralRe: how to pass the values between the datagridview's in different pages Pin
Anjani Poornima25-Aug-09 3:05
Anjani Poornima25-Aug-09 3:05 
Questionassign datatable to crystal report Pin
wasifmuneer25-Aug-09 0:18
wasifmuneer25-Aug-09 0:18 
AnswerRe: assign datatable to crystal report Pin
Adam R Harris25-Aug-09 4:59
Adam R Harris25-Aug-09 4:59 
Questionerror background image to a windows form Pin
praveenkumar_vittaboina24-Aug-09 23:35
praveenkumar_vittaboina24-Aug-09 23:35 
QuestionDynamic menu - display the appropritate page based upon menu item selected Pin
Punit Belani24-Aug-09 23:03
Punit Belani24-Aug-09 23:03 
Questionhow can i open an existing excel file as a new document. Pin
amit_8324-Aug-09 22:52
professionalamit_8324-Aug-09 22:52 
Questionnotifyicon help Pin
Erdinc2724-Aug-09 22:37
Erdinc2724-Aug-09 22:37 
AnswerRe: notifyicon help Pin
DaveyM6924-Aug-09 23:08
professionalDaveyM6924-Aug-09 23:08 
GeneralRe: notifyicon help Pin
Erdinc2724-Aug-09 23:37
Erdinc2724-Aug-09 23:37 
GeneralRe: notifyicon help Pin
Erdinc2729-Aug-09 0:56
Erdinc2729-Aug-09 0:56 
Questionam tryin to fill the dropdwn list from the database...but it is not displayin any values..plz help me Pin
ankitjain111024-Aug-09 22:37
ankitjain111024-Aug-09 22:37 
AnswerRe: am tryin to fill the dropdwn list from the database...but it is not displayin any values..plz help me Pin
Nagy Vilmos24-Aug-09 23:09
professionalNagy Vilmos24-Aug-09 23:09 
AnswerRe: am tryin to fill the dropdwn list from the database...but it is not displayin any values..plz help me Pin
Blue_Boy24-Aug-09 23:10
Blue_Boy24-Aug-09 23:10 
AnswerRe: am tryin to fill the dropdwn list from the database...but it is not displayin any values..plz help me Pin
ShineHu24-Aug-09 23:14
ShineHu24-Aug-09 23:14 
QuestionLaunch Browser and pass parameters. Pin
Member 232448324-Aug-09 22:21
Member 232448324-Aug-09 22:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.