Hi, 2 little questions pls:
From a form frm1 I call another form frm2. The form frm1 has a text box with a text in it.
I don t know how to access that value in frm2.
Also I would like to access i frm2 certain vaiables defined in frm1.
2/ What d be the help section in Visual Basic where I ca find such guidelines.
Thanks a lot

I don t know how to pass variales and text controls values between forms
Nick Waanders
This is an intersting solution, but I don't think its best practice.
Events are very expensive, same as Exceptions, and should be used only when needed.
If you have a lot of forms and interactions between them, you will have a large number of events that you don't need. remmeber that the buttons click allready fires one event, and than you want to fire a new event...
Dan Handevik
Therefore, I m not sure that I understand eligazit's both solutions(described in the first answer to my original message: dated: 12-17-2005, 8:20 AM UTC) that seem interesting, or I must be doing some thing wrong.(I m using V.B2003 and not VC++). I tried them this way:
1/
I declare the textbox1 in the parent form1 with modifier: public as u suggested.
From Form1 I do a form2.showdialog(Me)
In form 2 from which I want to acess TextBox1 which s located in Form1 I do:
dim f as form1
f=Me.parent (like me.this in VC++ I guess)
then when I access: f.textbox1 (always from form2) I get the error: Object reference not set to an instance of an object.
So for your solution dear Eliz when you wrote Form2.textBox1.Text = "Something"; unfortunately it doesn t fix my pb since I want to access Form1.TextBox1.text from Form2 instead of the opposit.
2/ I also tried your other solution, which seems interesting and more professional but, it ddn t work for me either, so i m not sure what i m doing wrong, cause I got the same error:
I tried it this way:
In Form1, I declared the public property:
Dim _SomeValue as text
Public Property SomeValue() As Integer
Get
_SomeValue = Form1.textbox1.text
Return _SomeValue
End Get
End Property
In form2 I access: Me.parent.SomeValue or I do
dim f as form1
f=me.parent
msgbox f.somevalue
but I get the error:Object reference not set to an instance of an object.
Thanks and Merry christmas
Kimber20
I really don't know a lot about VB, but shouldn't this code read:
Dim f1 As Form1 = Me.Parent as Form1
If Not form1 Is Nothing Then
; msgbox etc
End If
If you're not planning to check the return value of the Parent property, you might just as well just perform a static cast (which will throw an exception if it is of the wrong type):
Dim f1 As Form1 = CType(Me.Parent, Form1)
But ouch, I think I made a very unfortunate mistake... you see, can't check any sample code I create here at home since my PC is still broken. Uhm... I think you shouldn't be checking the Parent property but rather the Owner property. A form has no parent because it is not contained, but it is owned by another form. Could you try checking the me.Owner property
Also, this post might also help:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=158585&SiteID=1
I hope this finally resolves your issue.
JustLearninC#
That's not such an easy question. Your situation was not really specifically targetted to Windows Forms, rather to application structure and common C# language constructs. In that case you would do well to check the FAQ for book recommendations.
Ikbal
1. To get access to a TextBox from form1 and form2, all you have to do is to set the TextBox 'Modifiers' property to public (private is the default). this means that other forms can access this control:
Form2.textBox1.Text = "Something";
The better way to do it, also for your other question, is to use accessors:
private int _someValue;
public int SomeValue
{
get { return _someValue; }
set { _someValue = value; }
}
This is the right way to allow access to a class member.
ArtNJ
First, declare an object at the top of your first form (in this case frm1) for each form you want to access data from:
/// <summary>
/// Required designer variable.
/// </summary>
///
public static frm1 frm1Object;
public static frm2 frm2Object;
And then in the void main on the same form, make new objects and pass in the frm1Object:
static void Main()
{
frm1Object = new frm1();
frm2Object = new frm2();
Application.Run(frm1Object);
}
Now, if you want to access textBox1 from frm2, then make textbox1 public static on frm1:
public static System.Windows.Forms.TextBox textBox1;
Also (I just discovered when testing this with a TextBox) everywhere you use textBox1 on frm1, you have to change the "this." to "frm1.":
frm1.textBox1.Text = "textBox1";
And to do something on textBox1 from frm2:
frm1.textBox1.[whatever];
I've tested this out on my computer using the command of Hide(); for textBox1. Hope that helps!
Shawn Anderson 020564
I believe the most elegent solution is to pass values using event notification. I found this solution on another site and tried it to pass data between MDI child and parent -- it works fine.
1. In the child form (the form that you want to send information from), first create the event handler:
public event EventHandler NotifyParent;
2. Also in the child form, create the event handler code. Note that here we are sending the value of "textBox1.text" as data when we publish this event.
protected void OnNotifyParent(){
NotifyParent(textBox1.text,
EventArgs.Empty);}
3. The final task in the child form is to create the code to actually fire this event we have created. Here we are firing the event when a button is clicked:
private void btnOK_Click(object sender, EventArgs e){
this.OnNotifyParent();}
4. Next we need to add code to the MDI parent window to subscribe to this new event that the MDI child window will be firing when the "OK" button is clicked. First we wire up the event by adding the EventHandler to the code that creates and shows the child form:
private void addChildForm(){
childForm =
new childForm();childForm.MdiParent =
this;childForm.NotifyParent +=
new EventHandler(childForm_NotifyParent); //<=== add this EventHandlerchildForm.Show();
}
5. Finally, we add a method to process the event when it is received by the parent form. Here we are taking the text value sent from the child form and displaying it in a text box on the parent form. The "object sender" is the "textBox1.Text" value sent in the "OnNotifyParent" method in the child form above:
private
void childForm_NotifyParent(object sender, System.EventArgs e){
txtParentForm.Text = sender.ToString();
}
Dracoo
Do you know in what section or any resources if i wanna read more about this issue. either in visual stdudio help (what would be the section) or any other resource in the web for this particular problem of passing data or vaiables between forms especially from the parent to the child form.
Thanks again and have a good sunday.
etrek
Thanks a lot for your time and help guys
I ll check that out tonight
BradB1976
Solution two, with the accessors (properties), is the better solution. It encapsulates the field, which is good practise.
There can be three reasons why you are getting this error. The first is because the textbox is null (nothing in VB) at the moment you are accessing it. A textbox is null as long as the InitializeComponent has not yet been called. Check the InitializeComponent call to see that it performs the 'new' allocation. Because the textbox is initially null, you will always need to check the value of the textbox to see if it doesn't equal null/Nothing. In this case, you will need to call the property on a different location, after it has been properly initialized. In a form, the Load event would be a good place.
As a sidenote: I personally wouldn't put any error handling in the properties (the get/set methods), as the debugger validates properties just like it does with fields. If an assertion is triggered in a property, the debugger will assert as well.
Back to the problem: The second probable cause could be that the Parent property could be null/nothing.
The third possible problem could be that the Parent property of your class if not of type Form1. This is quite improbable, but you would need to check. The 'as' operator tries to perform a dynamic cast. That means it tries to convert the value that is returned to a value of type Form1. If that is not the case, it puts null/nothing in value. You should check that.
What you need to do is to check the return value of the Parent property for null/nothing. It should actually be common practise to either assert on such errors, or to throw exceptions, or to simple ignore such cases and find a good way to work around the error, if possible.
S.Guhananth
Unfortuantely, I can access
form2.textebox1
from form1 which is the parent, but
I can t access : form1.textbox1 from form2 which is the child
Please if you can assist any further, thanks
Ian Sullivan
I think it s rather your second or third possibility.
Regarding your first one, the text box has a value; I even tried calling it after I gave it a value "a first Name for example". But still my new form is not recognizing that value of fom1.
So It s either your second or third assumption. May be the parent of my form2 is not form1, I don t know why that might be the case, or the parent of form2 is simply empty. To give you more details about how I create form2:
I create a form1 of type IsMdicontainer = False and textbox1.text="First Name" with modifier property= Public
I launch the form2 from form1 using this code in Form1:
Dim f As New Form2
f.ShowDialog(Me)
f.BringToFront()
Then in a button Click event handler in Form2 I use for example:
Dim f1 As Form1
f1= Me.Parent
MsgBox("Name: " & f1.textbox1.text)
But I get the error:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication2.exe
Additional information: Object reference not set to an instance of an object.
So I guess there is something wrong with the parent of fom2; may be its parent is not form1.
Because even displaying : Me.parent.name in form2 gives me the same error. so there must be a pb with parent-child relation between the 2 forms.
Take care BROs.
Arix
I don't think it is a very good idea to make everything public and static. Sure, that will get you access to everything, but design-wise that's not really a good idea. That'll make everything global. It will also not work when you have multiple instances of Form1 and/or Form2 running.
I think Eligazit's answer is the correct one. When opening frm2 from frm1, pass the 'this' pointer to ShowDialog to set frm1 as parent of frm2. Like:
Form2 frm2 = new Form2();
frm2.ShowDialog( this );
In frm2, you can use the Parent property to get the frm1 instance. Don't forget to cast it. Like this:
Form1 frm1 = (Form1)this.Parent;
Now, as Eligazit described, you should create public properties or methods in Form1 so that Form2 can access them. You can make the TextBox data member public, but that is not really good practise. As Eligazit suggested, build accessors.
Now just one of the methods to obtain these values. Like:
string textBoxText = frm1.GetTextBoxValue();
Muthu.R
Thanks to all the guys, u re really great, happy new year 2006 , hopefully, with no mistakes when debugging