-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
Related
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
im currently made project with VB VS2012
i have 2 form,
form1 contains textbox and button, this button is to open form2.
in form2, i have 1 button. i want to set form1.textbot value when form2 button is clicked then close the form2.
form1 button to call form2
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog(Me)
End Sub
this is button form2
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Dim x As New form1
x.textbox.Text = "tes"
End Sub
i've tried ctype, directcast but its not working. by the way, this form1 is docked to a mainform. i tried to do the same but without form docked, it works, but when it docked, its not working.
Try if you are using default instances.
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Form1.textbox.Text = "tes"
Close()
End Sub
Did you really name the text box textbox? Descriptive names can be helpful.
What you're currently doing is creating a new instance of form1 with the line Dim x As New form1 - and that means you're not updating the original form1.
Since you are passing a reference to form1 to the Form2 dialog already, i.e. .ShowDialog(Me) then this would work:
'form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog(Me)
End Sub
'Form2
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
CType(Me.Owner, form1).textbox.Text = "tes"
End Sub
i've found my solution
here's code on form 1
Private t1 As String = String.Empty
Private t2As String = String.Empty
Dim f As New form1()
If f.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
t1 = f.t1.ToString
t2 = f.t2.ToString
textbox.Text = t1
textbox2.Text = t2
f.Close()
End If
and here's on form2
Public t1 As String
Public t2 As String
Me.DialogResult = Windows.Forms.DialogResult.OK
If Me.DialogResult = Windows.Forms.DialogResult.OK Then
t1 = dgdriver.SelectedRows(0).Cells(1).Value.ToString
t2 = dgdriver.SelectedRows(0).Cells(0).Value.ToString
Me.Close()
End If
I have a webform for entering Invoice info to a DB.
I have 3 textboxes requesting product quantity, product and price.
I decided to create a DataTable to store temporarily quantity, productId, price so that the user might add N products for later saving the data on the DataTable (displayed on a grid) to the DB.
I don't know what am I doing wrong but am unable to store a second product. After I save 1 row to the DataTable the second one is not added but overwrites the first one. I know this has to be something really stupid on my part but haven´t been able to figure out what am I doing wrong and am sort of a beginner.
Public Class IngresoFacturas
Inherits System.Web.UI.Page
Dim miDataTable = New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
miDataTable.Columns.Add("dCantidad", GetType(System.Int32))
miDataTable.Columns.Add("idProducto", GetType(System.Int32))
miDataTable.Columns.Add("mTotal", GetType(System.Decimal))
End Sub
Protected Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
miDataTable.Rows.Add(RadNumericTextBox2.Text, Int32.Parse(RadAutoCompleteBox2.Entries.Item(0).Value), RadNumericTextBox3.Text)
RadGrid1.Rebind() 'Forces rebind to update Grid
RadAutoCompleteBox2.Entries.Clear()
RadNumericTextBox2.Text = ""
RadNumericTextBox3.Text = ""
End Sub
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
RadGrid1.DataSource = miDataTable
End Sub
End Class
Tried Andrew´s solution but it didn{t work so I came up to this:
Anything you guys could warn me about using ViewState to solve this. It is working.... but don{t know if it{s the best approach
Public Class IngresoFacturas
Inherits System.Web.UI.Page
Dim miDataTable As DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
miDataTable = New DataTable
miDataTable.Columns.Add("dCantidad", GetType(System.Int32))
miDataTable.Columns.Add("idProducto", GetType(System.Int32))
miDataTable.Columns.Add("mTotal", GetType(System.Decimal))
ViewState("tabla") = miDataTable
End If
End Sub
Protected Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
miDataTable = ViewState("tabla")
miDataTable.Rows.Add(RadNumericTextBox2.Text, Int32.Parse(RadAutoCompleteBox2.Entries.Item(0).Value), RadNumericTextBox3.Text)
RadGrid1.Rebind() 'Forces rebind to update Grid
ViewState("tabla") = miDataTable
RadAutoCompleteBox2.Entries.Clear()
RadNumericTextBox2.Text = ""
RadNumericTextBox3.Text = ""
End Sub
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
RadGrid1.DataSource = miDataTable
End Sub
End Class
Something like this might help:
Dim miDataTable As DataTable
Sub PrepareDT()
miDataTable = New DataTable
miDataTable.Columns.Add("dCantidad", GetType(System.Int32))
miDataTable.Columns.Add("idProducto", GetType(System.Int32))
miDataTable.Columns.Add("mTotal", GetType(System.Decimal))
End Sub
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not Page.IsPostBack Then
PrepareDT()
End If
End Sub
(and remove the code from the Page_Load method) so that you only "reset" miDataTable on the first access of the page.
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
I have the problem to retreive the string from one form to another. here is my code: What's wrong with this?
Public Class Form3
Dim unit As String
Public itmname As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj1 As New Form4
itmname = tb1.Text
MessageBox.Show(itmname)
obj1.Label1.Text = itmname
obj1.Show()
End Sub
End Class
Public Class Form4
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Form3
MessageBox.Show("item name:" + .itmname)
Label1.Text = .itmname
End With
End Sub
End Class
You shouldn't have to do any special code in Form4 to set the value. you already set the textbox value from from3's button click event. Setting again is just overwriting it with a blank value from a newly instantiated form. Just clear all the code you have listed in Form4's load event and it should work.