Cross form communication - c#

I have a program with two forms.
The second form, Form2 in which I want a few labels initialized with values from the main form.
The code:
public Form2()
{
InitializeComponent();
Form1 mainForm = (Form1)this.Owner;
lblName.Text = mainForm.gvRow.Cells[2].Value.ToString();
lblItemType.Text = mainForm.gvRow.Cells[1].Value.ToString();
lblLocation.Text = mainForm.gvRow.Cells[3].Value.ToString();
}
For some reason this does not work in the Form2() section, this.Owner is null. But if I was to place the code in an event method it works just fine.
How can I fix that?

The second form shouldn't need to even know about your main form in the first place. Even if it did, it's an extremely bad idea to be reading into its internal controls.
Instead your second form should have public properties through which it can accept the data that your main form wants to provide to it, without exposing any of its internal controls, and the main form can set those properties using the data from its controls. You could also potentially use parameters to the constructor instead, if you have just a bit of data, and that is the only time you need to provide it.
public class Form2
{
public string Name
{
get { return lblName.Text; }
set { lblName.Text = value; }
}
}
public class MainForm
{
public void Foo()
{
Form2 child = new Form2();
child.Name = mainForm.gvRow.Cells[2].Value.ToString();
child.Show();
}
}

This code is executed when the Form2 form is created. The Owner isn't set yet (and, presumably, the data isn't present yet). If you put it in the VisibleChanged event handler - it will be executed when the Owner and data are (presumably) present.

Use the Load Event. The Owner is only initialized after you Show the form, which then in return raises the Load Event.

Owner isn't set until the form is shown - i.e. in ShowDialog, not during the constructor. You should pass the parent as a parameter in the constructor:
public Form2(Form1 mainForm)
{
InitializeComponent();
lblName.Text = mainForm.gvRow.Cells[2].Value.ToString();
lblItemType.Text = mainForm.gvRow.Cells[1].Value.ToString();
lblLocation.Text = mainForm.gvRow.Cells[3].Value.ToString();
}

That's because the Owner is not initialized yet in the Form2 constructor, set your code in your Form2_Load event

Use the Form.Show(IWin32Window) overload to pass the owner to the child form.
http://msdn.microsoft.com/en-us/library/szcefbbd(v=vs.110).aspx

You need to set the Owner property yourself
As an alternative you could pass a reference to Form1 to the Form2 constructor. In the code that opens Form2 you probably have something like this:
var form2 = new Form2();
form2.Show();
You could replace that with:
var form2 = new Form2(this);
form2.Show();
In Form2 you'd add a constructor overload:
public Form2(Form1 owningForm)
{
InitializeComponent();
Form1 mainForm = owningForm;
lblName.Text = mainForm.gvRow.Cells[2].Value.ToString();
lblItemType.Text = mainForm.gvRow.Cells[1].Value.ToString();
lblLocation.Text = mainForm.gvRow.Cells[3].Value.ToString();
}
If different "owning forms" are possible you may need to define an interface instead of passing Form2.

Related

Pass value from parent form to child form?

I'm trying to pass a value set in my parent form to a second form. I created a property with a get part in the parent form.
I don't want to do something like:
Form2 secondForm = new Form2(value);
It is an already exiting form and I don't want to keep creating a new form every time I want to pass a value.
See this example.
1-Create a window form application,Declare a public string global variable in Form1 , using this variable we can pass value from Form1 to Form2.
2-Now in form2 ,Create a object for Form1 and Get the value using this object.
See image
You have some possibilities here:
Give a Reference from your first Form as value
Form2 secondForm = new Form2(yourForm1);
So you can access via the getter in your first Form. yourForm1.MyValue;
This seems to be a bit ugly. Better is you create a Interface, which hold your Property and is implemented from you first Form.
public interface IValueHolder
{
public int MyValue {get;}
}
public class FirstForm : Form, IValueHolder
{
public int MyValue{get;}
//Do your form stuff
Form2 form = new Form2(this);
}
So your Form2 just take the Interface and stays independent from Form1. Further you can create a Property on Form2 which you access from Form1. For example if your Property in Form1 changes you set the value from Form2 as well.
public class Form2 : Form
{
public int MyValue{get;set;}
}
public class Form1 : Form
{
private int _myValue;
public int MyValue
{
set
{
if (_myValue != value)
{
form2.MyValue = value;
}
}
}
}
At least you can use a Event maybe. Further you can create a Property on Form2 which holds an Form1 Reference or a IValueHolder as described above.
Hope this helps.
I'm not sure about how you are going to use the Form2 in the parent(let it be frmParent). anyway you can follow any of the steps below:
Define the property in the child form as static so that you can access that by using Form2.frmProperty.
Or define the property as public and then access through the class instance, so that you can access the variable through that instance as long as the instance existed. something like the following:
Form2 secondFormInstance = new Form2();
secondFormInstance.frmProperty = 10;
// at some later points
int childValue = secondFormInstance.frmProperty; // take value from that variable
secondFormInstance.frmProperty++; // update the value
Or you can use like what you specified in the question.

Timer not getting control textbox data

So im trying to execute a method from a Timer the problem is that everytime i call the method and pass a string as argument, automatically the string is set to NULL and that's weird, is there a fix or something, here is some code if you wish to look at it
private void timer1_Tick(object sender, EventArgs e)
{
int currentHour = FixTime(DateTime.Now.ToString("hh tt"));
int currentMinute = FixTime(DateTime.Now.ToString("mm tt"));
int currentSeconds = FixTime(DateTime.Now.ToString("ss tt"));
string currentTT = DateTime.Now.ToString("tt");
int userHour = Settings.Default.hour;
int userMinute = Settings.Default.minutes;
int userSeconds = Settings.Default.seconds;
string userTT = Settings.Default.TT;
if (currentHour == userHour && currentMinute == userMinute &&
currentSeconds == userSeconds && currentTT == userTT)
{
MakeThePost(postTextBox.Text); // the postTextBox.Text field automatically is set to null
}
}
private void MakeThePost(string data)
{
string text = data;
if (!String.IsNullOrEmpty(text))
{
fb.Post("me/feed", new { message = text });
}
else
MessageBox.Show("Nothing to post on facebook", "Field is empty",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
UPDATE: So aparently the problem isnt in the timer or even in the MakeThePost the real problem is in another form iam trying to display, when its displayed the constructor makes a new Form1 object, why i make this?, because i want to have access to the controls in the main form, but when i execute the Form1 constructor to make a new object, the Form1 postTextBox control stop updating its Text property, so is there another way to access main Form controls without initializing an object?
The posted code doesn't seem to contain anything that would cause the postTextBox.Text property to become null.
There has to be something else in your application that nulls that property. Make sure Data Binding is not silently setting this to null.
Insert breakpoints both on the line calling MakeThePost, and on the first line of MakeThePost, then run the application.
See if the property is null only before the call, or both before and inside the method.
EDIT:
According to your latest edits, it seems you're creating a new Form1 instance, whereas you should probably pass a reference to the existing one when instantiating Form2
Add a Form2 constructor that takes a Form1 object as parameter:
public partial class Form2 : Form
{
private Form1 otherForm;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 frm) : this()
{
otherForm = frm;
}
//Other methods and properties of Form2
}
Then create a new instance of Form2:
Form2 secondForm = new Form2(form1Instance);
Now, inside the Form2 instance, you have the otherForm reference, which will allow you to call public members of the Form1 instance. You might have to change the access modifier on some Form1 members to make them public.

Fully access a control from another Form

I have a main Windows Form (From1) which has a TextBox in it. I've also created another Windows Form (FindReplaceForm) which I'm going to use for implementing a form of find and replace dialog. I need to fully access all the properties and methods of my textbox in From1 from FindReplaceForm window.
Although I set the Modifiers property of my TextBox to Public, in FindReplaceForm window I can't access the text in it.
You can access the the owner form in the child using:
((Form1)Owner).textBox1.Text = "blah";
Assuming you have called your the child form using:
Form2 form = new Form2();
form.Show(this);
You need to pass a reference to the control or the form to your constructor so that you can reference the instance of the class. Add an argument of the same type as the calling form to the constructor:
private Form1 calling_form;
public FindReplaceForm (Form1 calling_form)
{
this.InitializeComponent()
this.calling_form = calling_form;
}
Then in your button call you can say:
calling_form.TextBox_value_text.SelectedText = "";
In your code, Form1 is a CLASS, not a variable. When you show your FindReplaceForm you should specify the Owner (just use this).
Now you can the Owner property on FindReplaceForm to get access to Form1.
Showing FindReplaceForm:
FindReplaceForm.Show(this);
In your button click event:
void Buttton1_Click(object sender, EventArgs e)
{
((Form1)this.Owner).TextBox_value_text.SelectedText = "";
}
Even if you set the textbox access modifier back to private, you can simply pass a reference to the textbox in the second form's constructor, thus enabling the second form to manipulate any property of the textbox:
first form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this.textBox1);
frm.Show();
}
}
second form:
public partial class Form2 : Form
{
private TextBox _OwnerTextBox;
public Form2(TextBox ownerTextBox)
{
InitializeComponent();
_OwnerTextBox = ownerTextBox;
this.textBox1.Text = _OwnerTextBox.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
_OwnerTextBox.Text = this.textBox1.Text;
}
}
In your FindReplaceForm.button1_Click function you want to control an object of class Form1. The error in your code explains that you don't call a function of an object of class Form1, but a function of class Form1 itself. The latter can only be done on static functions
You describe that you have an existing Form1 object and that your FindReplaceForm needs to change Form1 by calling functions in Form1. Probably there might be more than 1 Form1 object. Therefor your FindReplaceForm instance somehow needs to know which Form1 object it needs to control. Someone needs to tell this to your FindReplaceForm. This is usually done using the constructor of the FindReplaceForm or using a property.
public class FindReplaceForm
{
private Form1 formToControl = null;
public FindReplaceForm(Form1 formToControl)
{
this.formToControl = formToControl;
}
private void OnButton1_Click(...)
{
this.formToControl.TextBox_Value_Text.SelectedText = ...
}
Another method would be not to put the formToControl in the constructor, but as a property that you can set separately. Both methods have their advantages:
via constructor: your FindReplaceForm knows for sure there is alway a formToControl. The only place you have to check if formToControl really exists is in the constructor.
Using a default constructor with a separate FormToControl property may cause run time errors if people forget to set the FormToControl.
If you have a lot of properties, or some properties may have default values, or may be changed later, then the property method is more flexible.

Use information from objects in a child form from a main form in C#

I have a C# form called Form1.cs. By pressing a button, a new Form called Form2.cs comes up and I do something in form2. I have some variables in both forms.
I want to communicate between these two forms like this.
in form1:
string s=frm2.textbox1.text;`
form2:
if(frm1.checkbox1.checked==true)
or something like these codes.
I have tried the below code:
form1:
Form2 f=new Form2(this);
f.showDialog();`
form2:
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
`
and this works. But I don't want to use pointers like "this" and call this.mainform. Is there another way to communicate between forms like function calls?
Thank you.
Here are a couple of different approaches you can take that remove the need for Form 2 to know about Form 1 and that will make Form 2 reusable:
Delegates
You can declare a delegate function on the second form, and then pass a delegate method from the first form to the second one, so the second form can call back to the first one.
That approach means your second form no longer has any direct knowledge of your first form. It also means you can reuse the second form from any form and just pass in a delegate with the correct signature. Example below:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f=new Form2(UpdateTextBox);
f.ShowDialog();
}
private void UpdateTextBox(string newText)
{
label1.Text = newText;
}
}
Form 2:
public partial class Form2 : Form
{
public delegate void ChoiceMadeOnForm2Delegate(string choice);
private ChoiceMadeOnForm2Delegate _choiceDelegate;
public Form2(ChoiceMadeOnForm2Delegate choiceDelegate)
{
InitializeComponent();
_choiceDelegate = choiceDelegate;
}
private void saveButton_Click(object sender, EventArgs e)
{
_choiceDelegate(textBox1.Text);
Close();
}
}
In this example the delegate method just has one parameter, but if you want to pass back a series of values to Form 1 your delegate method declaration could include more parameters, or the parameter could be a class instead.
If you also want to set initial values for Form2 from Form 1 you can of course add those as constructor parameters for Form 2 and pass them in when you new up Form 2.
In your example Form 2 is shown as a dialog, but if you ever want to not show Form 2 modally you could even have a delegate on Form 1 that you pass to Form 2, so the forms can then communicate in two directions.
Use data binding and a shared class
Another approach is to use databinding, whereby you bind both forms to the same object and pass that object from Form 1 to Form 2 in its constructor when you open Form 2. When either form then changes the object those changes will then be reflected on both forms simultaneously and instantly.
To do that you need to read up on a concept called databinding and implement the INotifyPropertyChanged interface on the data class. You then bind the relevant controls on both forms to that class. Documentation on INotifyPropertyChanged can be found here
You can pass only information that Form2 needs and expose only information that Form1 needs
Form1
string valueOfForm2 = null;
using Form2 f = new Form2(this.checkbox1.Checked)
{
f.ShowDialog();
valueOfForm2 = f.ReturnValue;
}
Then use valueOfForm2 for you needs in the Form1
Form2
bool _IsForm1ValueChecked;
//By this property text will be exposed to outside of Form
public string ReturnValue { get { return this.textbox1.Text;} }
public Form2(bool isForm1ValueChecked)
{
_IsForm1ValueChecked = isForm1ValueChecked;
}
_IsForm1ValueChecked will be set in the contructor - then use it for your purpose in the Form2
I think for such stuff, I was using properties.
I prefer not access from one form the controls of the other one.
If I need information from one form, I prefer giving the access to this other form through properties.
More than that, you can define an interface that will contain all the properties/methods that you need for the communication between the forms. It will be clearer to work with an interface, you will get the information you need and won't be overloaded with other irrelevant information.
For example:
Interface IForm2
{
// your properties
string PersonName {get;} // Just an example
// your methods
}
class Form1: Form
{
private IForm2 _form2;
void Foo()
{
var pname = _form2.PersonName; // Just an example
}
}
class Form2: Form, IForm2
{
string PersonName
{
get
{
return personNameTextBox.Text;
}
}
}

getting textbox value from another form

I have an TextBox named pass in Form1 that I need to get the value of in form2. I tried this:
public partial class Form1 : Form {
public string GetPass() {
return pass.Text;
}
}
public partial class form2 : Form {
//...
MessageBox.Show(new Form1().GetPass());
}
The above code returns an empty string, why?
You are not showing your actual code as evidenced by the syntax errors etc. - the only logical explanation for your problem is that you are not passing the reference to Form1 correctly to Form2, but create a new form instead - that new form would have the empty textbox.
To further help you, please show how you pass the reference to your Form1 in your actual code.
Edit:
Is see your edit now and above is exactly the problem. You have to pass a Form1 instance to form2 instead of creating a new one, i.e.:
public partial class form2 : Form
{
private Form1 form1;
public form2(Form1 otherForm)
{
form1 = otherForm;
}
public void Foo()
{
MessageBox.Show(form1.GetPass());
}
}
Define one string variable as Public in declaration section
for ex. we have a form with name "frmOne"
public string strVar = string.Empty;
Now, assign the value of TextBox of "frmOne" to that variable from where you are getting the value of Textbox.
for ex.
strVar = Textbox1.Text.ToString();
Now in another form say "frmTwo", you will get access the value of that textbox of "frmOne" something like that (where you want to get the value) :
frmOne frm = new frmOne();
string strValue = frm.strVar;
So, finally strValue local variable of frmTwo contains the value of Textbox of frmOne.
You are creating a NEW form1 where the textbox is likely to be blank, and calling GetPass() on that empty form. You need an instance of the already-opened form1 where the textbox might have a value.
Because you are creating a new instance of Form1 each time you call GetPass().
You need to get the instance of the opened form1 one way or another, and call GetPass on it:
form1.GetPass();
If there is no specifics on the order of creation of form1 and form2, you can use the following to get the instance of form1:
foreach (Form openedForm in Application.OpenForms) {
if (openedForm.GetType() == Form1) {
MessageBox.Show(openedForm.GetPass());
}
}
It's returning empty because you're creating a new instance of the form. Assuming that Form1 is already open somewhere, you need to retrieve the existing instance of Form1 and pull the value from there.
hi you can write this :
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
internal Form2 F2=new form2();
private void CommandBarButton1_Click(object sender, EventArgs e)
{
MessageBox.Show(f2.TextBox1.Text);
}
}

Categories