Saving multiple items in a ComboBox control in VB.NET - vb.net

I'm working on a student grading system and I want a suggestion to save all the data that were entered in the TextBox Controls into a text file, but I'm facing a problem. In my program the student can select multiple courses from the ComboBox Control and enter a grade for each one of them.
How can I save all the selected items from the ComboBox control with the grades entered for the same student?
For example: A student enters his name Adam K., and Adam K. selected six courses and put grades for each one of them.
How can I save all these pieces of information in order to be displayed like the following?
Adam K. , history 98/100, math 56/100, geography 78/100 and so on.

Since the information you describe is not very clear, I can only try to provide you with a solution based on the information you provide.
The effect is as follows:
In test.txt file:
You can try my method if you want this effect.
Public Class Form1
Dim name As String
Dim info As New Dictionary(Of String, String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim course As String() = {"History", "Math", "English", "Chinese", "Science", "Biology"}
For Each item As String In course
ComboBox1.Items.Add(item)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.Text IsNot "" Then
If TextBox2.Text Is "" Then
Try
info.Add(ComboBox1.Text, "0 / 100") 'score default
Catch
MsgBox("The course " & ComboBox1.Text & " already exists.")
End Try
Else
Try
info.Add(ComboBox1.Text, TextBox2.Text & "/100")
Catch
MsgBox("The course " & ComboBox1.Text & " already exists.")
End Try
End If
Else
MsgBox("Please enter course info")
End If
ComboBox1.Text = "" 'clear course info
TextBox2.Text = ""
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
name = TextBox1.Text
TextBox3.Text &= name & " "
For Each kvp As KeyValuePair(Of String, String) In info
TextBox3.Text &= kvp.Key.ToString & ":" & kvp.Value.ToString & " "
Next kvp
TextBox3.Text &= vbCrLf
TextBox1.Text = ""
info.Clear() 'clear all info
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim file As System.IO.StreamWriter 'Write Text to Files with a StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("D:\test.txt", True)
file.WriteLine(TextBox3.Text)
file.Close()
TextBox3.Text = "" 'Append to Text Files in Visual Basic
'Dim inputString As String = "This is a test string."
'My.Computer.FileSystem.WriteAllText("D:\test.txt", inputString, True)
End Sub
End Class

Related

Add a completely different line with every button click

Complete noob to vb.net (and programming in general) here, all I really want is every time I click a button, the number in the textbox is added by 1 but the new number shows up on the next line. Tried to google this a hundred times but nothing really helped.
I don't want to use loops as I don't want all numbers to show up at once, only for the added number to show up after clicking a specific button (on a new line).
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtoutput As String = ""
Dim a As Integer = 1
txtoutput &= "the value of a =" & a & Environment.NewLine
a = a + 1
TextBox1.Text = txtoutput
End Sub
You are replacing the Text, you want to append a new line, so you need to do:
Private a As Int32 = 0
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a += 1
Dim newLine = $"the value of a = {a}"
TextBox1.Text = TextBox1.Text & Environment.NewLine & newLine
End Sub
You also have to use a field and not a local variable if you want to retain the old value and increment it. Otherwise it is reset always to it's inital value.
Please try to change dim a to static a
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtoutput As String = ""
Static a As Integer = 1
txtoutput &= "the value of a =" & a & Environment.NewLine
a = a + 1
TextBox1.Text = txtoutput
End Sub

Index Val List VB.Net

I have a Listbox called ListTxtDrawR4. Every time I select an item, I want to display the value corresponding to the index in a textbox with name TxtNumberListScan.Lines(i). for example, if I select the 5th line in ListTxtDrawR4, I want to display the value of line 5 from TxtNumberListScan in another Textbox. how do I do that?
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(0))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(1))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(2))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(3))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(4))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(5))
Names of controls have been changed to protect the innocent. (I watch Dragnet :-)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This is just some data to test with
Dim A() As String = {"Mathew", "Mark", "Luke", "John"}
Dim B() As String = {"Peter", "Paul", "James", "Judas"}
ListBox1.Items.AddRange(A)
For Each s In B
TextBox1.Text &= s & Environment.NewLine
Next
TextBox1.Text.TrimEnd() 'Remove final new line
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim index As Integer = ListBox1.SelectedIndex
'Check that the index does not exceed the number of lines.
If index < TextBox1.Lines.Count Then
Dim StringToCopy = TextBox1.Lines(index)
TextBox2.Text &= StringToCopy & Environment.NewLine
End If
End Sub

search and count occurances of a word in a textbox string

Right now I have two textboxes. One is the input and the other the text string. It functions perfectly. Is there a way to get rid of the input box and have the "searched" word in the code so when I hit a button it gives the result.
Private Function FindWords(ByVal TextSearched As String, ByVal Paragraph As String) As Integer
Dim location As Integer = 0
Dim occurances As Integer = 0
Do
location = TextSearched.IndexOf(Paragraph, location)
If location <> -1 Then
occurances += 1
location += Paragraph.Length
End If
Loop Until location = -1
Return occurances
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = TextBox2.Text ' display result
Label2.Text = FindWords(TextBox1.Text, TextBox2.Text)
' MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")
End Sub
There are a couple of small problems that you'll might see of you turn on option strict in your project's compile settings. Your function returns an integer. To properly assign it to a textbox label, you should really add .ToString to the end of the assignment like this
Label2.Text = FindWords(TextBox1.Text, TextBox2.Text).ToString
So leaving you code as you wrote it, in this sub, you just want to pass a string as the parameter for the text to find?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = TextBox2.Text ' display result
Label2.Text = FindWords(TextBox1.Text, TextBox2.Text)
' MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")
End Sub
Just change it to this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim searchString as string="Hello" 'or whatever string you want to use
Label1.Text = TextBox2.Text ' display result
Label2.Text = FindWords(searchString, TextBox2.Text)
' MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")
End Sub
but it might be better to change it a bit more so that your function only gets executed once if you want to display a messagebox with the number of occurences - like this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim searchCount as Integer
dim searchString as string="Hello" 'or whatever string you want to use
Label1.Text = TextBox2.Text ' display result
searchCount = FindWords(searchString, TextBox2.Text)
Label2.Text = searchcount.ToString
' MsgBox("The word " & TextBox2.Text & " has occured " & searchcount.Tostring & " times!!")
End Sub

How do you change the value in my.settings in a form when you enter numbers in a textbox in VB.Net

I was wondering if you could help me? My question is that, is there a way of changing the value in my.Settings in a form if you enter a number/decimal in a textbox and click a button and then update in the settings to be then changed in another from which is linked to my.Settings in a variable?!
Form 1:
Public Class frmConverter
Dim input As String
Dim result As Decimal
Dim EUR_Rate As Decimal = My.Settings.EUR_Rates
Dim USD_Rate As Decimal = 1.6
Dim JYP_Rate As Decimal = 179.65
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
input = txtInput.Text
Try
If ComboBox1.Text = "£" Then
Pounds()
ElseIf ComboBox1.Text = "€" Then
Euros()
ElseIf ComboBox1.Text = "$" Then
Dollars()
ElseIf ComboBox1.Text = "¥" Then
Yen()
End If
Catch es As Exception
MsgBox("Error!")
End Try
End Sub
Private Sub btnSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettings.Click
Me.Hide()
frmExchange.Show()
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtInput.Text = ""
lblResult.Text = ""
End Sub
Function Pounds()
If ComboBox1.Text = "£" And ComboBox2.Text = "€" Then
result = (input * EUR_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "$" Then
result = (input * USD_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "¥" Then
result = (input * JYP_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
End If
Return 0
End Function
Form 2:
Public Class frmExchange
Private Sub frmExchange_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
My.Settings.EUR_Rates = (txtinput.Text)
My.Settings.Save()
My.Settings.Reload()
End Sub
Public Sub SetNewRate(ByVal rate As Decimal)
txtinput.Text = rate.ToString
End Sub
Private Sub btnchange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnchange.Click
If ComboBox1.Text = "€" Then
My.Settings.USD_Rates = (txtinput.Text)
frmConverter.SetNewRate(txtinput.Text)
End If
End Sub
End class
It sounds like you are trying to use My.Settings as some sort of set of global reference variables. Thats not what they are for, not how they work and not what they are.
First, turn on Option Strict as it looks like it may be Off. This will store the decimal value of a textbox to a Settings variable which is defined as a Decimal:
My.Settings.USD_Rates = CDec(SomeTextBox.Text)
Thats all it will do. It wont save the value and it wont pass it around or share it with other forms and variables.
My.Settings.Save 'saves current settings to disk for next time
My.Settings.Reload 'Load settings from last time
This is all covered on MSDN. There is no linkage anywhere. If you have code in another form like this:
txtRate.Text = My.Settings.USD_Rates.ToString
txtRate will not automatically update when you post a new value to Settings. There are just values not Objects (see Value Types and Reference Types). To pass the new value to another form:
' in other form:
Public Sub SetNewRate(rate As Decimal)
' use the new value:
soemTextBox.Text = rate.ToString
End Sub
in form which gets the change:
Private Sub btnchangeRate(....
' save to settings which has nothing to do with passing the data
My.Settings.USD_Rates = CDec(RateTextBox.Text)
otherForm.SetNewRate(CDec(RateTextBox.Text))
End Sub
You may run into problems if you are using the default form instance, but that is a different problem.
You botched the instructions. The 2 procedures are supposed to go in 2 different forms - one to SEND the new value, one to RECEIVE the new value. With the edit and more complete picture, there is an easier way.
Private Sub btnSettings_Click(...) Handles btnSettings.Click
' rather than EMULATE a dialog, lets DO a dialog:
'Me.Hide()
'frmExchange.Show()
Using frm As New frmExchange ' the proper way to use a form
frm.ShowDialog
' Im guessing 'result' is the xchg rate var
result = CDec(frm.txtInput.Text)
End Using ' dispose of form, release resources
End Sub
In the other form
Private Sub btnchange_Click(....)
' do the normal stuff with Settings, if needed then:
Me.Hide
End Sub

Change every character in textbox

I am having a problem with string replacement. Below is my code as of now. I want to replace each character in textbox1 and write it to textbox2, but this only works for the last character.
If I write:
Hello
Then it should end up as:
[[h]][[e]][[l]][[l]][[o]]
Public Class Form1
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Try
TextBox2.Text = TextBox1.Text.Replace("0"c, "[[something0a0]]")
TextBox2.Text = TextBox1.Text.Replace("1"c, "[[something1a1]]")
TextBox2.Text = TextBox1.Text.Replace("2"c, "[[something2a2]]")
TextBox2.Text = TextBox1.Text.Replace("3"c, "[[something3a3]]")
Catch ex As Exception
End Try
End Sub
End Class
You're overwriting the value of TextBox2. Chain your Replace calls instead and set the assignment once.
TextBox2.Text = TextBox1.Text.Replace("0"c, "[[something0a0]]")
.Replace("1"c, "[[something1a1]]")
.Replace("2"c, "[[something2a2]]")
.Replace("3"c, "[[something3a3]]")
A way you could do this is using a loop like this. Not sure if it's the most efficient, but it's fairly easy to understand:
TextBox2.Text = ""
For Each chr As Char In TextBox1.Text
TextBox2.Text += "[[" & chr & "]]"
Next
And another simple way is:
TextBox2.Text = "[[" & String.Join("]][[ ", TextBox1.Text.ToCharArray().AsEnumerable()) & "]]"
HTH