Passing variables between windows forms in VS 2010 - vb.net

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!!

Related

When a ComboBox value is selected, output particular text (Visual Basic)

I'm creating a decision interface for clinical support with numerous amounts of combo boxes with boolean values 'Yes|No". However I want it so if the user choses either yes or no, a button can be clicked at the bottom and then another windows form appears and says whether they have cancer or not.
For example, if the user clicks 'yes' in the comboBox and then clicks the 'submit' button, another form will appear and the text box will say whether they have cancer or not. Would anyone be able to supply an example of how this would work? I have the form with the combo boxes, a button that links to another form, and a textbox inside the second form....but I can't get the information between the two forms.
The code I have at the moment is
Private Sub submit_Click(sender As Object, e As EventArgs) Handles submit.Click
If (RectalBleeding.SelectedItem = "Yes") Then Outcome.OutcomeBox.Text = "you have cancer" End If Outcome.Show()
End Sub
Particular combo box im trying to link is called 'RectalBleeding'. The button on the decision interface form is called 'submit' the second form is called 'outcome'. The box inside outcome is called 'outcomeBox' I want it so that if 'Yes' was chosen in the comboBox, the user clicks 'submit' second form appears and in the text box it says "you have cancer"
Thanks!
You can use public properties and constructors to pass values between two forms. Below is some code that uses these tools for your purpose.
Here is the code for the main form:
Public Class Form1
Private frm As Outcome
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddDecision()
End Sub
Private Sub AddDecision()
RectalBleeding.Items.Add("Yes")
RectalBleeding.Items.Add("No")
End Sub
Private Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
If RectalBleeding.Text = "Yes" Then
frm = New Outcome("You have cancer")
Else
frm = New Outcome("You don't have cancer")
End If
frm.Show()
End Sub
End Class
and here for the outcome form
Public Class Outcome
Private _Decision As String
Public Property Decision() As String
Get
Return _Decision
End Get
Set(ByVal value As String)
_Decision = value
End Set
End Property
Private Sub Outcome_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillTextBox()
End Sub
Public Sub New(ByVal results As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Decision = results
End Sub
Private Sub FillTextBox()
outcomeBox.Text = Me.Decision
End Sub
End Class

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

How to pass text from one form to opened form at back?

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

combobox not being populated

I have a windows form project with a main form. There is a textbox leave event that opens a new form. In that new forms load event i have a combobox item loop that populates the combobox items. It works perfectly fine if run on the main form but doesnt work on the second form. Why doesn't the comboboxes on the secondary form populate when that form is opened via a textbox_leave event from the main form?
this is the leave event
Private Sub tbChartTitle_Leave(sender As Object, e As System.EventArgs) Handles tbChartTitle.Leave
If Not tbChartTitle.Text = Nothing Then
frmTitleAttributes.Show()
End If
End Sub
This is the code that populates one of the comboboxes on the second form (it works if run on a combobox on the main form)
Private Sub frmTitleAttributes_Load(sender As Object, e As System.EventArgs) Handles Me.Load
InitializeComponent()
AddFonts()
End Sub
Private Sub AddFonts()
' Get the installed fonts collection.
Dim allFonts As New Drawing.Text.InstalledFontCollection
' Get an array of the system's font familiies.
Dim fontFamilies() As FontFamily = allFonts.Families
' Display the font families.
For i As Integer = 0 To fontFamilies.Length - 1
cbxTitleFonts.Items.Add(fontFamilies(i).Name)
Next
End Sub
make sure that the Load handler is hit after you show your form (use break point)
also you can try to call it in the Shown event
Private Sub frmTitleAttributes_Shown(sender as Object, e as EventArgs) _
Handles frmTitleAttributes.Shown
AddFonts()
End Sub