Label won't update from separate form - vb.net

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

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.

Passing value from one form to another using events in vb.net

I'm trying to pass a value from one form, let's call it Form1 to another form, Form2. When i double click a row in a listview that is in Form2, the value should be passed to a combobox in Form1.
I could get one of my other forms to do this using an event called PropertyChanged, but I can't seem to get it to work on other forms. I don't know if it is the fact that you can only have 1 event in the entire project and not have another with the same name. I'm missing something, but i just don't know where.
This is the code i used in Form2:
Public Event PropertyChanged As Action(Of Object)
Private Sub ListView2_DoubleClick(sender As Object, e as EventArgs) Handles ListView2.DoubleClick
RaiseEvent PropertyChanged(ListView1.SelectedItems(0).SubItems(0).Text)
End Sub
And this is the code I used in Form1:
Dim WithEvents f2 As Form2
Private Sub PropertyChanged(obj As Object) Handles f2.PropertyChanged
cmb_form1.Text = obj
End Sub
There are several ways to implement this. This one is quick and dirty.
It consists of writing up a property in Form2 which is public. When the user makes a choice, it goes in the property. Then Form1 reads the property before it disposes Form2.
Here's a little code snippet to better illustrate what I mean:
Public Class Form1
' This form has a button which opens a Form2 instance
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form2 As New Form2 ' instantiate Form2
form2.ShowDialog() ' the user chooses a value
MessageBox.Show(form2.Result) ' get it before it's out of scope!
End Sub
End Class
Public Class Form2
Public Property Result As String ' the value is stored in there, it can be any type
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Result = "Clicked!" ' store the value you want to share between forms
Me.Close()
End Sub
End Class
Have fun!

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

Using Variables Across Forms - VB

I've got a fairly simple question (I think) on passing variables between forms using Visual Basic.
I've got a program with 2 forms (Form1 and Form2). Form1 has 3 radio buttons, which the user has to select one of and then loads Form2.
Now I've made it so that if radiobutton1 is picked, the Public Variable "radio_select" will equal "radiobutton1", if radiobutton2 is picked, "radio_select" will equal "radiobutton2".
But whenever I try call "radio_select" in my second form, it comes up blank. Why could this be? And how can I fix it.
I've tried using if form1.radiobutton1.checked = true but I keep getting the first radiobutton, regardless of the radio button I've selected.
I think the form is being unloaded, or there is an issue somewhere there, as it appears none of the variables get passed to the second form, once it has been initialized. Also note, the first form is hidden Me.Hide() when the second form is called.
Have you considered a slight re-design whereby you create a property on Form2 called RadioSelect and then set this from Form1 before showing Form2:
Class Form2
Public Property RadioSelect As String
...
End Class
...
Dim f2 as new Form2()
f2.RadioSelect = "radiobutton2"
f2.Show() ' Or f2.ShowDialog()
This gets you away from an unnecessary public variable and should also ensure Form2 can see what it needs from Form1, or whoever calls it.
Edit:
The following works for me:
Public Class Form1
Public Test As String
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Test = "I'm Here"
Me.Hide()
Form2.ShowDialog()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Text = Form1.Test
End Sub
End Class

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