How to pass text from one form to opened form at back? - vb.net

I have two forms. One is InvoiceEntry and another is ListofSuppliers. There is a control "txtsnam" in InvoiceEntry.ListofSuppliers contains a Datagrid which has all the names of suppliers. When user presses f9 on this textbox ListofSupplier form is opened. The user select the name of supplier from grid and press enter. On this press enter i want to pass the name of supplier to the "txtsnam".I have used this code but its not working for me:
On InvoiceEntry:
Private Sub txtsnam_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtsnam.KeyDown`
If e.KeyCode = Keys.F9 Then
Dim lov As New SupplierLOV
lov.ShowDialog()
End If
End Sub
On ListofSuppliers:
Private Sub RadGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RadGridView1.KeyDown
If e.keycode=keys.Enter then
InvoiceEntry.txtsnam.text=RadGridView1.CurrentRow.Cells(0).Value.ToString
End If
End Sub
I also tried with the help of property but its also not working. I placed the breakpoint on line 2 it executes bt text doesnot appear in txtsnam

Solution 1: Create an event in Form2 and subscribe to it in Form1 when you open the Form2. When pressing on the Return key, raise the event in Form2 and transmit the value as parameter. Form1 will receive your value.
Solution 2: Add a member to Form2 of type Form1 and pass Form1 as parameter to Form2 when you create Form2. In this way you have full access to Form1 properties and you can set it from code anytime. When you close Form2 you already have your data set in Form1.
Solution 3: Have a static provider class and store there your info that you want from different Forms. You can set and get these values from anywhere in your program.
Solution 4: ... there are many other ways. These were the simplest ones.

Try this
Private Sub RadGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RadGridView1.KeyDown
If e.keycode=keys.Enter then
InvoiceEntry.txtsnam.text=RadGridView1.Currentcell.value
End If
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.

Label won't update from separate form

I have 2 forms in my program. Form1 has a tab control on it, and on one of these tabs there are a load of labels. Form2 has a few textboxes and dropdown lists on it. Form1 has a button on it that opens Form2 on top of it as a normal form, not as a dialog.
There is code in Form1 that on loading populates the labels on it from a MySQL database. This code is in a separate public sub in the form that is called when the form loads.
What I am trying to do is fill in some of the boxes on Form2, when this Form closes it updates the database with these values (Works fine) and then those values are displayed on Form1. I can get Form2 to run the code in Form1 that populates the labels, but the problem is that the labels on Form1 never actually change. I have tried multiple things, including calling refresh on the labels, trying to change the labels from within Form2 instead of Form1, but they just never update. The .text value of the labels is being updated to the new value, I have checked this via debugging and stopping at the correct points to see what the value is.
Any ideas anyone?? I think it might be to do with the fact that it's Form2 calling the code and for some reason it doesn't have access to change the labels on Form1.
Code:
Form1 (AECSurveyForm)
Private Sub AECSurvey_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadMeterData()
End Sub
Public Sub LoadMeterData()
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
'utility
Meter1UtilityLabel.Text = AECMeteringData(0)("Utility1")
End Sub
Private Sub MeterButton1_Click(sender As Object, e As EventArgs) Handles Meter1Button.Click
If Not Application.OpenForms().OfType(Of AECMeteringDataForm).Any Then
AECMeteringDataForm.GetData(AECGlobalValues.CurrentSurveyUniqueIdentifier, 1)
AECMeteringDataForm.Show()
End If
End Sub
Form2 (AECMeteringDataForm)
Private Sub AECMeteringDataForm_FormClosing(sender As Object, e As EventArgs) Handles MyBase.Closing
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
AECMeteringData(0)("Utility1") = UtilityComboBox.SelectedItem.ToString
AECMeteringDataAdapter.Update(AECMeteringData)
AECSurveyForm.LoadMeterData()
End Sub

Call function outside user control

I need help to call a function in a usercontrol that is shown in a panel within the form, so far this is i tried, but no luck, i can't still get the text inputted on the textbox
Public Class Form1
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Dim ustudent As New StudentAdd
ustudent.Dock = DockStyle.Fill
SplitContainer1.Panel2.Controls.Add(ustudent)
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
ustudent.SaveData()
End If
End Sub
End Class
in user control have some textbox
textbox1 and textbox2
Public Class StudentAdd
Public Sub SaveData() As Boolean
'just testing whether it could work well
' getting textbox value
MsgBox(TextBox1.Text)
End Sub
End Class
But ustudent is a local var, try to declare it outside link_clicked event. Do you want to create multiple user controls in the win or just one?
For one you could add it at design time by dragging from the your project components panel
For more, you should implement some logic to identify the selected component and make it available for save data. If you want to save all just enumerate components in Panel2 (of type StudentAdd) and call the method

Passing variables between windows forms in VS 2010

I have two forms. Form 1 allows the user to pick an employee from a dropdown combo box. That employee is then passed onto Form 2 where the user enters additional information regarding that employee. That data will then be passed into a SQL table.
On form 1 I have:
Dim ChangeJobInfo As New Form2(Employee)
ChangeJobInfo.Show()
On Form 2 I have:
Public Sub New(ByVal Employee As String)
MsgBox(Employee)
End Sub
The variable passes just fine. The issue is that nothing shows up on the new form. When I setup Form2, I added a combobox, date picker, two text boxes, submit button, etc., but when the form loads it is completely blank. No errors, the MsgBox returns the right result, but none of my gui elements show up. If I change the code on form 1 to Form2.show() I see the form as laid out in the designer.
Any ideas on how to get those items to show up?
Change your code in Form2.vb for the New sub to this:
Public Sub New(ByVal Employee As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
MsgBox(Employee)
End Sub
If you don't call InitializeComponent(), your complete GUI is not going to render.
You don't even have to use the InitializeComponent or New functions.
I have made an example to show how easily this can be done.
Clicking "Show Form" results in the below:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form2.Show()
End Sub
which is simply used to display the second form.
By clicking "Pass Data" results in the following code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Form2.Label1.Text = TextBox1.Text
End Sub
As shown above you can pass the data directly from control to control. The same idea can be used with variables too.
I am late but I think this answer can help.
For example, Form1 named "menu" opens and passes variable to Form2 named "ordine".
The variable to pass is "hotel"
In menu on button_click
Dim ordinef As New Ordine()
If ordinef Is Nothing Then
'control that form is not opened yet if open close before
ordinef = New Ordine()
Else
ordinef.Close()
ordinef = New Ordine()
End If
ordinef.hotel = hotel
ordinef.Show()
In Form2 (Ordine):
Private Sub Ordine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
public hotel as string
msgbox hotel
That's it!!

Click a specific button on another form

Consider i am having two forms, form1 and form2
How can i click,mouse over(any events) a specific button on another form using coding in vb.net?
I'm assuming that Form1 launches Form2, since there's not a whole lot of information in the description.
When Form1 launches, there are two buttons: "button1" and "Launch Form 2" (forgot to change text on button1, sorry. :(
When you click "Launch Form 2", Form2 pops up:
Clicking the "button1" on Form1, a message box originating from Form1 pops up saying:
Clicking the "button1" on Form2, a message box ALSO originating from Form1 pops up saying:
Here's the code:
Form1
Public Class Form1
Private WithEvents frm2 As New Form2
Private Sub Form1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1Button.Click
RunSomeCode("Called from form 1!")
End Sub
Public Sub RunSomeCode(ByVal message As String)
MessageBox.Show(message)
End Sub
Private Sub Form1LaunchForm2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1LaunchForm2Button.Click
frm2.Activate()
frm2.Show()
End Sub
Private Sub frm2_SimulateForm1ButtonClick() Handles frm2.SimulateForm1ButtonClick
RunSomeCode("Called from form 2!")
End Sub
End Class
Form2
Public Class Form2
Public Event SimulateForm1ButtonClick()
Private Sub Form2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form2Button.Click
RaiseEvent SimulateForm1ButtonClick()
End Sub
End Class
How it works
Form 2 has a public event called "SimulateForm1ButtonClick". That event can be raised whenever you want, from any code block. I just decided to raise it when I click the button on the form.
Form 1 has an instance of Form2 WithEvents. It's very important that you use the WithEvents keyword, or that public event in Form2 won't show up. :(
Form 1 has a sub that handles the "SimulateForm1ButtonClick" that is raised when Form2 clicks its button.
Now, here's another important detail: The code executed when button1 is clicked on Form1 is actually in a private sub called RunSomeCode(). This is important, because it makes the code accessible from any other part of Form1, namely the part that handles Form2's event.
I hope that helps you out a little bit. I'm not sure exactly what you were asking. :/
Code: http://darin.hoover.fm/code/dl/FormsSandbox.zip
If you are trying to fire the event, just use Form2.Button1.PerformClick() assuming that the button on form 2 is called 'button1'.