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

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.

Related

How do i use an array to represent a form, to show and hide forms in VB

I want to use a array to help me switch between forms in a quiz that i have to create for a school assignment. When a question is correct it shows the correct form. I am using one button to go to the next form. There are 20 questions each with its own form. This is what i need:
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
Me.Hide()
arrayforms(count).Show()
Thanks
First Collect all of your questionary forms into Array. Name your questionary forms as "Questionary1", "Questionary2" something like that, or set tag to you forms. Then find your form by index and create an instance and ShowDialog, that's it. Try following code.
Public Class Form1
Private forms(20) As Type
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim yourFormIndex = 3
Dim frm As Form = Activator.CreateInstance(forms(yourFormIndex))
frm.ShowDialog()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
Dim index As Integer = 1
For Each myType As Object In types
If myType.BaseType.FullName.ToString.ToUpper = "SYSTEM.WINDOWS.FORMS.FORM" Then
If (myType.Name.ToString.StartsWith("Questionary")) Then
forms(index) = myType
index = index + 1
End If
End If
Next
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

Adding items from textbox to a list(of string)

I`m new to vb.net. Looking to find out how to add items into the list. At the moment,its only adding one item. I need it to save many items and must be able to display all items in another textbox. Please help!
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim patients As List(Of String) = New List(Of String)
patients.Add(TextBox1.Text)
TextBox2.Text = patients.Count
End Sub
End Class
Every time you click the button a new copy of the list variable is created and, of course, it is initially empty. You add one item but that's the end of the game.
If you want to preserve the contents of the list, you need to move the List variable at the global class scope
Public Class Form1
Dim patients As List(Of String) = New List(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
patients.Add(TextBox1.Text)
TextBox2.Text = patients.Count
End Sub
.....
End Class
You need to declare and instantiate your list outside of Button Click:
Public Class Form1
Dim patients As New List(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
patients.Add(TextBox1.Text)
TextBox2.Text = patients.Count
End Sub
End Class

TextBox1 doesn't show the listbox1 value

I'm Visual Basic beginner, yesterday i wrote a dictionary that give you the opposite of the entered word, so i designed the form to look like this
[url]http://img651.imageshack.us/img651/6115/errorbp.jpg[url]
by the way i made a two list boxes as databases so the code will compare if the textbox1.text = listbox1.text then it will command textbox2 to append the value of the listbox : textbox2.appendtext(listbox2.text) but nothing happens
my code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TnsBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = TextBox3.ToString Then
TextBox2.AppendText(ListBox2.Text)
ElseIf TextBox1.Text = TextBox4.Text Then
TextBox2.AppendText(ListBox1.ToString)
End If
End Sub
Private Sub AddBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox3.Text)
ListBox2.Items.Add(TextBox4.Text)
End Sub
End Class
the point of the code is ok cuz yesterday i finished the coding and the programs works fine but i forget to save it so i coded again and every thing above happens
this is the yesterday program
http://www.mediafire.com/?tavne7xjyth7y7v
virustotal link:
https://www.virustotal.com/file/1d39429ae1498a744e1556188b7e8914526b7e2fbb2d4904c2b4ea22fb278dc7/analysis/1346676641/
Initially you are setting the textbox text to "ListBox" without choosing anything specific so it is calling ToString() on the listbox which is why you get that.
I would change the method so that you have a Dictionary variable like so:
Public Sub Translate(input As String)
TextBox2.Text = OppositeDictionaires(input)
End Sub
Public OppositeDictionary As New Dictionary(Of String, String)
'Call as Add(TextBox3.Text, TextBox4.Text)
Public Sub Add(input As String, opposite As String)
OppositeDictionary.Add(input, opposite)
End Sub
Call add from your event and then Translate from your translate event. You should then get your output as intended, still add them to the listboxes if you want to display to the user but handle the translation in the code behind through a dictionairy object.
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

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.