passing data from textbox in form1 to textbox in opened form2 in visual basic - vb.net

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

Related

How to keep the data of a form after hiding it

The user enters data in Form1 for example his name and phone number, he clicks on the "next" button that opens Form2 and Hide Form1, if he clicks on the "back" button I want the program to show Form1 with the data he entered before
The code for the "Next" Button in Form1:
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
Dim MyForm As New Form2
MyForm.Show()
Me.Hide()
End Sub
What should I do in order to keep the data the user entered if he comes back to Form1?
The code for the "Previous" button in Form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Me.Close()
End Sub
One approach you could use is to only the use the DEFAULT INSTANCES of your Forms. These are accessed by using only the name of the Form, WITHOUT ever using the "New" keyword.
You're doing exactly that in Form2 when you show Form1 with:
Form1.Show()
This works because Form1 is your startup object and VB.Net used the default instance to start the application.
You could do the same thing in Form1, to show Form2:
' ... code in Form1 ...
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
Form2.Show() ' show the default instance of Form2 (do NOT use the "new" keyword)
Me.Hide()
End Sub
Just make sure in all places you Hide() the current form instead of closing it.
You can access the properties/fields of the default instances using the same syntax to show them, using only their name. For example, here is a made up value being accessed on Form2:
Dim userName As String = Form2.txtAddress1.Text
This assumes that Form2 was previously displayed and populated by the user. You could access the Forms from anywhere in the code using this type of syntax.
If you want to implement your own "default instance" mechanism, you could use Shared members in a class:
Public Class WizardForms
Public Shared F1 As New Form1
Public Shared F2 As New Form2
Public Shared F3 As New Form3
End Class
Then you could use code like this to display one:
WizardForms.F2.Show()
This would work as long as you are only HIDING the forms. If you allow the user to close the Forms (or close them via code), then you'd need extra code to make sure they get recreated as needed.

VB.Net Add value from datagridview to listbox in another form

I have a trouble on VB.Net desktop programming, I have a Form with a ListBox, Called Form1, this form have a button that will appear another form with DataGridView inside that new Form, we will call this as Participant. Then I have a scenario, when User click the Button, it will call the Participant Form, and when user click the datagridview row in the Participant Form, it will get the value from that row and add the value to the ListBox in Form1, I have done getting the value from the row, but I can't add the value to the ListBox. This is my code to help you understand what I have done.
This is the code that will call the Participant Form :
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
Dim myForm As New Participant
If myForm Is Nothing Then
myForm = New Participant
End If
myForm.Show()
End Sub
And this is the code that will add the value from DataGridView in Participant Form to the ListBox in Form1
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim yourColumnIndex As Int32 = 1
If e.ColumnIndex = yourColumnIndex Then
participant_Share = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Form1.ListBox1.Items.Add(participant_Share)
End If
End Sub
But this code cannot insert the value to the ListBox, Please help me if you know how to solve this.
Thank you
You can change Participant constructor to accept a ListBox as parameter and store it in a local variable.
Private yourListBoxReference As ListBox
Public Sub New(ByVal yourListBox As ListBox)
InitializeComponent()
yourListBoxReference = yourListBox
End Sub
So you can call this form as:
myForm = New Participant(yourListBox)
And use the local variable to add an element to your ListBox:
yourListBoxReference.Items.Add(participant_Share)

How can I pass value from DataGridView to another DataGridView on another form?

How can I pass values from a DataGridView to another DataGridView on another form? Please I need this answer deadly.
I have a form with a DataGridView, I am using this as invoice so at the event of end_edit for the field of item name I make a search by the first letters of this item name and put the result in another DataGridView on another form. I need to when I choose the item that I need from the form number 2(search form) to move to the same cell in the form number 1(invoice form) ?????
Thank you
You have to be more specific about your Question
given this scenario
form1 with a datagridview and form2 with a datagridview
you open form2 from form1 you would go to form2 and put sub new in it with reference to the main form like
Private f1 As Form1
Public Sub New(ByRef f As Form)
InitializeComponent()
Me.f1 = f
End Sub
on Form1 show your Form
Dim f2 As New Form2(Me)
f2.Show()
then on form 2 you have access to form1
assuming both tables have the same Datasource` bound
you can go on the ClickCell event from your Datagridview on Form2
and put in something like this to be at the same position on both DGV's
Private Sub xDataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles xDataGridView.CellClick
f1.xBindingSource.Position = Me.yBindingSource.Position
End Sub
this however will only work if you don't allow sorting of the columns otherwise it will be in defferent rows.
if you want to pass a value from dgv on form2 to dgv in form1
then you can do something like this
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button2.Click
f1.xDataGridView.CurrentRow.Cells("MyCell").Value = Me.xDataGridView.CurrentRow.Cells("MyCell").Value
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.