Windows Develop Bookmark and Share   
 index > Windows Forms General > C# Forms to and fro using property
 

C# Forms to and fro using property

Come on experts!

I got 2 Forms (1st Form with textBox1 and button1 and 2nd with textBox1 and button1).

The Forms to and fro communicationgoes like this:
On Form1, enter text intextBox1 and click button1 to edit the text bypassing itto Form2 textBox1 on ShowDialog().
Now edit the text in Form2 textBox1 and close by clicking Form2 button1 and the edited text is passed backto Form1 textBox1.

Here's the code:

Code Snippet:

// Form1:


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

internal string TextBoxText
{
get
{
return textBox1.Text;
}
set
{
textBox1.Text = value;
}
}

private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();

F2.ShowDialog();
}
}


// Form2:

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

Form1 F1 = (Form1)Application.OpenForms["Form1"];

private void button1_Click(object sender, EventArgs e)
{
F1.TextBoxText = textBox1.Text;
this.Close();
}

private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = F1.TextBoxText;
}
}




I know that the best way to pass valuesto andfro forms is by using EventAgrs. Vide my earlier threads:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/53c9a7b0-e7ff-4f7a-ab66-40e118284692

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/5ab1f32c-f25b-4d42-bf65-860a253c9371

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/1f7bd677-954e-430c-8040-75bdb57cf19e



However, I want to know what is fundamentally wrong in the above codesnippet though itis very very simple.

Thanks.

  • Edited byrecherche Sunday, September 13, 2009 12:32 PMtypo
  • Edited byrecherche Sunday, September 13, 2009 12:26 PMtypo
  • Edited byrecherche Sunday, September 13, 2009 12:27 PMtypo
  • Edited byrecherche Sunday, September 13, 2009 12:45 PMtyp
  •  
recherche  Sunday, September 13, 2009 12:22 PM
The code works fine, is there a problem? There is a nasty bug in the implementation of Application.OpenForms, you should avoid relying on it. This code shows the bug at work:

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

private void button1_Click(object sender, EventArgs e) {
this.ShowInTaskbar = !this.ShowInTaskbar;
MessageBox.Show("Number of open forms: " + Application.OpenForms.Count);
}
}

You already know how to do it properly.

Hans Passant.
  • Marked As Answer byrecherche Sunday, September 13, 2009 2:01 PM
  •  
nobugz  Sunday, September 13, 2009 1:25 PM
You were not passing the value of Form1.Textbox.Text to the Form2 instance object.

/****************************************/

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

internal string Form1TextBoxText

{

get

{

return textBox1.Text;

}

}

internal string Form2TextBoxText

{

set

{

textBox1.Text = value;

}

}

private void button1_Click(object sender, EventArgs e)

{

Form2 F2 = new Form2(this.textBox1.Text); // change made here

F2.ShowDialog();

}

}

/*****************************************************/

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

// added this new constructor.

public Form2(string text)

: this()

{

this.textBox1.Text = text;

}

Form1 F1 = (Form1)Application.OpenForms["Form1"];

private string Form1TextBoxText

{

set

{

textBox1.Text = value;

}

}

private void button1_Click(object sender, EventArgs e)

{

F1.Form2TextBoxText = textBox1.Text;

this.Close();

}

private void Form2_Load(object sender, EventArgs e)

{

textBox1.Text = F1.Form1TextBoxText;

}

}

/**********************************************/


Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Sunday, September 13, 2009 12:55 PM
The code works fine, is there a problem? There is a nasty bug in the implementation of Application.OpenForms, you should avoid relying on it. This code shows the bug at work:

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

private void button1_Click(object sender, EventArgs e) {
this.ShowInTaskbar = !this.ShowInTaskbar;
MessageBox.Show("Number of open forms: " + Application.OpenForms.Count);
}
}

You already know how to do it properly.

Hans Passant.
  • Marked As Answer byrecherche Sunday, September 13, 2009 2:01 PM
  •  
nobugz  Sunday, September 13, 2009 1:25 PM
Thanks man.
recherche  Sunday, September 13, 2009 2:01 PM
I was not seeing the textbox on Form2 get initialized.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Sunday, September 13, 2009 2:16 PM

You can use google to search for other answers

Custom Search

More Threads

• In a numericUpDown, can I separate decimals with a point instead of a comma
• Little Drop Down menu
• Flickering effect
• How can simulate button clicking in multiple threads environmentï¼
• When XP Theme is in effect, what color should the tabpage be?
• WinForms and mini-database
• Divide et Impera (GraphicsPath)
• Datagridviewcombobox question
• new to windows forms
• axWebBrowser, NewWindow3 and post data