Vb.Net - Accessing text in controls on another form - vb.net

I am fairly new to vb.net and would like to be able to access the value (such as .text on a textbox) from another an open form. In my application I open a form from my main form, and when I try to access the text in the controls on the main form I am unable to see the .text value on the control.
I can loop through all controls on the main form just fine, but when I want to see the actual values, all controls are empty. My controls such as text boxes and combo boxes are inside of a tab control and group boxes.
Is there a way to make all .text or values on the open form available from the other open form?
Here is how I am looping through the controls on the main form.
Try
For Each Tp As TabPage In UserData.UserTabControl.TabPages
'Name of Tabcontrol is UserTabcontrol
For Each gbx As GroupBox In Tp.Controls
For Each ctrl As Control In gbx.Controls
If ctrl.Name = "UserName" Then
MsgBox(UserData.UserName.Text) 'Messagebox here is empty
End If
Next ctrl
Next gbx
Next Tp
Me.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Thanks in advance.
Chris

From the example you've given you already have access to a reference to your Control. Instead of going back to the Form and trying to access that control as a property of the Form you could just cast your reference and call its Text property directly.
If ctrl.Name = "UserName" Then
MsgBox(DirectCast(ctrl, TextBox).Text) 'Assuming your UserName control is a TextBox
End If

If you would like to reference controls on an open form, call it Form1:
First add a Form1 property or variable to the calling form:
Public Class Form2
Public Property f1 As Form1
...
Private Sub DoSomething()
MsgBox("Here's some text from Form1: " & f1.Textbox1.Text)
End Sub
End Class
In the callee form, set the Form2 property to the form object:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.f1 = Me
Form2.ShowDialog() ' or Form2.Show()
End Sub
End Class
You can then reference all Form1 objects from Form2 using the f1 property.

By this command, I could change the amount of control in another form.
My.Forms.myForm.labelControl.Text = "bela"
Please try this :
MsgBox(My.Forms.UserData.UserName.Text)

Related

Textbox all properties not working when textbox added in other control

I have added a windows form on control panel using following code
Dim frmopen As New WinForm1
panel1.Controls.Add(frmopen)
Form opens normally work also but my concern is,
I have one textbox on that WinForm1, when i am typing text in that textbox it takes input but when i am clicking in between that typed text the cursor goes to either last character or first character.
So if i have to enter characters in between, then first i have to erase the typed characters and then i have to type again.
Plz help me to work textbox normally??
Thank you
Try changing the child form's FormBorderStyle to None.
See: Windows Forms: Unable to Click to Focus a MaskedTextBox in a Non TopLevel Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim frm2 As New Form2
frm2.TopLevel = False
frm2.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.Panel1.Controls.Add(frm2)
frm2.Show()
End Sub
End Class

passing data from textbox in form1 to textbox in opened form2 in visual basic

i have form1 to enter movies details
in this form1 i have a textbox called NVideosGenres
through this textbox i can open the form2 with a space
the form2 contains 5 combobox to let the users choose the genres if there is more then one
when user choose the genres they will be applied to a textbox in the same form like this way
for example i choose from three combobox
action - war - western
so now i have a problem because i know that to pass value i should do this
Videos.NVideosGenres.text = me.FinalGenres.text
me.close()
when i click on the button the form2 will close but the data don't pass to NVideosGenres in the form1
any help??
There are many, many ways to do this. One of the easiest ways would be to create a property on Form2 that holds a reference to your main calling form. Then you can set the values of the textboxes (or whatever) directly from Form2. This is definitely not the best way to do it, but it certainly is quick and easy.
Private Sub btnShowForm2_Click(sender As System.Object, e As System.EventArgs) Handles btnShowForm2.Click
'Create a new instance of Form2 (called frmDetail in this example).
Dim frm2 As New frmDetail()
'Set the MyParentForm property of Form2 to this form.
frm2.MyParentForm = Me
'Show the form.
frm2.ShowDialog()
End Sub
Now in Form2, when you click the "OK" button to close the form, you can use the property that references your parent form to set the values directly.
' Property to hold a reference to the calling form.
Private mParentForm As frmMain
Public Property MyParentForm() As frmMain
Get
Return mParentForm
End Get
Set(ByVal value As frmMain)
mParentForm = value
End Set
End Property
' When I click the OK button, I will store the value of my various textboxes to properties.
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
'Set the value of the Genre textbox on the parent form to the value
'of the textbox on my current form.
MyParentForm.txtGenre.Text = txtGenre.Text
Me.Hide()
End Sub

Loading a form dynamically [duplicate]

It takes two forms to be able to enter all the info on a particular transaction. I want to be able to flip back and forth between these two forms retaining what was entered on each until the 'Save' button is clicked.
I think I should be able to use Form2.Show, Me.Hide and then Form1.Show, Me.Hide.
The first time I go to Form2 it goes thru the Form2 load event (that’s reasonable) but any knowledge of the control contents on Form1 have been lost. Even though Form1 is hidden (and not closed) the contents of its controls are gone. Why?
The second time I go to Form2 the load event does not fire because Form2 is hidden and at this point all the Form1 control contents are available. So, in flipping back and forth between Form1 and Form2 it works as I want it to after I go to Form2 the second time. But, I need it to work the first time and every time.
For days I’ve been trying to understand this. I’ve commented out nearly every line of code, stepped thru code, googled till I’m blue in the face (there has been a lot written about this), and I still can’t figure out why this behavior occurs.
Can anyone explain this phenomenon? Or better yet tell me what I need to do to make this work.
I have this code behind the Form1 button that goes to Form2
If Form2 Is Nothing Then
Dim Form2 As New Form2
End If
Form2.Show()
Me.Hide()
And this code behind the Form2 button to return to Form1
Form1.Show
Me.Hide
This is all you are probably missing:
Class Form1
Private f2 As Form2 ' this is Form1's reference to the
' form2 instance
Later when you click to go to form2, your original code just needs a small tweak:
If f2 Is Nothing Then
f2 = New Form2(Me) ' set declared variable to new instance
End If
F2.Show()
Me.Hide()
In this case Form1 is passing the reference using the trick you were shown before using the constructor:
Sub New(frm As Form1) ' this is in Form2 only
f1 = frm
End Sub
You dont need this in Form1 because he/it is creating his own f2 object reference.
The main problem in your original code, was: Dim Form2 As New Form2. You are creating a new Form2 each time (I suspect that resides in an event or sub). Those new instances can't know the control values in the previous instances. Declaring F1 or F2 as shown gives it module/form level Scope.
Dim declares a variable and its Type. f1 is of Type Form1. It does not create an object if it is an object variable
New creates an instance of an object Type (reference types). This directly relates to the Sub New method in the class. When you use New, Sub New is called so anything special that is needed can take place there. Value types like Integer do not need to be created or instanced, only declared.
Where you declare (Dim) a variable determines its Scope. If you do this in a Sub, the variable or object only exists in that sub. if you do it at the form/class level, it has Form/Class level scope.
I would try something like this. Display the 2 forms modally using the ShowDialog method.
In each form, create a property (call it "OtherForm") which points to an instance of the other form.
Continue the loop by setting the DialogResult of either form to Windows.Forms.DialogResult.Yes
Exit the loop by setting the DialogResult of either form to Windows.Forms.DialogResult.No
This will execute each forms load event each time it is displayed.
So call this from a button click event procedure on your so-called Form0.
Dim Form1 As New Form1
Dim Form2 As New Form2
Dim sFormToShow As String = "Form1"
Form1.OtherForm = Form2
Form1.DialogResult = Windows.Forms.DialogResult.Yes
Form2.OtherForm = Form1
Form2.DialogResult = Windows.Forms.DialogResult.Yes
Do
If sFormToShow = "Form1" Then
Form1.ShowDialog()
ElseIf sFormToShow = "Form2" Then
Form2.ShowDialog()
End If
sFormToShow = IIf(sFormToShow = "Form1", "Form2", "Form1")
Loop While Form1.DialogResult = Windows.Forms.DialogResult.Yes AndAlso Form2.DialogResult = Windows.Forms.DialogResult.Yes
Add this property to Form1:
Private _myOtherForm As Form2
Public Property OtherForm() As Form2
Get
Return _myOtherForm
End Get
Set(ByVal value As Form2)
_myOtherForm = value
End Set
End Property
Add this property to Form2:
Private _myOtherForm As Form1
Public Property OtherForm() As Form1
Get
Return _myOtherForm
End Get
Set(ByVal value As Form1)
_myOtherForm = value
End Set
End Property
From each form, you will then be able to access all of the public properties and methods of the other form using the _myOtherForm variable.
If you were to call this from Form1, it will display the text in TextBox1 in Form2 and vice-versa:
MessageBox.Show(_myOtherForm.TextBox1.Text)
To close the current form, continue the loop and open the other form, add this to a button click event procedure:
Private Sub btnShowOtherForm_Click(sender As Object, e As EventArgs) Handles btnShowOtherForm.Click
Me.DialogResult = Windows.Forms.DialogResult.Yes
Me.Close()
End Sub
To close the current form, exit the loop and to cease showing any further forms, add this to a button click event procedure:
Private Sub btnStopShowingForms_Click(sender As Object, e As EventArgs) Handles btnStopShowingForms.Click
Me.DialogResult = Windows.Forms.DialogResult.No
Me.Close()
End Sub
These two procedures need to be added to each form.

How do I retain a form's control values when flipping between forms

It takes two forms to be able to enter all the info on a particular transaction. I want to be able to flip back and forth between these two forms retaining what was entered on each until the 'Save' button is clicked.
I think I should be able to use Form2.Show, Me.Hide and then Form1.Show, Me.Hide.
The first time I go to Form2 it goes thru the Form2 load event (that’s reasonable) but any knowledge of the control contents on Form1 have been lost. Even though Form1 is hidden (and not closed) the contents of its controls are gone. Why?
The second time I go to Form2 the load event does not fire because Form2 is hidden and at this point all the Form1 control contents are available. So, in flipping back and forth between Form1 and Form2 it works as I want it to after I go to Form2 the second time. But, I need it to work the first time and every time.
For days I’ve been trying to understand this. I’ve commented out nearly every line of code, stepped thru code, googled till I’m blue in the face (there has been a lot written about this), and I still can’t figure out why this behavior occurs.
Can anyone explain this phenomenon? Or better yet tell me what I need to do to make this work.
I have this code behind the Form1 button that goes to Form2
If Form2 Is Nothing Then
Dim Form2 As New Form2
End If
Form2.Show()
Me.Hide()
And this code behind the Form2 button to return to Form1
Form1.Show
Me.Hide
This is all you are probably missing:
Class Form1
Private f2 As Form2 ' this is Form1's reference to the
' form2 instance
Later when you click to go to form2, your original code just needs a small tweak:
If f2 Is Nothing Then
f2 = New Form2(Me) ' set declared variable to new instance
End If
F2.Show()
Me.Hide()
In this case Form1 is passing the reference using the trick you were shown before using the constructor:
Sub New(frm As Form1) ' this is in Form2 only
f1 = frm
End Sub
You dont need this in Form1 because he/it is creating his own f2 object reference.
The main problem in your original code, was: Dim Form2 As New Form2. You are creating a new Form2 each time (I suspect that resides in an event or sub). Those new instances can't know the control values in the previous instances. Declaring F1 or F2 as shown gives it module/form level Scope.
Dim declares a variable and its Type. f1 is of Type Form1. It does not create an object if it is an object variable
New creates an instance of an object Type (reference types). This directly relates to the Sub New method in the class. When you use New, Sub New is called so anything special that is needed can take place there. Value types like Integer do not need to be created or instanced, only declared.
Where you declare (Dim) a variable determines its Scope. If you do this in a Sub, the variable or object only exists in that sub. if you do it at the form/class level, it has Form/Class level scope.
I would try something like this. Display the 2 forms modally using the ShowDialog method.
In each form, create a property (call it "OtherForm") which points to an instance of the other form.
Continue the loop by setting the DialogResult of either form to Windows.Forms.DialogResult.Yes
Exit the loop by setting the DialogResult of either form to Windows.Forms.DialogResult.No
This will execute each forms load event each time it is displayed.
So call this from a button click event procedure on your so-called Form0.
Dim Form1 As New Form1
Dim Form2 As New Form2
Dim sFormToShow As String = "Form1"
Form1.OtherForm = Form2
Form1.DialogResult = Windows.Forms.DialogResult.Yes
Form2.OtherForm = Form1
Form2.DialogResult = Windows.Forms.DialogResult.Yes
Do
If sFormToShow = "Form1" Then
Form1.ShowDialog()
ElseIf sFormToShow = "Form2" Then
Form2.ShowDialog()
End If
sFormToShow = IIf(sFormToShow = "Form1", "Form2", "Form1")
Loop While Form1.DialogResult = Windows.Forms.DialogResult.Yes AndAlso Form2.DialogResult = Windows.Forms.DialogResult.Yes
Add this property to Form1:
Private _myOtherForm As Form2
Public Property OtherForm() As Form2
Get
Return _myOtherForm
End Get
Set(ByVal value As Form2)
_myOtherForm = value
End Set
End Property
Add this property to Form2:
Private _myOtherForm As Form1
Public Property OtherForm() As Form1
Get
Return _myOtherForm
End Get
Set(ByVal value As Form1)
_myOtherForm = value
End Set
End Property
From each form, you will then be able to access all of the public properties and methods of the other form using the _myOtherForm variable.
If you were to call this from Form1, it will display the text in TextBox1 in Form2 and vice-versa:
MessageBox.Show(_myOtherForm.TextBox1.Text)
To close the current form, continue the loop and open the other form, add this to a button click event procedure:
Private Sub btnShowOtherForm_Click(sender As Object, e As EventArgs) Handles btnShowOtherForm.Click
Me.DialogResult = Windows.Forms.DialogResult.Yes
Me.Close()
End Sub
To close the current form, exit the loop and to cease showing any further forms, add this to a button click event procedure:
Private Sub btnStopShowingForms_Click(sender As Object, e As EventArgs) Handles btnStopShowingForms.Click
Me.DialogResult = Windows.Forms.DialogResult.No
Me.Close()
End Sub
These two procedures need to be added to each form.

How to check a checkbox of form 1 from another form in vb.net?

I have two forms, form 1 and form 2 (windows application). How can i access and check a checkbox in form 1 from form 2. Initially i tried calling the form name and then the control like form1.chkCanada.checked = true, it did not work. And then i added a property in form 1
Public Property abc As Boolean
Get
Return chkCanadianStmtInd.Checked
End Get
Set(value As Boolean)
chkCanadianStmtInd.Checked = value
End Set
End Property
and then in form 2
Dim frm As New frm1
frm.abc = True 'Checked
And it still doesnt work. Am i missing anything here?
Alternatively you can pass a handle of form1 to form2 constructor
Form1:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim _form2 As New Form2(Me)
_form2.Show()
End Sub
End Class
Form2:
Public Class Form2
Public Sub New(ByVal _form1 As Form1)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_form1.CheckBox1.Checked = True
End Sub
End Class
Consolidating comments here:
In order to access controls on a form that shows another form to the user you have two options, if no interaction is needed with the first form while the second form is active you can use showdialog and do all of your logic after the second form has closed, if you ned to maintain the ability to interact with the first form while the second is still open then you need to use custom events.
Showdialog:
The simpler of the two options is to switch your form.show() function calls to form.showdialog(). This effectively tells the first form that it should stop processing at the form.showdialog() line and wait for the child form to close before proceeding. Once the second form is closed the first form will pick up where it left off and that would be where any processing that relies on the values of the second form would take place.
Custom Events:
If you want to allow the user to interact with both the first and second forms at the same time then you will need to use custom events. In order to do this you will need three things. The custom event, a raiseevent call and an event handler.
So in your Form2 class you will need to declare the custom event. In this case since you are trying to check(or uncheck I assume) a box your custom event declaration will look like:
public event ChangeCheckedValue(byref state as boolean)
Now on your button click event you will need to raise the event to the handler on Form1:
RaiseEvent ChangeCheckedValue(booleanValue)
Now that those statements are in place you will need to changed your form2 object that is being shown by Form1. What I normally do is make Form2 a form wide variable on Form1 and declare it like:
private withevents frm as Form2
Once you have the frm variable in your Form1 class you can add a handler for the ChangeCheckedValue event:
protected sub HandleCheckChanged(byref bln as boolean) handles frm.ChangeCheckedValue
'Set the checked state of your checkbox.
End sub
Once you have all that set up you should see what you expect.