Form1 & Form2

maxpic55

Member
Joined
Dec 16, 2002
Messages
15
Hello,
How can I do?
I have two forms (form1 and form2) open.
if I push a button on form2, I want that a value goes to a textbox on form1.
Thank you

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Codice generato da Progettazione Windows Form "
.......
.......
#End Region

Dim frm As New Form1()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

frm.Text1.Text = "PROVAR"
frm.Refresh()

End Sub
End Class
 
Edit the form constructor. If the form1 is the one you open first, leave it like it is and edit the constructor of the second one so it accepts a form instance as an argument, like this:
Code:
this in your second form
Dim firstform As Form1 variable to hold an instance of the first
form that will be available to the whole class
Public Sub New(ByVal frm As Form1) accept an instance of the form
MyBase.New()
InitializeComponent()
firstform = frm assign the passed in instance to the variable that can
be accessed in whole class
End Sub
Then you just access form with firstform.TextBox1.Text = "something" or however you want to call it.
:)
 
Last edited by a moderator:
Any ideas how to do this in c#.net

Hi, Im new to .Net but working on the same
problem in c# and kinda stuck.

If you can help me out, I would be very greatful.

Cheers

Wacy1
 
Originally posted by wacy1
Hi, Im new to .Net but working on the same
problem in c# and kinda stuck.

If you can help me out, I would be very greatful.

Cheers

Wacy1

What is the problem you are having with it?
 
Form Problem

Hello Mutant,

Well Im developing a Pocket Pc app that needs to take information from a number of forms. But there seems to be a problem with accessing the controls on a form that has been opened using the ShowDialog method.

Using the code below, throws a null exception when I try to access the text in test dialogs TextBox1.

Form2 testDialog = new Form2();
String s = "";

// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialogs TextBox.
s = testDialog.TextBox1.Text;
}

In form 2 a simple button that on click does:
DialogResult = DialogResult.OK

The dialog box closes and any references to the dialog box causes an exception.

It works fine for Windows froms, just not in a smartdevice app.
If anyone could shed some light on it, I would be greatful.

Cheers in advance

Wacy1
 
Back
Top