As you can guess, I\'m kind of new to .NET and just want to reference a control on one form from another.
Usually I would just do Form.Control.Property but that does
You should be able to make whatever you need to reference outside the form public. In many cases that's all that's needed.
Sometimes, it's useful to create a separate public property or method. And have the method take care of the details. For example, if you just want to able able to access the text, you could create a property something like this (in C#).
public string CustomerNo
{
get
{
return txtCustomerNo.Text;
}
set
{
txtCustomerNo.Text = value;
}
}
While you can create a property on your second form that will return the data needed, you can just as easily change the Modifier property of that control. By default, the modifiers of controls are private, and therefore not accessible to anything other than the current form. Click on the control, and find the Modifier property. Set that to Public, and you'll be able to access it from outside the form, such as:
Dim f As New SecondForm
f.FirstNameTextBox.Text = "Bob"
My.Forms.frmGenerate.txtCustomerNo.Text
I save the name of the person logging in to a textbox on the next form after they login like this. My login button has the last lines: Me.Hide() ( Not Me.Close() ) Admin.Show() End
When the user enters a correct username and password, it hides the login form and opens the next page, Admin.vb
In the Admin_Load of the Admin.vb page as the last lines I put:
Dim logged As String logged = Login.txtUser.Text Login.Close(). 'Close hidden login.vb' End Sub
So, the user presses the login button and it hides the login form with the value I want in the txtUser textbox. Then it opens the Admin page. The load event of the Admin page, gets my value from the hidden login page in the txtUser textbox and puts it in the parameter "logged." Once the value is grabbed, it closes the login page and leaves the sub. It all happens in the blink of an eye! Now anywhere I want to use the name of the person that logged in, like in a textbox I just use "logged."
txtchangesby.Text = logged
Very simple and works.
In the form with the control you want to reference:
Public Property CustomerNo() As TextBox
Get
Return txtCustomerNo
End Get
Set(ByVal Value As TextBox)
txtCustomerNo = Value
End Set
End Property
In the form you wish to reference the control:
Dim CustomerNo as string = _sourceForm.CustomerNo.Text
It's a bad design to do this, but it's the simplest method - and should set you on your way.
If you are only interesting in the value entered in the text box then the following might be better:
Public ReadOnly Property CustomerNo() As String
Get
Return txtCustomerNo.Text
End Get
End Property
The above will require the second form to have a reference to the actual instance of the first form. Add the below to the second form:
Private _sourceForm as frmGenerate
Public Property SourceForm() As frmGenerate
Get
Return _sourceForm
End Get
Set(ByVal Value As frmGenerate)
_sourceForm = Value
End Set
End Property
Now you can do the following where you handle the creation and startup of your second form:
Dim customersForm as new frmCustomers
customersForm.SourceForm = Me
customersForm.Show()
EDIT: I have constructed a sample project for you here:
http://www.yourfilelink.com/get.php?fid=595015
You need to ensure that the properties you add are public
, or they will not be accessible by other classes.