Click here to Skip to main content
15,900,434 members
Home / Discussions / C#
   

C#

 
GeneralRe: change text from form Pin
OriginalGriff25-Nov-13 6:00
mveOriginalGriff25-Nov-13 6:00 
GeneralRe: change text from form Pin
messages25-Nov-13 6:32
messages25-Nov-13 6:32 
GeneralRe: change text from form Pin
OriginalGriff25-Nov-13 7:59
mveOriginalGriff25-Nov-13 7:59 
GeneralRe: change text from form Pin
messages26-Nov-13 3:59
messages26-Nov-13 3:59 
GeneralRe: change text from form Pin
OriginalGriff26-Nov-13 5:01
mveOriginalGriff26-Nov-13 5:01 
GeneralRe: change text from form Pin
messages26-Nov-13 5:22
messages26-Nov-13 5:22 
GeneralRe: change text from form Pin
OriginalGriff26-Nov-13 5:56
mveOriginalGriff26-Nov-13 5:56 
AnswerRe: change text from form Pin
BillWoodruff25-Nov-13 5:08
professionalBillWoodruff25-Nov-13 5:08 
Unless there is a compelling reason to create new instances of the Forms with every click of the Button(s), then you want to create the two Forms once.

There are many ways you could have the two Forms interact; the method shown here works by giving each Form a valid reference to the other Form.

In the case of Form1, the "Main Form," which creates Form2: by definition, it "knows about" ... has a valid reference to ... Form2.

Look at the Load EventHandler in Form1: notice that it sets the Public Property of Type Form1 in Form2 with a valid reference to Form1.

Form1:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Form2 form2 = new Form2();

    private void Form1_Load(object sender, EventArgs e)
    {
        // form2 gets a reference to this instance of Form1
        form2.referenceToForm1 = this;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        form2.Text = textBox1.Text;
        form2.Show();
        Hide();
    }
}
Form 2:
C#
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    // holds the reference to Form1
    public Form1 referenceToForm1 { get; set; }

    private void button1_Click(object sender, EventArgs e)
    {
        referenceToForm1.Text = textBox1.Text;
        referenceToForm1.Show();
        Hide();
    }
}
Note that you could get into an "undefined" state if you did this:

1. clicked the Button on form1 which would show form2, and hide form1

2. then, closed form2: at that point your Application is still running, and form1 is hidden ... so it won't show in the TaskBar, so: you are now running in "outer space," and HAL will not open the pod-bay doors.

Finally, I am not sure why you are implementing this kind of functionality which I find rather strange: perhaps as a learning experiment ?
"What Turing gave us for the first time (and without Turing you just couldn't do any of this) is he gave us a way of thinking about and taking seriously and thinking in a disciplined way about phenomena that have, as I like to say, trillions of moving parts.

Until the late 20th century, nobody knew how to take seriously a machine with a trillion moving parts. It's just mind-boggling." Daniel C. Dennett

GeneralRe: change text from form Pin
messages25-Nov-13 5:26
messages25-Nov-13 5:26 
GeneralRe: change text from form Pin
BillWoodruff25-Nov-13 12:39
professionalBillWoodruff25-Nov-13 12:39 
AnswerRe: change text from form Pin
Vinh Nguyen26-Nov-13 0:01
Vinh Nguyen26-Nov-13 0:01 
QuestionHow can I access the properties of this object? Pin
turbosupramk325-Nov-13 3:20
turbosupramk325-Nov-13 3:20 
AnswerRe: How can I access the properties of this object? Pin
Vinh Nguyen25-Nov-13 3:52
Vinh Nguyen25-Nov-13 3:52 
AnswerRe: How can I access the properties of this object? Pin
OriginalGriff25-Nov-13 5:10
mveOriginalGriff25-Nov-13 5:10 
AnswerRe: How can I access the properties of this object? Pin
turbosupramk325-Nov-13 5:54
turbosupramk325-Nov-13 5:54 
AnswerRe: How can I access the properties of this object? Pin
Abhinav S25-Nov-13 6:35
Abhinav S25-Nov-13 6:35 
GeneralRe: How can I access the properties of this object? Pin
turbosupramk325-Nov-13 7:13
turbosupramk325-Nov-13 7:13 
AnswerRe: How can I access the properties of this object? Pin
Richard Deeming25-Nov-13 8:56
mveRichard Deeming25-Nov-13 8:56 
GeneralRe: How can I access the properties of this object? Pin
turbosupramk325-Nov-13 9:02
turbosupramk325-Nov-13 9:02 
GeneralRe: How can I access the properties of this object? Pin
Richard Deeming25-Nov-13 9:07
mveRichard Deeming25-Nov-13 9:07 
QuestionMemory Usage from Task Manager Pin
devvvy24-Nov-13 13:06
devvvy24-Nov-13 13:06 
AnswerRe: Memory Usage from Task Manager Pin
Dave Kreskowiak24-Nov-13 18:05
mveDave Kreskowiak24-Nov-13 18:05 
GeneralRe: Memory Usage from Task Manager Pin
devvvy25-Nov-13 0:49
devvvy25-Nov-13 0:49 
GeneralRe: Memory Usage from Task Manager Pin
Richard MacCutchan25-Nov-13 2:02
mveRichard MacCutchan25-Nov-13 2:02 
GeneralRe: Memory Usage from Task Manager Pin
devvvy29-Nov-13 19:30
devvvy29-Nov-13 19:30 

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.