vb.net Entering and retrieving data from a Array [duplicate] - vb.net

This question already has answers here:
Building a multidimensional array in vb.net
(5 answers)
Closed 9 years ago.
Below is my code to enter data into an array and retrieve the information
Entering the data seems to have no problems but when I click the button to retrieving the information it just presents 0's in the text box.
Public Class Form1
Dim Array(20) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim counter As Integer
For counter = 0 To ListBox1.Items.Count - 1
Array(counter) = txtEnterMarks.Text(counter)
Next
Label1.Text = "omfg"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For counter = 0 To Array.Length - 1
ListBox1.Items.Add(Array(counter))
Next
End Sub
End Class

Again .. try this different answer
Structure Student
Dim Name As String
Dim Mark As Integer
End Structure
To save your input in it :
Dim StudentsInfo as New List(Of Student)
Dim si As New Student
si.Name = txtEnterName.Text
si.mark = val(txtEnterMark.Text)
StudentsInfo.Add(si)
To get value from StudentsInfo
txtEnterName.Text = StudentsInfo(0).Name
txtEnterMark.Text = StudentsInfo(0).mark.ToString
About preserve it To ListView .. Again .. Try Mr. Google ...

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

How to cycle through array one at a time with button [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
This is my code so far. My professor said I can not hard code. Can you please help me with the loop under the button click event. Right the flash form will load and pull up the first term and definition in the text files but when you click the next button it shows the path of the file that has been read in.
Public Class frmFlash
Private _intSizeOfArray As Integer = 3
Private _strLocationOfTerms(_intSizeOfArray) As String
Private _strLocationOfDefinitions(_intSizeOfArray) As String
Private _objReader As IO.StreamReader
Private _strTerms As String
Private Sub frmFlash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objReader As IO.StreamReader
Dim strLocationOfTerms As String = "C:\Users\boone\Desktop\Terms.txt"
Dim strLocationOfDefinitions As String = "C:\Users\boone\Desktop\Definitions.txt"
Dim intTermArray(4) As Integer
Dim intCount As Integer
lblDefinition.Visible = False
If IO.File.Exists(strLocationOfTerms) Then
objReader = IO.File.OpenText(strLocationOfTerms)
_strTerms = objReader.ReadLine()
lblTerm.Text = _strTerms
End If
If IO.File.Exists(strLocationOfDefinitions) Then
objReader = IO.File.OpenText(strLocationOfDefinitions)
strLocationOfDefinitions = objReader.ReadLine()
objReader.Close()
lblDefinition.Text = strLocationOfDefinitions
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim objReader(_intSizeOfArray) As IO.StreamReader
Dim intCount As Integer = 0
Dim strTermArray As String
Dim strLocationOfTerms As String = "C:\Users\boone\Desktop\Terms.txt"
Dim strLocationOfDefinitions As String = "C:\Users\boone\Desktop\Definitions.txt"
Do While intCount <= _intSizeOfArray
lblTerm.Text = strLocationOfTerms(_strTerms)
lblDefinition.Text = strLocationOfDefinitions
intCount += 1
Loop
End Sub
Private Sub btnDefinition_Click(sender As Object, e As EventArgs) Handles btnDefinition.Click
lblDefinition.Visible = True
End Sub
End Class
Since you are working with arrays, the easiest way to do this is to skip the processing in the button handler entirely. Load up your arrays in the Load event and just grab an IEnumerator for them.
'In Class level declarations
Private _termsEnumerator As IEnumerator(Of String)
Private _defsEnumerator As IEnumerator(Of String)
Private Sub frmFlash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Your other initialization code...
'...
'Load your _strLocationOfTerms and _strLocationOfTerms here.
_termsEnumerator = _strLocationOfTerms.GetEnumerator()
_defsEnumerator = _strLocationOfDefinitions.GetEnumerator()
End Sub
Then it's just as simple as grabbing the next item from the arrays (they implement IEnumerable):
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If _termsEnumerator.MoveNext() And _defsEnumerator.MoveNext() Then
lblTerm.Text = _termsEnumerator.Current
lblDefinition.Text = _defsEnumerator.Current
End If
End Sub

How to print certain indexes based off of certain check boxes being checked

Below I have an array and in my design I have a check list box with 10 options. For example, if boxes 1 and 2 were checked, I would only want to print Indexes 0 and 1 ONLY. I have a button that prints all of the array members (included below) and that is what I want to make print only selected items. I have tried using a switch but that file had gotten corrupted and I am lost. Thank you. (Language is VB)
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn1.Click
Dim strDecimal(9) As String
strDecimal(0) = FormatPercent(0.0146175)
strDecimal(1) = FormatPercent(0.0345324585)
strDecimal(2) = FormatPercent(0.09324543575)
strDecimal(3) = FormatPercent(0.07346475)
strDecimal(4) = FormatPercent(0.0772346615)
strDecimal(5) = FormatPercent(0.42234234654)
strDecimal(6) = FormatPercent(0.6246264664)
strDecimal(7) = FormatPercent(0.4524642234)
strDecimal(8) = FormatPercent(0.6876543534)
strDecimal(9) = FormatPercent(0.6876543534)
For num As Integer = 0 To strDecimal.Length - 1
listArrays.Items.Add(strDecimal(num))
Next
End Sub
Private Sub clearList()
listArrays.Items.Clear()
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
clearList()
End Sub
Assuming you're using a CheckedListBox and want to know which items are Checked:
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
For Each itm As String In listArrays.CheckedItems
Debug.Print(itm)
Next
End Sub

'While Loop' and multiples of a variable

This is another school project, and I'm stumped, so PLEASE help.
I'm supposed to write a program that outputs the numbers 1 to 100, however there are 4 variables. When you enter a value for Variable 1 it takes the numbers that multiples of that number and changes it to the Variable Word that you type in.
The specific question I have is how do I get my variables to be multiples of that value?
This is all I have...thank you for your help.
Public Class Form1
Private Sub lblDiscription_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblDiscription.Click
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub bntCounter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntCounter.Click
Dim intCounter As Integer = 0
Dim intLeftMultiple As Integer = 1
Dim intRightMultiple As Integer = 1
Dim strLeftWord As String
Dim strRightWord As String
While intCounter < 101
lstDisplay.Items.Add(CStr(intCounter))
intCounter += 1
End If
End While
End Sub
Private Sub lblMiddle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblMiddle.Click
End Sub
End Class
This is what my GUI Looks like.

visual basic List.box question

i have 1 text box and 1 listbox in my VB form.
i want to check duplicate item,compare with textbox.text1 and listbox.list item.
and if textbox.text1 value is '3333' and listbox.list multiple value is '1111' '2222' '3333' '4444'
so how to implement such like duplicate check routine?
so if duplicate detect compare with current text1 value and compare with one of listbox's
value is if detect,want to popup messagebox
thanks in advance
Assuming you are inserting strings into your ListBox you can do this:
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim x As String
For Each x In ListBox1.Items
If (x = TextBox1.Text) Then
MessageBox.Show("Error")
Return
End If
Next
ListBox1.Items.Add(TextBox1.Text)
End Sub
If it's another type of object that has a property called Value then you need a small change:
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim x As Foo
For Each x In ListBox1.Items
If (x.Value = TextBox1.Text) Then
MessageBox.Show("Error")
Return
End If
Next
ListBox1.Items.Add(TextBox1.Text)
End Sub
Assuming that the ListBox contains strings, you can use the Contains method of the Items collection to check for matches. Example (make a form with a ListBox called '_theListBox', a TextBox called '_theTextBox' and a Label called '_theLabel'):
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_theListBox.Items.AddRange(New String() {"aaaa", "bbbb", "cccc", "dddd"})
End Sub
Private Sub _theTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _theTextBox.TextChanged
If ListBoxContainsItem(_theListBox, _theTextBox.Text) Then
_theLabel.Text = "It's a match"
Else
_theLabel.Text = ""
End If
End Sub
Private Function ListBoxContainsItem(ByVal lb As ListBox, ByVal text As String) As Boolean
' check if the string is present in the list '
Return lb.Items.Contains(text)
End Function