Passing Data between single instance Windows Forms
Hi! I want to pass data between two textboxes in two different forms when an OK button is clicked.
Now I have found out that there are many ways to do it (Constructor, Object, Delegates Approach etc) all of them work, no problem.
The issue here is that whenever the data is passed a new instance of the form is created.
i.e If I click the OK button on Form1 ten times, ten Forms2 will be created with the passed data.
How do I overcome this?
Thanks!
PS .NET4 VS-2010
Last edited by Scar88; November 28th, 2011 at 08:04 AM.
Re: Passing Data between single instance Windows Forms
My Form2 (vk) is modeless. Problem is when ever data is passed from keyboard to the main form (Virtual keyboard, which should always be displayed) it's passed to an instance of it.
this is my code for the OK button on vk Form
Code:
private void button_OK_Click(object sender, EventArgs e)
{
MainForm frm = new MainForm();
delPassData del = new delPassData(frm.funData);
del(this.display);
frm.Show();
this.Hide();
}
And the code on the Virtual keyboard form for passing the data
Re: Passing Data between single instance Windows Forms
Disable the button after it is clicked and handle the VisibilityChanged event of your child form. When it changes to not visible enable the button once again.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Re: Passing Data between single instance Windows Forms
Thank you for you suggestion but maybe due to my inexperience I fail to see how that addresses my problem.
The child form (vk) is not the issue (it's hidden after OK is clicked ). The issue is the new Instance of the main form that is triggered after data is passed.
How do I make the main form stay single?
Re: Passing Data between single instance Windows Forms
You are going about this the wrong way. You "VK" form should not be showing the main form. All it needs to do is close down. What you need to do is have you "VK" form raise an event to update the data on your main form. Here is some code that might help you.
This example uses 2 forms. On the first form, put a label and a button.
On the second form, put a textbox, and 2 buttons. (button2 will close the form).
Notice in this form, we declare the keyboard form, subscribe to the Updatedata event, then show the form as a dialog form.
form2 Code
Code:
public partial class Form2 : Form
{
public delegate void updateDataHandler(string data);
public event updateDataHandler UpdateData;
public Form2()
{
InitializeComponent();
}
protected virtual void OnUpdateData(string data)
{
if (UpdateData != null)
UpdateData(data);
}
private void button1_Click(object sender, EventArgs e)
{
OnUpdateData(this.textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
In this form, anytime we click button1, we raise the event UpdateData, which form1 is subscribed to. This in turn updates the label on form1 via the event handler.
Re: Passing Data between single instance Windows Forms
1. KeyBoard = null. Although technically not necessary, because the keyboard form object is only in scope on that 1 button click, it is a habit of cleaning up after myself. If you null any object you can, you won't get memory leaks.
2. The OnUpdateFunction, is designed to make sure that the UpdateData delegate can be called. If you just call the updateFunction and no one has subscribed to the event, you will get an error. Make the following changes and see what happens when you run the code.
in from 1, take out this line.
Code:
keyboard.UpdateData += new Form2.updateDataHandler(keyboard_UpdateData);
In form2, change this line...
Code:
OnUpdateData(this.textBox1.Text);
to
Code:
UpdateData(this.textBox1.Text);
When you run this, show form2, click the button1 on form2, now what happens is you should get an error. Since nothing is "subscribing" to the event, there is no pointer for the delegate to call, so you will get a null reference error. That is why the "OnUpdateData" function. Notice it just checks if "UpdateData" is null and if not, calls the UpdateData delegate. It is not only good practice, but it is basically the only way you should program events in c#.
Think of it this way, when you have a button on the form, most of the time, you will be using the buttonClick event. However, there are tons of other events that can be associated with the button (mousedown,mouseup....). Everytime one of those things happens on that button, the button needs to check and see if it should actually call the function associated with that event happening. The way they do that is to see if anything has "subscribed" to the event, by checking if it is null. If it is not null, then it calls the function specified by the subscriber.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.