Here's my entire code below. I keep getting a Run-Time Error 13, any help would be appreciated.
Private Sub Button_Clear_Click()
Input_Wager.Text = "0"
End Sub
The section below, I would like when the command button is pressed that if the Textbox Input_Wager is empty, it displays a message, otherwise it starts the computations in the elseif section
Private Sub Button_Submit_Click()
If Me.Input_Wager.Text = "" Then
MsgBox ("Display")
ElseIf Me.Input_Wager.Text <> "" Then
TextBox2.Text = "$" & (Me.Input_Wager * 35)
TextBox3.Text = "$" & (Me.Input_Wager * 17)
TextBox4.Text = "$" & (Me.Input_Wager * 8)
TextBox5.Text = "$" & (Me.Input_Wager * 11)
TextBox6.Text = "$" & (Me.Input_Wager * 6)
TextBox7.Text = "$" & (Me.Input_Wager * 5)
TextBox8.Text = "$" & (Me.Input_Wager * 11)
TextBox9.Text = "$" & (Me.Input_Wager * 17)
TextBox10.Text = "$" & (Me.Input_Wager * 2)
TextBox11.Text = "$" & (Me.Input_Wager * 2)
TextBox12.Text = "$" & (Me.Input_Wager * 1)
TextBox13.Text = "$" & (Me.Input_Wager * 1)
TextBox14.Text = "$" & (Me.Input_Wager * 1)
TextBox15.Text = "$" & (Me.Input_Wager * 1)
TextBox16.Text = "$" & (Me.Input_Wager * 1)
TextBox17.Text = "$" & (Me.Input_Wager * 1)
End If
End Sub
As #Andrew and #Raymond said, you are getting a type mismatch error because you are trying to multiply a control/object with an Integer.
First check to see if the value of Input_Wager is an Integer and not a string - do this by not letting it accept anything else than an integer -
Private Sub Input_Wager_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Input_Wager.KeyPress
If Not Char.IsNumber(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then
e.KeyChar = ""
End If
End Sub
Now that you know it has an integer value, change your multiplication lines to this -
TextBox2.Text = "$" & (Me.Input_Wager.Text * 35)
Related
The array is a 9 position array declared as a string, I have 9 text boxes that a user can input data into, each writes to one variable in the array.
I am trying to stop filling the array, and move to print it when the user either fills in all 9 text box's, or stops filling them in when he presses the "write to file" button. From my debugging points, it looks like I get down to the "for" loop, but the program crashes with no errors that I can make heads or tails out of (not syntax or variable)... can anyone see what i'm missing?
Thanks
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\Users\foo\test2.txt"
Dim i As Integer
Dim j As Integer
Dim aryText(9) As String
MessageBox.Show(j)
aryText(0) = "[" & TextBox1.Text & "]"
j = 0
MessageBox.Show(j)
If String.IsNullOrWhiteSpace(TextBox2.Text) Then
End
Else
aryText(1) = "*" & TextBox2.Text & "{label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
'If TextBox3.Text IsNot Nothing Then
If String.IsNullOrWhiteSpace(TextBox3.Text) Then
End
Else
aryText(2) = TextBox3.Text & " {label: " & "varchar, null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox4.Text) Then
End
Else
aryText(3) = TextBox4.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox5.Text) Then
End
Else
aryText(4) = TextBox5.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox6.Text) Then
End
Else
aryText(5) = TextBox6.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox7.Text) Then
End
Else
aryText(6) = TextBox7.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox8.Text) Then
End
Else
aryText(7) = TextBox8.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox9.Text) Then
End
Else
aryText(8) = TextBox9.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
MessageBox.Show(j)
For i = 0 To j
objWriter.WriteLine(aryText(j))
i = i + 1
Next
objWriter.Close()
MessageBox.Show("Text written to file")
End Sub
Please don't debug with MessageBoxes. Visual Studio has a lovely debugger.
VB.net arrays are declared Dim myArray(UpperBound) As String. So with your 9 text boxes you have an upper bound of 8, indexes 0 to 8
You want Exit Sub not End.
A For x = 0 to y...Next works by automatically incrementing x (that is what the Next means, Next x) the default is by one but you can change that by adding a Step. If you attempt to change y you will make a mess. Your For...Next will just write the last value in the array 9 times (aryText(j)) because j doesn't change. Don't increment x in the loop; it increments automatically when Next is called.
No need to mess with i and j; just use a For Each
Using code block for your stream writer ensures all resources are released.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FILE_NAME As String = "C:\Users\foo\test2.txt"
Dim aryText(8) As String
Dim msg As String = "Please fill in all the boxes,"
If String.IsNullOrWhiteSpace(TextBox1.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(0) = "[" & TextBox1.Text & "]"
End If
If String.IsNullOrWhiteSpace(TextBox2.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(1) = "*" & TextBox2.Text & "{label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox3.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(2) = TextBox3.Text & " {label: " & "varchar, null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox4.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(3) = TextBox4.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox5.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(4) = TextBox5.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox6.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(5) = TextBox6.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox7.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(6) = TextBox7.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox8.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(7) = TextBox8.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox9.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(8) = TextBox9.Text & " {label: " & "varchar, not null" & "}"
End If
Using objWriter As New System.IO.StreamWriter(FILE_NAME, True)
For Each s As String In aryText
objWriter.WriteLine(s)
Next
End Using
MessageBox.Show("Text written to file")
End Sub
I am attempting to use a CommandButton to populate TextBoxes in a Userform based on the entries in a ListBox and can't find a way to make it work.
The below code works to populate the first TextBox (TextBox2), but everything below is coming in blank. You'll notice I added & "" after TextBox3-11 - that's because I was receiving an "Invalid Use of Null" error.
How do I get each item in the ListBox to populate in it's corresponding TextBox?
Private Sub CommandButton5_Click()
Dim i As Long
i = ListBox1.ListIndex
With Me.ListBox1
TextBox2.Text = .List(i, 0)
TextBox3.Text = .List(i, 1) & ""
TextBox4.Text = .List(i, 2) & ""
TextBox5.Text = .List(i, 3) & ""
TextBox6.Text = .List(i, 4) & ""
TextBox7.Text = .List(i, 5) & ""
TextBox8.Text = .List(i, 6) & ""
TextBox9.Text = .List(i, 7) & ""
TextBox10.Text = .List(i, 8) & ""
TextBox11.Text = .List(i, 9) & ""
End With
End Sub
How to handle second condition in If Else statement, My if statement is working but the second condition is not working(ElseIF), Is there something wrong with my condition declaration?
Try
If Val(TextBox7.Text.Trim.Split(".")(1)) >= 60 Then
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) + 1 & "." & Val(TextBox7.Text.Trim.Split(".")(1) - 60) & " " & Format(Now, "MM/dd/yyyy")
TextBox3.Text = Val(TextBox3.Text.Trim.Split(".")(0)) & "." & Val(TextBox3.Text.Trim.Split(".")(1) - 60) & " " & Format(Now, "MM/dd/yyyy")
ElseIf Val(TextBox7.Text.Trim.Split(".")(1)) >= 100 Then
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) - 1 & "." & Val(TextBox7.Text.Trim.Split(".")(1) - 45) & " " & Format(Now, "MM/dd/yyyy")
TextBox3.Text = Val(TextBox3.Text.Trim.Split(".")(0)) - 1 & "." & Val(TextBox3.Text.Trim.Split(".")(1) - 45) & " " & Format(Now, "MM/dd/yyyy")
Else
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") & Strings.Right(time.Text.Trim, 11)
TextBox3.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") - 1 & Strings.Right(time.Text.Trim, 11)
'TextBox3.Text = Format(Val(TextBox3.Text.Trim.Split(".")(0) - 1) & "." & Val(TextBox3.Text.Trim.Split(".")(1)) & " " & Format(Now, "MM/dd/yyyy"))
End If
Catch
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") & Strings.Right(time.Text.Trim, 11)
End Try
>= 100 should be the first condition, since it's the most restrictive.
>= 60 should be the second condition, since all values >=100 are also >=60.
What #James said + I'd suggest you rewrite to Select Case - should be easier to follow:
Select Case Val(TextBox7.Text.Trim.Split(".")(1))
Case Is >= 100
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) - 1 & "." & Val(TextBox7.Text.Trim.Split(".")(1) - 45) & " " & Format(Now, "MM/dd/yyyy")
TextBox3.Text = Val(TextBox3.Text.Trim.Split(".")(0)) - 1 & "." & Val(TextBox3.Text.Trim.Split(".")(1) - 45) & " " & Format(Now, "MM/dd/yyyy")
Case Is >= 60
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) + 1 & "." & Val(TextBox7.Text.Trim.Split(".")(1) - 60) & " " & Format(Now, "MM/dd/yyyy")
TextBox3.Text = Val(TextBox3.Text.Trim.Split(".")(0)) & "." & Val(TextBox3.Text.Trim.Split(".")(1) - 60) & " " & Format(Now, "MM/dd/yyyy")
Case Else
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") & Strings.Right(time.Text.Trim, 11)
TextBox3.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") - 1 & Strings.Right(time.Text.Trim, 11)
End Select
Also several points of interest:
Don't use Val, use Convert class instead, for example, Convert.ToInt32.
You can wrap TextBox3.Text.Trim.Split(".") into a function, and reuse.
You read the date twice, it's better to read a date once before Select, then do dateVar.ToString("MM/dd/yyyy").
Favour .NET syntax instead of VB6. Val, Format, Strings.Left are all VB6. .NET equivalents are Convert.*, ToString with format string, and Substring.
Control naming - TextBox3 and TextBox7 don't mean anything. Try to come up with a better name for them.
Is it possible or do I need to convert it first? This is my code but the output is coming wrong:
TextBox7.Text = TextBox6.Text + time.Text.Substring(4)
This will be the sample run:
TextBox6.Text = "5.12"
time.Text = "9.51 AM 3/3/2014"
This is the result:
Textbox7.Text = "14.63 AM 3/3/2014"
Expected Result :
Textbox7.Text = "15.03 AM 3/3/2014"
try Like This
TextBox7.Text = Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim,4)) &
Val(Strings.Right(time.Text.Trim,(time.Text.Trim.Length-4)))
EDIT
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 4)),
"##.##") & Strings.Right(time.Text.Trim, 12)
EDIT NEW REQ:
TextBox3.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00")
If Val(TextBox6.Text.Trim.Split(".")(1)) >= 60 Then
TextBox6.Text = Val(TextBox6.Text.Trim.Split(".")(0)) + 1 & "." &
Val(TextBox6.Text.Trim.Split(".")(1) - 60)
End If
TextBox7.Text = Format(Val(TextBox6.Text), "##.##") & Strings.Right(time.Text.Trim, 12)
EDIT NEW REQ
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)),
"#0.00")
Dim xDate As Date = Format(CDate(Strings.Right(time.Text.Trim, 12)), "tt
MM/dd/yyyy")
If Val(TextBox7.Text.Trim.Split(".")(1)) >= 60 Then
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) + 1 & "." &
Val(TextBox7.Text.Trim.Split(".")(1) - 60)
End If
If Val(TextBox7.Text) > 24 Then
TextBox7.Text = Format(Val(TextBox7.Text - 24), "##.##") & " " &
DateAdd(DateInterval.Day, 1, xDate)
End If
I have the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' calculate button
If String.IsNullOrEmpty(TextBox1.Text) Then
MsgBox("Please enter a value to convert!")
End If
If currentIndex = vbNull Then
MsgBox("Please select a conversion!")
End If
Select Case currentIndex
Case 1
Label2.Text = TextBox1.Text & " Celsius = " & Math.Round(((TextBox1.Text * 1.8) + 32), 2) & " Fahrenheit"
Case 2
Label2.Text = TextBox1.Text & " Fahrenheit = " & Math.Round(((TextBox1.Text - 32) / 1.8), 2) & " Celsius"
Case 3
Label2.Text = TextBox1.Text & " Kelvin = " & Math.Round((TextBox1.Text - 273.15), 2) & " Celsius"
Case 4
Label2.Text = TextBox1.Text & " Celius = " & Math.Round((TextBox1.Text + 273.15), 2) & " Kelvin"
Case 5
Label2.Text = TextBox1.Text & " Kelvin = " & Math.Round((((TextBox1.Text - 273.15) * 1.8) + 32), 2) & " Fahrenheit"
Case 6
Label2.Text = TextBox1.Text & " Fahrenheit = " & Math.Round(((((TextBox1.Text - 32) * 5) / 9) + 273.15), 2) & " Kelvin"
Case 8
Label2.Text = TextBox1.Text & " Miles P/H = " & Math.Round((TextBox1.Text * 1.609344), 2) & " Kilometers P/H"
Case 9
Label2.Text = TextBox1.Text & " Kilometers P/H = " & Math.Round((TextBox1.Text / 1.609344), 2) & " Miles P/H"
Case 11
Label2.Text = TextBox1.Text & " Kilograms = " & Math.Round((TextBox1.Text * 2.20462), 2) & " Pounds"
Case 12
Label2.Text = TextBox1.Text & " Pounds = " & Math.Round((TextBox1.Text / 2.20462), 2) & " Kilograms"
Case 14
Label2.Text = TextBox1.Text & " Meters = " & Math.Round((TextBox1.Text * 3.2808399), 2) & " Feet"
Case 15
Label2.Text = TextBox1.Text & " Feet = " & Math.Round((TextBox1.Text / 3.2808399), 2) & " Meters"
End Select
End Sub
As you can see, I have a variable (currentIndex) and have a select case statement checking them against the various conversions, however my problem lies in the piece of code above this.
If currentIndex = vbNull Then
MsgBox("Please select a conversion!")
End If
I require it to spit an error message out if the index is null, however I cannot work out a way to do this. 0 cannot be used as this is the first entry in the index, and vbNull etc do not seem to work. Can anybody point me in the right direction? Thanks.
EDIT:
This is how current index is created:
Dim currentIndex As Integer
and this is how it is assigned:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
' Get the currently selected index of the item in the list box.
currentIndex = ListBox1.FindString(currentItem)
End Sub
I would suggest using SelectedIndex instead.
In your code:
If ListBox1.SelectedIndex = -1 Then
MsgBox("Please select a conversion!")
End If
Here is the reference for the property: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindex.aspx
If currentIndex is an index from dropdown box then value of -1 means that nothing was selected.