VB Saving Listview Items Error - vb.net

Well the code itself works. The problem occurs when there is a sub items without text, the program will crash. I'm looking for a method that will bypass this annoying error.
My Code:
If ComboBox1.Text = "Everything" Then
Dim SetSave As SaveFileDialog = New SaveFileDialog
SetSave.Title = ".txt"
SetSave.Filter = ".txt File (*.txt)|*.txt"
If SetSave.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim s As New IO.StreamWriter(SetSave.FileName, False)
For Each myItem As ListViewItem In Form1.ListView1.Items
s.WriteLine(myItem.Text & TextBox1.Text & myItem.SubItems(1).Text & TextBox1.Text & myItem.SubItems(2).Text & TextBox1.Text & myItem.SubItems(3).Text & TextBox1.Text & myItem.SubItems(4).Text & TextBox1.Text & myItem.SubItems(5).Text & TextBox1.Text & myItem.SubItems(6).Text & TextBox1.Text & myItem.SubItems(7).Text) '// write Item and SubItem.
Next
s.Close()
End If
Error:(this indicates the the listview item without text it can range from number 1 up to 7, the one below is 5)
InvalidArgument=Value of '5' is not valid for 'index'.
Parameter name: index

Your indexing is starting at 1. VB indexing starts at 0 so for 5 items you whould have index values of 0 to 4

Related

Display selected radio button and check box on a msgbox when button1 is clicked VB

So what I'm trying to do is create a MessageBox when Button1 is clicked with the selected Radiobuttons and Checkboxes on it.
Here's the design:
And I want the output to be something like this:
Thank you
Better way of doing this is to loop through the groupbox controls and check if the checkboxes are checked and if yes append that to some string.And after all your checks are complete display the string using messagebox.Simple as it is.To provide some more light to the solutions please go through the below code
Looping through controls
Dim strfinal As String
For Each gb As Control In Me.Controls
If gb.GetType() Is GetType(GroupBox) Then
Dim str As String
str = gb.Text
For Each c As CheckBox In gb.Controls
If c.Checked = True Then
str = str + vbNewLine + c.Text
End If
Next
If str <> "" Then
strfinal = strfinal + vbNewLine + str
End If
End If
Next
and displaying in messagebox
If strfinal <> "" Then
MessageBox.Show(strfinal, "somecaption", MessageBoxButtons.OK)
End If
hope this helps.
This code is going to give you the result exactly as in picture2 above :
Dim Toppings As String = "Toppings:" & VbCrlf
Dim TSize As String = "Size:"
Dim CrustType As String = "Crust Type:"
Here when you press on Button1, when the name of the groubBox that contains the toppings is ' ToppingsGroupBox' and the same for the other groupBoxes:
For Each CB As CheckBox In ToppingsGroupBox.Controls
If CB.Checked Then
Toppings &= "-" & CB.Text & VbCrlf
End If
End Each
For Each RB As RadioButton In SizesGroupBox.Controls
If RB.Checked Then
TSize &= RB.Text
End If
End Each
For Each RB As RadioButton In CurstTypeGroupBox.Controls
If RB.Checked Then
CurstType &= RB.Text
End If
End Each
If DineInRadioBox.Checked Then
MsgBox(TSize & VbCrlf & CurstType & Toppings & "*Dine In")
Else
MsgBox(TSize & VbCrlf & CurstType & Toppings & "*Take Out")
End If
Hope that will help you :)

File not getting created - Visual Basic

I'm making a skype tool, and I'm creating the config file automatically, or at least that's what I think I am doing. The problem is just that for some reason the FileNotFoundException is fired because of some of my code is refering to the file which doesn't exist yet. So is visual basic checking for exceptions before firing code, or am I doing something wrong?
Here's my file creation code:
If My.Computer.FileSystem.DirectoryExists("C:\BaconSkypeTool") Then
Else
My.Computer.FileSystem.CreateDirectory("C:\BaconSkypeTool")
End If
If My.Computer.FileSystem.FileExists(Path) Then
Else
Dim fs As FileStream = File.Create(Path)
Dim line1 As Byte() = New UTF8Encoding(True).GetBytes("This is the configuration file for BaconSkypeTool" & vbCrLf & "Please do not change anything in here if you don't know what you're doing." & vbCrLf & vbCrLf & "Values below" & vbCrLf & "ChangeStatusInCall = True" & vbCrLf & "Status = Online" & vbCrLf & "Enable .cancel command = True")
fs.Write(line1, 0, line1.Length)
fs.Close()
End If
And here's the code that fires the exception:
skype.CurrentUserStatus = SKYPE4COMLib.TOnlineStatus.olsOnline
Dim text = My.Computer.FileSystem.ReadAllText("C:\BaconSkypeTool\config.cfg")
Dim textAfterStatus As String = Nothing
Dim indexOfStatus = text.IndexOf("Status = ")
Dim text1 As String = Nothing
If indexOfStatus >= 0 Then
textAfterStatus = text.Substring(indexOfStatus + "Status = ".Length)
text1 = textAfterStatus.Split("Blacklist").First().Trim()
End If
The file creation is at the top of my file and therefore should be the first thing to be fired, but maybe that's just something I think.

Listbox1 add all the items to richtextbox in the same format

I'm trying to get all the ListBox1 items and have them go to the RichTextBox1. I want it in the same format that it's in the ListBox1. I've tried different code and they seem to give extra blank lines in between items. So far I've thought of this but it give extra blank lines.
Dim counter As String
counter = ListBox1.Items.Count - 1
ListBox1.SelectedIndex = 0
If ListBox1.SelectedIndex = 0 Then
Do Until ListBox1.SelectedIndex = counter
ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
RichTextBox1.Text = RichTextBox1.Text & vbNewLine & ListBox1.SelectedItem
Loop
End If
Use LINQ to select all the items and join on Environment.NewLine:
.Net >= 4.0
Me.RichTextBox1.Text = String.Join(Environment.NewLine, (From item As Object In Me.ListBox1.Items Select Me.ListBox1.GetItemText(item).Trim()))
.Net >= 3.5
Me.RichTextBox1.Text = String.Join(Environment.NewLine, (From item As Object In Me.ListBox1.Items Select Me.ListBox1.GetItemText(item).Trim()).ToArray())
you are adding a vbNewLine before you add the ListBox text... you should put that at the end of the line RichTextBox1.Text = RichTextBox1.Text & ListBox1.SelectedItem & vbNewLine and you also shouldn't increment your selected index value until after you add the line...
Alternatively, You can do this..
For i = 0 To ListBox1.Items.Count - 1
RichTextBox1.Text = RichTextBox1.Text & ListBox1.Items(i) & vbCrLf
Next
OR this SO question has a few good examples using LINQ and condensed query code...
passing all the items of listbox in the richtextbox
This is because initially the richtextbox is empty. Hence RichTextBox1.Text = RichTextBox1.Text & vbNewLine & ListBox1.SelectedItem will add a New Line first and the add the second element of list to the richtextbox, this is because you incremented the selected index value by 1 before adding to the richtexbox. So your code can be used in an effective way as follows:
Dim counter As Integer = ListBox1.Items.Count - 1
ListBox1.SelectedIndex = 0
If ListBox1.SelectedIndex = 0 Then
Do Until ListBox1.SelectedIndex = counter
RichTextBox1.Text = RichTextBox1.Text & ListBox1.SelectedItem & vbNewLine
ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
Loop
End If
try saving the listbox to a file using a streamwriter and writeline(), then opening it into a text box using streamreader and readtoend()
'assuming you have the default names
dim reader As streamreader, writer As streamwriter
writer = new streamwriter("TMP")
for each x as object in listbox1.items
writer.writeline(X)
next
writer.close()
reader=new streamreader("TMP")
textbox1.text=reader.readtoend()
reader.close()

Login with textboxes in visual basic, 3 tries

I'm making a login script in VB using textboxes.
My problem is that the msgbox which inform the user about attempts left keep looping itself as well and using up all the (3) tries.
Not sure what's wrong.
here is my code:
Dim cor_pw As String = "1234"
Dim cor_us As String = "abcd"
Dim tries1 As Integer = 3
tries1 -= 1
Do
MsgBox("wrong combination, " & tries1 & " chances left to make it right.")
Loop Until textbox1.Text = cor_us And textbox2.Text = cor_pw Or tries1<= 0
If textbox1.Text = cor_us And textbox2.Text = cor_pw Then
MsgBox("Congratulations! That's Correct!")
Else
MsgBox("You've used all tries! " & tries1 & " chances left, good bye!")
Me.Close()
End If
You need an OK button that indicates the user has finished entering text.
In the event for the OK button you would
validate the text
If valid then you are good
otherwise increment the retry1 variable -- which must be declared at the Form (module) level!
now test retry1
if retry 1 is > 3 then display failure message and disable the OK button
otherwise display retry message
exit the sub
At this point the user can reenter the values and then hit OK button again.
No loop.
Your loop event is in the wrong area(s)
Dim cor_pw As String = "1234"
Dim cor_us As String = "abcd"
Dim tries1 As Integer = 3
Do Until (textbox1.Text = cor_us And textbox2.Text = cor_pw) Or tries1<= 0
If textbox1.Text = cor_us And textbox2.Text = cor_pw Then
MsgBox("Congratulations! That's Correct!")
Else
MsgBox("You've used all tries! " & tries1 & " chances left, good bye!")
Me.Close()
End If
MsgBox("wrong combination, " & tries1 & " chances left to make it right.")
tries1 -= 1
Loop

How to use timer in vb.net

I have this code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim num As String
Dim message As String
Dim name As String
message = txtMessage.Text
Dim count As Integer = Me.TblContactsBindingSource.Count
If i < TblContactsDataGridView.Rows.Count - 1 Then 'stay within bounds
i = i + 1 ' for all rows except Row0
TblContactsDataGridView.Rows(i - 1).DefaultCellStyle.BackColor = Color.White ' restore previous highlight
TblContactsDataGridView.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'new highlight
num = Me.TblContactsDataGridView.Rows(i).Cells(1).Value.ToString()
name = Me.TblContactsDataGridView.Rows(i).Cells(0).Value.ToString()
If SerialPort1.IsOpen() Then
SerialPort1.Write("AT" & vbCrLf)
SerialPort1.Write("AT+CMGF=1" & vbCrLf)
SerialPort1.Write("AT+CMGS=" & Chr(34) & num & Chr(34) & vbCrLf)
SerialPort1.Write(message & Chr(26))
MessageBox.Show("Message has been successfully sent to " & vbNewLine & name & " (" & num & ") ", "Message Sent", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else 'next row is off the bottom so
'i = 0 'reset index
'TblSmsDataGridView.Rows(TblSmsDataGridView.Rows.Count - 1).DefaultCellStyle.BackColor = Color.White 'restore bottom row
'TblSmsDataGridView.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'highlight top row
End If
In a command button I have this:
Timer1.Interval = 2000
Timer1.Enabled = True 'no need to enable it and start it; one or t'other
What happen is, the message box appears over and over. How can i trigger message box to automatically close once it is finished? I commented the code in the "else" because the it repeats over and over.
You have to use a custom message box. Normal message box wont do the thing you wanted. It will pop up every 2 second. best choice is to make a new form and show it as a message box. :)
You need to set timer1.enabled = false in the timer1.tick handler.