Hi I couldn't find anything that referenced exactly what I was looking for. I have seen many examples on sites of passing data between two forms such as
How can I pass values from one form to another? Using The Controls Of One Form Into Another How to access controls on an inherited form?
On top of several blogs and hours of reading and not getting it.
So basically here is the act I am trying to accomplish Using DockPanel Suite
Main Form parentForm
Has a form Form1
docked inside that is always there. Cannot be removed or reloaded is constant(so should always be same instance I would assume) that has a textboxfrm1Box
.
parentForm
has a menu bar with a button button1
which will open a third form Form2
that also has a textbox frm2Box
. I would like to be able to click button1
(From the parentForm
) have it open Form2
and pass whatever is in frm1Box
to frm2Box
.
So from reading it seemed like setting up properties was what everyone recommended so that is what I did and it works great if I just do it from a button click on form1 to open form2 and pass the data but I cant seem to figure it out when adding another form. This is obviously not my full code I will be happy to post my actual code if it is needed just didnt to save space.
//Parent Form
public partial class parentForm : Form
public static Form1 form1 = new Form1();
public void showForm1()
{
form1 = new Form1();
form1.Show(dockPanel1, DockState.DockLeft);
}
private void parentForm_Load(object sender, EventArgs e)
{
showForm1();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.CustNameCb = form1.CustName;
form2.Show();
}
//Form1
public partial class Form1 : DockContent
{
private string _custName;
public Form2()
{
InitializeComponent();
}
public string CustName
{
get
{
return _custName;
}
set
{
_custName = value;
frm1Box.Text = _custName;
}
}
//Form2
public partial class Form2 : Form
{
private string _custNameCb;
public Form2()
{
InitializeComponent();
}
public string CustNameCb
{
get
{
return _custNameCb;
}
set
{
_custNameCb = value;
frm2Box.Text = _custNameCb;
}
}
I think I am missing this concept altogether but I couldn't find a "good" example of this in my searches or at least one I could follow. I am new to programming and C# in particular so please forgive my inexperience. If anyone has a better way of doing this please let me know. I have considered using SQL to store the data. I would prefer not to use a file based storage if possible. Also it is worth noting once I find the proper method of doing this it will need to be scale-able because when I actually implement this it will be 5 different buttons on parentForm opening 5 different form's but all still pulling data from that same Form1 yet form1 will have around 10 textboxes of data to pull.
Thanks in advance for any replies.
I have not worked with this control in C#, just VB, but I will do my best to describe how this would be done in C#.
To access controls on another form, the calling form must have an instance of the form for which it is trying to access. This can be accomplished when initializing the form object utilizing dependency injection.
Form 2 would require instance variable of type Form1.
This variable would be set via the class's constructor.
Form1's public content is now accessible to Form2. For instance, say Form1 had a textbox named txtName. You could set the text attribute with the following code:
You're so close. In your Properties, the "setter" is displaying the passed value in the TextBox, but how is the "getter" retrieving the value from the TextBox? It is only returning the value stored in the private variable. For Form1, are you setting the private variable "_custName" somehow (now shown) thru code (like thru the TextChanged() event maybe)?
If not, change the Property to:
This will return the value that is actually in the TextBox and hopefully get it into your second form...