VB.NET handling data between different forms - vb.net

I'm writing a simple application - address book. User enters new addresses and they are added as an entry to a list visible on the main form (frmStart). I use one form to add and edit (AddContForm). Add button on the frmStart works fine, however I experience some problems with the edit button as when I press it and enter new data they are added as new entry however the previous entry is still there. Logic is handled by Contact.vb class. Please let me know how to fix this problem. Here is the code:
Contact.vb
Public Class Contact
Public Contact As String
Public Title As String
Public Fname As String
Public Surname As String
Public Address As String
Private myCont As String
Public Property Cont()
Get
Return myCont
End Get
Set(ByVal value)
myCont = Value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.Cont
End Function
Public Sub Display()
Dim C As New Contact
C.Cont = frmAddCont.txtTitle.Text
C.Fname = frmAddCont.txtFName.Text
C.Surname = frmAddCont.txtSName.Text
C.Address = frmAddCont.txtAddress.Text
frmStart.lstContact.Items.Add(C)
End Sub
End Class
Here is frmStart.vb
Public Class frmStart
Public Button As String
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Button = ""
Button = "Add"
frmAddCont.ShowDialog()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
Button = ""
Button = "Del"
Dim DelCont As Contact
DelCont = Me.lstContact.SelectedItem()
lstContact.Items.Remove(DelCont)
End Sub
Private Sub lstContact_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContact.SelectedIndexChanged
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Button = ""
Button = "Edit"
Dim C As Contact
If lstContact.SelectedItem IsNot Nothing Then
C = DirectCast(lstContact.SelectedItem, Contact)
frmAddCont.ShowDialog()
End If
End Sub
End Class
Here is AddContFrm.vb
Public Class frmAddCont
Public Class ControlObject
Dim Title As String
Dim FName As String
Dim SName As String
Dim Address As String
Dim TelephoneNumber As Integer
Dim emailAddress As String
Dim Website As String
Dim Photograph As String
End Class
Private Sub btnConfirmAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmAdd.Click
Dim B As String
B = frmStart.Button
Dim C As New Contact
C.Display()
Me.Hide()
If B = "Edit" Then
C = DirectCast(frmStart.lstContact.SelectedItem, Contact)
frmStart.lstContact.SelectedItems.Remove(C)
End If
End Sub
Private Sub frmAddCont_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Update:
I really don't care much for whether the data on the AddForm is the old data or the new data. I don't understand how this code would help me achieve my goal. I think the problem now is that when I click edit previous entry on the list is not deleted (what I believe I have indicated in my question) - I tried to delete it (by using the if condition and checking what button was pressed) however it seems it is not working properly for some reason. I don't understand how your code could help me - please if possible explain it better.

If you're going to use the shared instance of frmAddCont, then it'll remember the data each time you show it, so you'd have to add some kind of Initialize method where you initialize all the controls to the defaults you want (for example empty textboxes).
However, the other way around it would be to do something like:
using(dlg as new frmAddCont)
{
If(dlg.ShowDialog() = DialogResult.OK)
{
Dim C As Contact = dlg.GetNewContact()
}
}
Please note, the above code is not tested and is just meant as a sample rather than the exact code you need.

Related

Passing data from one form to another form's textboxes

I am trying to pass the selected nodes from a treeview to another form that is displayed in the text boxes.
this is my code in TreeView1_NodeMouseDoubleClick event
the code works fine when the data from the selected treeview is shown in the same form, but the problem is when I want to pass it to the other form 4 nothing is shown and the debug shows me that if it receives the values ​​but they are not reflected, maybe my code to refer to the other form is wrong I would like a support with this case.
Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Dim nodeKey As String = TryCast(e.Node.Tag, String)
Dim form4 As New Form4
' Dim nodetext As String = TryCast(e.Node.Tag, String)
'Dim nodeKey As String = DirectCast(e.Node.Tag, String)
If nodeKey IsNot Nothing AndAlso nodeKey.StartsWith("DIST") Then
'You have double clicked a district node
Dim IDDISTRITO As Integer = Integer.Parse(nodeKey.Substring(4))
form4.lblco.Text = IDDISTRITO
'Do something with the district id here
'...
form4.txtdis.Text = e.Node.Text
form4.txtpro.Text = e.Node.Parent.Text
form4.txtdepa.Text = e.Node.Parent.Parent.Text
End If
End Sub
so I call form 3 where is the treeview
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim for3 As New Form3
for3.Show()
End Sub
As you assumed, yes you are referencing the forms incorrectly. Please follow this technique. In form3 class, declare a public variable Public Dim form4 As New Form4.
Then modify the form3 calling procedure like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim for3 As New Form3
for3.form4 = Me
for3.Show()
End Sub
Now modify your existing code to this :
Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Dim nodeKey As String = TryCast(e.Node.Tag, String)
' Dim nodetext As String = TryCast(e.Node.Tag, String)
'Dim nodeKey As String = DirectCast(e.Node.Tag, String)
If nodeKey IsNot Nothing AndAlso nodeKey.StartsWith("DIST") Then
'You have double clicked a district node
Dim IDDISTRITO As Integer = Integer.Parse(nodeKey.Substring(4))
form4.lblco.Text = IDDISTRITO
'Do something with the district id here
'...
form4.txtdis.Text = e.Node.Text
form4.txtpro.Text = e.Node.Parent.Text
form4.txtdepa.Text = e.Node.Parent.Parent.Text
End If
End Sub
I have not tested the above code, but I believe you have got the idea.
Well I managed to solve it, the simplest thing was complicated, I publish the solution.
form 4 by calling 3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim for3 As New Form3
AddOwnedForm(for3)
for3.ShowDialog()
End Sub
just add this code and voila
Dim form4 As Form4 = CType(Owner, Form4)

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

Update item from Dataset adaptor

I'm trying to update a row brought back from a dataset adaptor. I'm clearly doing something wrong but my googling/logic keeps failing me.
Heres what I have;
Public Class Popup
Inherits System.Web.UI.Page
Private dtJobs As Barry.joblistDataTable
Private taJobs As New BarryTableAdapters.joblistTableAdapter
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Type As String = Request.QueryString("t")
Select Case Type
Case "cancel"
CancelLocation(Request.QueryString("t"))
End Select
End Sub
Protected Sub CancelLocation(ByVal JobNo As String)
dtJobs = New Barry.joblistDataTable
dtJobs = taJobs.GetByJobNo(JobNo)
dtJobs.Item("Status") = "Cancelled"
taJobs.Update(dtJobs)
End Sub
End Class
The database field is called Status and I want to put the string of Cancelled in there

Why is my string variable in Audiobooks(secondaryform) not considered a member of ShoppingCart(mainform)

I instantiated a place in memory with the Dim MyCollection As New AudioBooks statement in the Public Class ShoppingCart Shopping cart form. Here is where I declared my variable in audiobooks Dim StrLearnCalclusInOneDayAB As String in the Public Class AudioBooks form.
If ListBox1.Items.Contains(Me.MyCollection.StrLearnCalclusInOneDayAB)
Here is my main syntax error, I need to look through listbox1 to see if the variable from audiobooks is there. I qualifying this wrong, and i know its so frustratingly simple.
Here is the full code. I need a VB tutor and will pay $5 per question below is the code in full.
Public Class ShoppingCart
Dim Tax As Decimal
Dim PB1 As Decimal
Dim AB1 As Decimal
Dim MyCollection As New AudioBooks
'My overall objective is to move the strings from audiobooks and printbooks to shopping cart and calculate the total
'I was instructed to use a module
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
Tax = 0.06
End Sub
Private Sub PrintBooksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintBooksToolStripMenuItem.Click
'Create an instance of the printbook
Dim frmPrintBook As New PrintBook
'Display the form in modal styple
frmPrintBook.ShowDialog()
End Sub
Private Sub AudioBooksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AudioBooksToolStripMenuItem.Click
'Create an instance of the audiobook
Dim frmAudioBook As New AudioBooks
'Display the form in modal style
frmAudioBook.ShowDialog()
End Sub
Public Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
'Dim MyCollection As New AudioBooks
'I want to run an if else statement which checks Listbox1 to see what books are in the cart and calculates the total
'I'm having trouble finding these variables(from audiobooks) with intelligsense, 'StrLearnCalclusInOneDayAB' etc.
'I'm pretty sure I need a new instance of memory from the audio bookcooks class to use the object variables
'declared in that class. I'm having trouble getting the syntax right. Intelligsense only will find the lstboxaudio
'The book says, form's class-level variables are accessible to statements in the form file,
'they are not accessible by default to statements outside the form file
'I want the code to look like this, only lstboxAudio comes up in Intelligsense, why is this, I want StrLearnCalclusInOneDayAB to
'Also I'm not sure I'm using contains correctly, hope I was clear
'I will eventually need to total the books in the cart, why Can't intellisense find the variable?
'This is my primary question
If ListBox1.Items.Contains(MyCollection.StrLearnCalclusInOneDayAB) Then
End If
Cart.IDidItYourWay = 11.95
Cart.TheHistoryofScotland = 14.5
Cart.LearnCalculusInOneDay = 29.95
Cart.FeelTheStress = 18.5
Cart.LearnCalculusInOneDayAB = 29.95
Cart.RelaxationTechniques = 11.5
Cart.TheScienceOfBodyLanguage = 12.95
Cart.TheHistoryOfScotlandAB = 14.5
'I feel I shouldn't have to reference an object to change properties in a class, why must I do that?
'This question might not make sense.
End Sub
Private Sub lblSubtotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblSubtotal.Click
End Sub
End Class
Public Class AudioBooks
Public Sub lstboxAudio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstboxAudio.SelectedIndexChanged
End Sub
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim AudioBooks1 As String
'AudioBooks1 = lstboxAudio.SelectedItem.ToString
'Save the selected item to a string
'ShoppingCart.ListBox1.Items.Add(AudioBooks1)
'shopping cart form, listbox1, items, add, that string of audiobooks
Dim StrLearnCalclusInOneDayAB As String
StrLearnCalclusInOneDayAB = lstboxAudio.Items(0).ToString
'If strLearnCalculusInOneDayAB is selected then add to the shopping cart listbox
If lstboxAudio.SelectedIndex = 0 Then
ShoppingCart.ListBox1.Items.Add(StrLearnCalclusInOneDayAB)
End If
' if the selectedindex of lslboxaudio is 0, the string is selected
'then move it to the shoppingcart
Dim StrTheHistoryOfScotlandAB As String
StrTheHistoryOfScotlandAB = lstboxAudio.Items(1).ToString
If lstboxAudio.SelectedIndex = 1 Then
ShoppingCart.ListBox1.Items.Add(StrTheHistoryOfScotlandAB)
End If
Dim StrTheScienceOfBodyLangAB As String
StrTheScienceOfBodyLangAB = lstboxAudio.Items(2).ToString
If lstboxAudio.SelectedIndex = 2 Then
ShoppingCart.ListBox1.Items.Add(StrTheScienceOfBodyLangAB)
End If
Dim StrRelaxationTechniquesAB As String
StrRelaxationTechniquesAB = lstboxAudio.Items(3).ToString
If lstboxAudio.SelectedIndex = 3 Then
ShoppingCart.ListBox1.Items.Add(StrRelaxationTechniquesAB)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
It relates to the Dim statement being classified private by default in the Audiobooks form. The must explicitly be declared public. Not knowing access levels and static shared variable dynamics creates this Question.

VB.NET editing existing that with a form

I have a simple questions that puzzles me. I need a little bit of refreashment with VB as I have been away for a while. I have a form that adds new contacts. New contacts are added by pressing an appropriate button and they appear as an entry in the list on the form. I try now to add an edit button that will edit existing entries. User will select a given entry on the list and press edit button and will be presented with an appropriate form (AddContFrm). Right now it simply adds another entry with the same title. Logic is handled in a class called Contact.vb Here is my code.
Public Class Contact
Public Contact As String
Public Title As String
Public Fname As String
Public Surname As String
Public Address As String
Private myCont As String
Public Property Cont()
Get
Return myCont
End Get
Set(ByVal value)
myCont = Value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.Cont
End Function
Sub NewContact()
FName = frmAddCont.txtFName.ToString
frmStart.lstContact.Items.Add(FName)
frmAddCont.Hide()
End Sub
Public Sub Display()
Dim C As New Contact
'C.Cont = InputBox("Enter a title for this contact.")
C.Cont = frmAddCont.txtTitle.Text
C.Fname = frmAddCont.txtFName.Text
C.Surname = frmAddCont.txtSName.Text
C.Address = frmAddCont.txtAddress.Text
'frmStart.lstContact.Items.Add(C.Cont.ToString)
frmStart.lstContact.Items.Add(C)
End Sub
End Class
AddContFrm
Public Class frmAddCont
Public Class ControlObject
Dim Title As String
Dim FName As String
Dim SName As String
Dim Address As String
Dim TelephoneNumber As Integer
Dim emailAddress As String
Dim Website As String
Dim Photograph As String
End Class
Private Sub btnConfirmAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmAdd.Click
Dim C As New Contact
C.Display()
Me.Hide()
End Sub
Private Sub frmAddCont_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
and frmStart.vb
Public Class frmStart
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
frmAddCont.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
Dim DelCont As Contact
DelCont = Me.lstContact.SelectedItem()
lstContact.Items.Remove(DelCont)
End Sub
Private Sub lstContact_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContact.SelectedIndexChanged
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Dim C As Contact
If lstContact.SelectedItem IsNot Nothing Then
C = DirectCast(lstContact.SelectedItem, Contact)
C.Display()
End If
End Sub
End Class
You haven't really added a question but looking at your code it's a bit weird.
If you click Add it will show frmAddCont and then in the confirm button of that form it'll save the data, but if you click Edit it won't show the form and will only add the same data again. I think you're missing a frmAddCont.Show() in your edit button handler.
However, all in all, you're mixing data with GUI too much. The Contact class should know nothing about frmAddCont, rather, the Add and Edit buttons in the main form should show frmAddCont as required (but I would do ShowDialog rather than Show to make it Modal) and if it's in edit mode I'd send in the Contact to be edited to frmAddCont and then when the user press confirm I'd amend/create the Contact as needed and if it's an Add I'd have a method that the main form could call to get out the new Contact.
I think it's fine for the GUI to know about your Contact class, but the Contact class should now know anything about the forms.