Click here to Skip to main content
15,911,531 members
Home / Discussions / C#
   

C#

 
AnswerRe: Can I compile only some line of code? Pin
CoderForEver13-Dec-09 16:14
CoderForEver13-Dec-09 16:14 
Questionwindows mobile 5.0, Pin
nuttynibbles12-Dec-09 23:32
nuttynibbles12-Dec-09 23:32 
AnswerRe: windows mobile 5.0, Pin
MumbleB13-Dec-09 1:53
MumbleB13-Dec-09 1:53 
Questionpassing data from child form to parent Pin
tasumisra12-Dec-09 18:10
tasumisra12-Dec-09 18:10 
AnswerRe: passing data from child form to parent Pin
dan!sh 12-Dec-09 18:56
professional dan!sh 12-Dec-09 18:56 
GeneralRe: passing data from child form to parent Pin
tasumisra12-Dec-09 19:38
tasumisra12-Dec-09 19:38 
GeneralRe: passing data from child form to parent Pin
dan!sh 12-Dec-09 21:15
professional dan!sh 12-Dec-09 21:15 
GeneralRe: passing data from child form to parent Pin
OriginalGriff12-Dec-09 22:20
mveOriginalGriff12-Dec-09 22:20 
There are two problems here:
1) Declaring the property as static means it won't compile - static properties have no "this" object.
2) While this method will work with a little tweak, it is not good practice (and it's best to use good practice as early in the learning process as possible, or it gets harder to break the bad habits).

A better way to do it it to signal a "data changed" event from the button form to the label form:

Form 1:
private void Form1_Load(object sender, EventArgs e)
    {
    Form2 f2 = new Form2();
    f2.Changed += new EventHandler(Changed);
    f2.Show();
    }

private void Changed(object sender, EventArgs e)
    {
    ChangedArgs ca = e as ChangedArgs;
    if (ca != null)
        {
        label1.Text = ca.strData;
        }
    else
        {
        label1.Text = "Fired!";
        }
    }


Form2:
public event EventHandler Changed;

private void button1_Click(object sender, EventArgs e)
    {
    EventHandler eh = Changed;
    if (eh != null)
        {
        eh(this, new ChangedArgs("Hello!"));
        }
    }


Additional:
public partial class ChangedArgs : EventArgs
    {
    private string _strData;

    public string strData
        {
        get { return _strData; }
        set { _strData = value; }
        }

    public ChangedArgs(string str)
        {
        _strData = str;
        }
    }


The ChangedArgs class allows you to transfer all the relevant data Form2 needs to pass to Form1.

All those who believe in psycho kinesis, raise my hand.

GeneralRe: passing data from child form to parent Pin
dan!sh 13-Dec-09 0:05
professional dan!sh 13-Dec-09 0:05 
GeneralRe: passing data from child form to parent Pin
tasumisra13-Dec-09 2:47
tasumisra13-Dec-09 2:47 
GeneralRe: passing data from child form to parent Pin
OriginalGriff13-Dec-09 2:52
mveOriginalGriff13-Dec-09 2:52 
GeneralRe: passing data from child form to parent Pin
DaveyM6913-Dec-09 4:10
professionalDaveyM6913-Dec-09 4:10 
GeneralRe: passing data from child form to parent Pin
dan!sh 13-Dec-09 5:38
professional dan!sh 13-Dec-09 5:38 
GeneralRe: passing data from child form to parent Pin
tasumisra13-Dec-09 2:45
tasumisra13-Dec-09 2:45 
QuestionHttpWebRequest.BegineGetResponse() not running asynchronously… Pin
royk12312-Dec-09 17:53
royk12312-Dec-09 17:53 
AnswerRe: HttpWebRequest.BegineGetResponse() not running asynchronously… Pin
Luc Pattyn13-Dec-09 2:04
sitebuilderLuc Pattyn13-Dec-09 2:04 
GeneralRe: HttpWebRequest.BegineGetResponse() not running asynchronously… Pin
royk12314-Dec-09 0:37
royk12314-Dec-09 0:37 
Questionconnectionstring Pin
farokhian12-Dec-09 10:03
farokhian12-Dec-09 10:03 
QuestionException Handling Pin
Razanust12-Dec-09 7:53
Razanust12-Dec-09 7:53 
AnswerRe: Exception Handling Pin
PIEBALDconsult12-Dec-09 8:52
mvePIEBALDconsult12-Dec-09 8:52 
QuestionSchedulle a task for some days of the week Pin
baranils12-Dec-09 6:14
baranils12-Dec-09 6:14 
AnswerRe: Schedulle a task for some days of the week Pin
Abhinav S12-Dec-09 6:20
Abhinav S12-Dec-09 6:20 
AnswerRe: Schedulle a task for some days of the week Pin
Luc Pattyn12-Dec-09 6:22
sitebuilderLuc Pattyn12-Dec-09 6:22 
GeneralRe: Schedulle a task for some days of the week Pin
baranils12-Dec-09 6:35
baranils12-Dec-09 6:35 
GeneralRe: Schedulle a task for some days of the week Pin
baranils12-Dec-09 6:56
baranils12-Dec-09 6:56 

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.