How can I make the data open in the same form - vb.net

i want to transfer values from one form to another...
This is the code for the MainPage
Public Property StringPass As String
Public Property NumPass As String
Private Sub MainPage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label7.Text = StringPass
TextBox3.Text = NumPass
End Sub
This is the code for the SecondPage
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OBJ As New MainPage
Dim NUM As New MainPage
OBJ.StringPass = TextBox1.Text
NUM.NumPass = TextBox6.Text
OBJ.Show()
NUM.Show()
Me.Hide()
End Sub
How can OBJ and NUM pass values to their respective textboxes but open in the same MainPage?

You are creating 2 forms and you only need one.
If you want to pass data to the next form you can assign those values just like you would with a normal class.
Make sure you have saved all forms as well.
Your naming convention is not the best here just FYI.
Here is an example with your code.
This is the code for the SecondPage
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OBJ As New MainPage
OBJ.Label7.Text = TextBox1.Text
OBJ.TextBox3.Text = TextBox6.Text
OBJ.Show()
Me.Hide()
End Sub

Related

Get values of one form into another form in Visual Basic

I am trying to get an integer value of one form (Form1) inside another form (Form2). I have tried to access it via the below code but not getting it. Can someone please tell me what I am doing wrong.
Public Class Form1
Public Points As Integer = 100
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Points
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FinalPoints As New Form1
Label1.Text = FinalPoints.Label1.Text
End Sub
End Class
The problem: As you are showing your Form2 (Form2_Load is executed), a new Form1 is created. This newly created Form1 has NOT executed the Form1_Load function yet!
You would need to show the newly created FinalPoints (Form1) with FinalPoints.Show() like that:
Dim FinalPoints As New Form1
FinalPoints.Show()
Label1.Text = FinalPoints.Label1.Text
to let the Form1_Load function execute, which is then setting your FinalPoints.Label1.Text. But that would just opens a new Form1.
Also you can just get the public Points variable inside the Form2_Load like that (you also do not have to create a new Form1):
Label1.Text = Form1.Points
Alternatively: Just use a public variable inside Form2 and assign your value to it, before you show the form.
Public Class Form1
Public Points As Integer = 100
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Points.ToString
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FinalPoints As New Form2
FinalPoints.StringFromForm1 = Label1.Text
FinalPoints.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Public Property StringFromForm1 As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = StringFromForm1
End Sub
End Class
This line: Dim FinalPoints As New Form1 creates a new instance of Form1, but what you want is to refer to an existing instance of Form1. There are different techniques you can try. For example, overload the Show method of Form2.
Something like this:
Form1: pass the value to Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2
Me.Hide()
f.Show(Points)
End Sub
Form2: fetch the value from caller (Form1)
Public Class Form2
Public Overloads Sub Show(ByVal Points As Integer)
Me.Label1.Text = Points.ToString
MyBase.Show()
End Sub
End Class

VB.net BackgroundWorker UI

I am trying to create an app that produces an excel document based on user input. The document can become quite extensive (multiple sheets), so I want to put the creation of the document on a BackgroundWorker and pass the progress to a ProgressBar.
I'm having problems accessing UI controls while creating the document.
How can I access multiple UI controls from a BackgroundWorker?
This is a very basic example of two of the many things that i want to do in the BackgroundWorker:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
OpenWorkbook()
Dim Combobox() As ComboBox = {ComboBox1, ComboBox2, ComboBox3, ComboBox4}
With xlWorkBook.Sheets("Cover1")
.Range("E5").Value = TextBox1.Text
.Range("E6").Value = TextBox2.Text
.Range("E7").Value = TextBox3.Text
.Range("E8").Value = TextBox4.Text
For i = 0 To Combobox.Count - 1
.Range("A" & i + 1).Value = Combobox(i).Text
Next
End With
CloseWorkBook()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
I'm not experienced at all and after researching on this subject, I'm still not getting along with the Delegate/Invoke. Any help would be awesome. Thanks in advance.
As pointed out by Heinzi you cant just pass reference to a control into a bgw. instead you pass the values.
Option Strict On
Public Class FormControls
WithEvents Bgw As New ComponentModel.BackgroundWorker With {.WorkerReportsProgress = True}
Public Class BgwArgs
Public TxtBx1Txt As String = FormControls.TextBox1.Text
Public TxtBx2Txt As String = FormControls.TextBox2.Text
Public CmbBx1Txt As String = FormControls.ComboBox1.SelectedText
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim BgwArgs As New BgwArgs
Bgw.RunWorkerAsync(BgwArgs)
End Sub
Private Sub Bgw_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles Bgw.DoWork
Dim BgwArgs As BgwArgs = DirectCast(e.Argument, BgwArgs)
.Range("E5").Value = BgwArgs.TxtBx1Txt 'etc
End Sub
End Class
I assume the OpenWorkbook is a sub that uses interop to create xlWorkBook where you would instead want to do that within the thread also, same with CloseWorkbook all that should be handled in the DoWork event and not in their own separate routines.

VB.net WindowsForm changing then [System.Data.MissingPrimaryKeyException]

-I created 2 forms after I use hide() and show() to switch between form.
-In the 2nd form, I create datatable (bond to datagrid) and primary key for datatable to use find().
-After I press [BACK] button form 2nd form to 1st form (use hide() and show() function) and come back to use 2nd form application again. the runtime error show this [System.Data.Missing.PrimaryKeyException: Table doesn't have a primary key]
1st Form
Public Class MainMenu
Public MainForm As MainMenu
Public AssetCheckForm As AssetCheck
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AssetCheckForm.Show()
MainForm.Hide()
End Sub
Public Sub MainMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MainForm = New MainMenu()
AssetCheckForm = New AssetCheck()
End Sub End Class
2nd Form
Dim dtAsset As New DataTable("AssetTable")
Public Sub readData()
If readStatus = 0 Then
Try
Dim splits As String()
Using sr As StreamReader = New StreamReader(inputcsvname)
'read the first line for the table columns
splits = sr.ReadLine.Split(","c)
For i As Integer = 0 To UBound(splits)
dtAsset.Columns.Add(splits(i))
Next
'read the rest of the lines to add rows
Do While Not sr.EndOfStream
splits = sr.ReadLine.Split(","c)
dtAsset.Rows.Add(splits)
Loop
End Using
Catch ex As Exception
Finally
End Try
dtAsset_display = dtAsset.Copy()
totalcount.Text = getRowsCount(dtAsset_display)
dtAsset.Columns("AsstCode").Unique = True
dtAsset.PrimaryKey = New DataColumn() {dtAsset.Columns("AsstCode")}
'bind display part to DataGrid
DataGrid1.DataSource = dtAsset
Private Sub BackButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonF4.Click
clearData()
MainMenu.MainForm.Show()
MainMenu.AssetCheckForm.Hide()
End Sub
I think I missed something about VB.net WinForm concept.
Does anyone know what this error means?
The fact that you are calling MainForm = New MainMenu() in MainForm it means you now have two instances of MainForm.
So then when you call MainMenu.MainForm.Show() you are showing your new instance, not the existing one.
You need to change this around so that you store the reference to the existing MainForm in your AssetCheckForm form.
Something like this:
Public Class MainMenu
Private AssetCheckForm As AssetCheckForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.AssetCheckForm = New AssetCheckForm
Me.AssetCheckForm.MainMenu = Me
Me.Hide()
Me.AssetCheckForm.Show()
End Sub
End Class
Public Class AssetCheckForm
Friend MainMenu As MainMenu
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.MainMenu.Show()
Me.Close()
End Sub
End Class

Visual Basic 2012: Passing variable from one form to another

I have two forms, the main (Main.vb) program window and a pop-up that appears when the program is started (getInitialBalance.vb). I need to get a value entered into the PopUp window from the popup window to the Main program. The relevant code is shown below:
getinitialbalance.vb
Public Class GetInitialBalance
Public initialBalance As Integer
Private Sub btnApplyInitialBal_Click(sender As Object, e As EventArgs) Handles btnApplyInitialBal.Click
Dim textinput As Integer = txtInitialBalance.Text
initialBalance = textinput
Me.Close()
End Sub
End Class
Main.vb
Public Class Main
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GetInitialBalance.ShowDialog()
End Sub
Dim localInitialBalance As Integer = GetInitialBalance.initialBalance
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(localInitialBalance)
End Sub
End Class
New up the GetInitialBalance form and then when the user clicks OK on the popup dialog, grab the value initialBalance from the reference to GetInitialBalance, like this:
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim popup As New GetInitialBalance
If popup.ShowDialog = Windows.Forms.DialogResult.OK Then
localInitialBalance = popup.initialBalance
End If
End Sub
Your entire code should look like this:
Public Class GetInitialBalance
Public initialBalance As Integer
Private Sub btnApplyInitialBal_Click(sender As Object, e As EventArgs) Handles btnApplyInitialBal.Click
initialBalance = Convert.ToInt32(textinput)
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
End Class
Public Class Main
Dim localInitialBalance As Integer
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim popup As New GetInitialBalance
If popup.ShowDialog = Windows.Forms.DialogResult.OK Then
localInitialBalance = popup.initialBalance
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(localInitialBalance)
End Sub
End Class

vb.net textbox not displaing value

In my frmMain class I have a textbox(txtCustomer) which populates from a database. I want to pass this value to another textbox in frmDepartment(txtDeptCustomer).
I am failing to see the logic of why the code I am using is not displaying a value in txtDeptCustomer. I can query the database ok with the variable, so the string is being passed through, but just not displaying in txtDeptCustomer. I would be grateful if someone could point out my error. Thanks
frmDepartment
Dim customer As Object = frmMain.txtCustomer.Text
This is passing correct value to db.
sql = "SELECT * FROM Departments where Customer = '" & CType(customer, String) & "'"
textbox txtDeptCustomer <--- NOT DISPLAYING VALUE
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
txtDeptCustomer.Text = CType(customer, String)
End Sub
Public Customer as String = Nothing
Private Sub btnDO_Click(sender As Object, e As EventArgs) Handles btnDoWork.Click
Customer = Database Call
Dim frmDepartmentInstance as new frmDepartment
frmDepartment.ShowDialog(Me)
End Sub
Then in the Load event of frmDepartment you can say
txtDeptCustomer.Text = frmMain.Customer
Proof of concept: New Project. Two forms | Form 1 has a button and a textbox | Form2 just has textbox
Public Class Form1
Public Test As String = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Test = TextBox1.Text
Dim frm2 As New Form2
frm2.ShowDialog(Me)
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = Form1.Test
End Sub
End Class
You must declare then as public your customer variable in frmDepartment, like:
Public customer as String
And in the button click from your frmMain you pass the value like:
frmDepartment.customer = txtCustomer.Text
frmDepartment.Show()
And then in Loading your frmDepartment you have now the option of assigning customer to txtDeptCustomer like:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDepartment.Text = customer
End Sub