access to another forms objects
-
Hi, I have two windows forms.User opens the first form(form1) and clicks "open" button then a new form opens(form2). He makes a choice in form2 .I need to add some string to listBox that is on form1 when user clicks button on the form2. Both forms are open at this time. If i use "Dim myForm As New Form1" it makes the change i need but it creates a new form1. would someone send some syntax for that? Thanx in advance.. --junior coder--
-
Hi, I have two windows forms.User opens the first form(form1) and clicks "open" button then a new form opens(form2). He makes a choice in form2 .I need to add some string to listBox that is on form1 when user clicks button on the form2. Both forms are open at this time. If i use "Dim myForm As New Form1" it makes the change i need but it creates a new form1. would someone send some syntax for that? Thanx in advance.. --junior coder--
You can pass the form1 as reference to form2 and when the user clicks button on form2 set the string in form1's reference. That is, When displaying form2 from form1 you would say, Dim form2 as new form2(form1) In the Form2's constructor(Public Sub New) say Public Sum New(byref Form1 as form1) Hope this helps.
-
You can pass the form1 as reference to form2 and when the user clicks button on form2 set the string in form1's reference. That is, When displaying form2 from form1 you would say, Dim form2 as new form2(form1) In the Form2's constructor(Public Sub New) say Public Sum New(byref Form1 as form1) Hope this helps.
Here is form1 code:
Public myform2 As New form2
I defined form2 as global variable Here is form2 code when you hit a button:Dim f1 As New Form1 f1.Show() f1 = Me.Owner f1.textBox1.Text="something else"
But it does not work :confused: here is error message: An unhandled exception of type 'System.NullReferenceException' occurred in TestForms.exe Additional information: Object reference not set to an instance of an object. --junior coder-- -
Here is form1 code:
Public myform2 As New form2
I defined form2 as global variable Here is form2 code when you hit a button:Dim f1 As New Form1 f1.Show() f1 = Me.Owner f1.textBox1.Text="something else"
But it does not work :confused: here is error message: An unhandled exception of type 'System.NullReferenceException' occurred in TestForms.exe Additional information: Object reference not set to an instance of an object. --junior coder--I assume you have a button in form1 to invoke form2. So put this code in button_click event of the form1 where you call the form2. Form1 Code ---------- Dim myform2 As New Form2(Me) myform2.ShowDialog() In form2 Declare a Variable Dim prevform As Form1 If you click on the + sign near the Windows Form Designer generated code,You will see the Public Sub New. it should look like this. Public Sub New(ByRef form1 As Form1) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() prevform = form1 'Add any initialization after the InitializeComponent() call End Sub In the Form2 button_click (where you want to call form1, paste this code. prevform.TextBox1.Text = "something else" Me.Hide() This should work fine.
-
I assume you have a button in form1 to invoke form2. So put this code in button_click event of the form1 where you call the form2. Form1 Code ---------- Dim myform2 As New Form2(Me) myform2.ShowDialog() In form2 Declare a Variable Dim prevform As Form1 If you click on the + sign near the Windows Form Designer generated code,You will see the Public Sub New. it should look like this. Public Sub New(ByRef form1 As Form1) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() prevform = form1 'Add any initialization after the InitializeComponent() call End Sub In the Form2 button_click (where you want to call form1, paste this code. prevform.TextBox1.Text = "something else" Me.Hide() This should work fine.
thanx it works fine..:-D --junior coder--