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
Related
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)
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
I need to write this small program in visual basic, but i face a problem which is i can't change the color or add list form text file during runtime.
this is picture of what i wont to do
http://i62.tinypic.com/30tghh0.png
And this is the code i wrote so far,,
Public Class Form1
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim listBox1 As New System.Windows.Forms.ListBox
listBox1.Text = "New List"
listBox1.Location = New Point(25, 25)
listBox1.Size = New Size(380, 280)
Me.Controls.Add(listBox1)
OpenToolStripMenuItem.Enabled = True
SaveToolStripMenuItem.Enabled = True
SaveAsToolStripMenuItem.Enabled = True
CloseToolStripMenuItem.Enabled = True
EditToolStripMenuItem.Enabled = True
End Sub
Private Sub ExirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExirToolStripMenuItem.Click
End
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Dim myfile As String = OpenFileDialog1.FileName
Dim allLines As String() = File.ReadAllLines(myfile)
For Each line As String In allLines
ListBox1.Items.Add(line)
Next
End If
End Sub
End Class
The problem as you see is in OpenToolStripMenuItem sub with line ListBox1.Items.Add(line)
is there is no listbox1 because is not created yet.the same with color, save and the rest.
so, please help me to solve it.
Move the declaration of ListBox1 out to the Class level:
Public Class Form1
Private ListBox1 As System.Windows.Forms.ListBox
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
ListBox1 = New System.Windows.Forms.ListBox
...
End Sub
...
End Class
*But why create it at run-time like this? You could add it at design-time and set the Visible() property to False. When the New button is clicked, you change Visible() to True. Unless you need a new ListBox to be created each time so you have more than one? If so, how will they be laid out?...
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.
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.