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.
Related
I am trying to find text in a userform text box if the text is found then add + 1 to the front e.g 1 & (label.caption) when clicked again would then change this to 2 &(label.caption) Below is some of the code I've been trying to work with but to no avail, then this would have to be reversed so that when i need to reduce the amount it takes 1 of the value at a time then if no value remove from the text box.
Private Sub La01_Click()
If Uf_Device_selection.Option_Add_device.Value = True Then
Dim Textfind As Range
Dim Dset As Integer
For Each Textfind In Uf_Device_selection.Text_Device_list.Value
Dim myDevice As Variant
Dim myDevices As Variant
myDevices = TriageToArray(Texfind.Value, ",")
For Each myDevice In myDevices
Select Case myDevice
Case Is = (Sheets(3).Range("M15").Value & La01.Caption & " , ")
Dset = Dset + 1
End Select
Next
Next Textfind
Sheets(3).Range("M15").Value = Dset + 1
Uf_Device_selection.Text_Device_list.Value = Sheets(3).Range("M15").Value & " " & La01.Caption & " , " & Uf_Device_selection.Text_Device_list.Value
Else
Uf_Device_selection.Text_Device_list.Value = Replace(Text_Device_list.Value, La01.Caption & " , ", "", 1, 1)
End If
End Sub
Below is some of the code in my project this work for finding text in the cells but as you can see im finding it hard to convert to the userform text box
Option Explicit
Sub try_to_find_text()
Dim ALCell As Range
Dim car As Integer
Dim van As Integer
Dim truck As Integer
Dim digger As Integer
For Each ALCell In ActiveSheet.Range("E21:E1000")
Dim myVehicle As Variant
Dim myVehicles As Variant
myVehicles = TriageToArray(ALCell.Value,",")
For Each myVehicle In myVehicles
Select Case myVehicle
Case Is = "car"
car = car + 1
Case Is = "van"
van = van + 1
Case Is = "truck"
truck = truck + 1
Case Is = "digger"
HCAS = HCAS + 1
Case Is = ""
End Select
Next
Next ALCell
ActiveSheet.Range("B13").Value = car
ActiveSheet.Range("C13").Value = van
ActiveSheet.Range("D13").Value = truck
ActiveSheet.Range("E13").Value = digger
End Sub
Public Function TriageToArray(ByVal ipString As String, Optional ByVal ipSeparator As String = " ") As Variant
Dim myArray As Variant
myArray = Split(ipString, ipSeparator)
Dim myIndex As Long
For myIndex = LBound(myArray) To UBound(myArray)
myArray(myIndex) = LCase$(Trim$(myArray(myIndex)))
Next
TriageToArray = myArray
End Function
Any help would be much appreciated
just to add I've been trying a new way see below but i now need a way to change the number at the front as now it will find the word and put the 2 in front of the 'caption' but when its goes to 3 it put 2 3 'caption'
Private Sub La02_Click()
If Uf_Device_selection.Option_Add_device.Value = True Then
Dim n As Long
Dim nt As Long
Dim Dset As Integer
n = InStr(Uf_Device_selection.Text_Device_list.Value, La02.Caption & " ,")
nt = InStr(Uf_Device_selection.Text_Device_list.Value, Sheets(3).Range("C30").Value & " " & La02.Caption & " ,")
Dset = Sheets(3).Range("C30").Value
If n = 0 Then
Text_Device_list = La02.Caption + " , " + Text_Device_list.Value
Dset = Dset + 1
Sheets(3).Range("C30").Value = Dset
Else
Dset = Dset + 1
Sheets(3).Range("C30").Value = Dset
If nt = 0 Then
Text_Device_list = Replace(Text_Device_list.Value, La02.Caption & " , ", Sheets(3).Range("C30").Value & " " & La02.Caption & " , ", 1, 1)
Else
Text_Device_list = Replace(Text_Device_list.Value, Sheets(3).Range("C30").Value - 1 & " " & La02.Caption & " , ", Sheets(3).Range("30").Value & " " & La02.Caption & " , ", 1, 1)
MsgBox "done now onto replace"
End If
End If
Else
Text_Device_list = Replace(Text_Device_list.Value, La02.Caption & " , ", "", 1, 1)
End If
End Sub
now just last line of code to replace the number and capton with new number and caption
finally figured it out
Private Sub La02_Click()
Dim n As Long
Dim nt As Long
Dim dset As Integer
Dim dset_1 As Integer
Dim destplus As Integer
n = InStr(Uf_Device_selection.Text_Device_list.Value, La02.Caption & " ,")
nt = InStr(Uf_Device_selection.Text_Device_list.Value, Sheets(3).Range("X20").Value & " " & "X" & " " & La02.Caption & " ,")
dset = Sheets(3).Range("X20").Value
dset_1 = Sheets(3).Range("X20").Value - 1
dsetplus = Sheets(3).Range("X20").Value + 1
DsetR = Sheets(3).Range("AH20").Value
If Uf_Device_selection.Option_Add_device.Value = True Then
If n = 0 Then
Text_Device_list = La02.Caption + " , " + Text_Device_list.Value
dset = dset + 1
Sheets(3).Range("X20").Value = dset
Else
If nt = 0 Then
dset = dset + 1
Sheets(3).Range("X20").Value = dset
Text_Device_list = Replace(Text_Device_list.Value, La02.Caption & " , ", dset & " " & "X" & " " & La02.Caption & " , ", 1, 1)
Else
Text_Device_list = Replace(Text_Device_list.Value, dset & " " & "X" & " " & La02.Caption & " , ", dsetplus & " " & "X" & " " & La02.Caption & " , ", 1, 1)
dset = dset + 1
Sheets(3).Range("X20").Value = dset
End If
End If
Else
If nt > 0 Then
If dset > 2 Then
Text_Device_list = Replace(Text_Device_list.Value, dset & " " & "X" & " " & La02.Caption & " , ", dset_1 & " " & "X" & " " & La02.Caption & " , ", 1, 1)
dset = dset - 1
Sheets(3).Range("X20").Value = dset
Else
Text_Device_list = Replace(Text_Device_list.Value, dset & " " & "X" & " " & La02.Caption & " , ", La02.Caption & " , ", 1, 1)
dset = dset - 2
Sheets(3).Range("X20").Value = dset
End If
Else
dset = dset - 1
Text_Device_list = Replace(Text_Device_list.Value, La02.Caption & " , ", "", 1, 1)
End If
End If
End Sub
so this code will add the caption from the label click to the userform text-box, also if you click again it will put "number of clicked times" , X ,"caption. it doesn't matter where in the text it is. also when the remove it clicked it will count down the "number of clicked" , X , caption. then remove from text box.
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 was trying to edit the data from the selected rows in datagridview if the button is clicked and put it in the textbox to update the data and what's happening is if I select a row it is directly putted in the textboxes before I click the edit button, but what I want is if I select a row then click the edit button and the data from the selected rows will be putted in the textboxes provided. Can someone help me please..
here is my code in datagridview:
index = e.RowIndex
Dim selectedRow As DataGridViewRow
selectedRow = DataGridView1.Rows(index)
TextBox8.Text = selectedRow.Cells(1).Value.ToString
TextBox1.Text = selectedRow.Cells(2).Value.ToString
TextBox2.Text = selectedRow.Cells(3).Value.ToString
TextBox3.Text = selectedRow.Cells(4).Value.ToString
TextBox4.Text = selectedRow.Cells(5).Value.ToString
TextBox5.Text = selectedRow.Cells(6).Value.ToString
TextBox6.Text = selectedRow.Cells(7).Value.ToString
ComboBox1.Text = selectedRow.Cells(8).Value.ToString
TextBox9.Text = selectedRow.Cells(9).Value.ToString
then this is my code in edit button
Try
Dim cmd As New MySqlCommand
connect.Open()
cmd.Connection = connect
da = New MySqlDataAdapter("Select * from tbl_book_info", connect)
cmd.CommandText = "update tbl_book_info set (Title = '" & TextBox1.Text & "',Author = '" & TextBox2.Text & "',Copyright = '" & TextBox3.Text & "',Publisher = '" & TextBox4.Text & "',Place = '" & TextBox5.Text & "',Copies = '" & TextBox6.Text & "', Subject = '" & ComboBox1.Text & "', ClassNo = '" & TextBox8.Text & "',Registered by = '" & TextBox9.Text & "',Time = '" & TextBox10.Text & "',Date = '" & TextBox11.Text & "')"
cmd.ExecuteNonQuery()
MsgBox("Already Updated!")
MessageBox.Show("update tbl_book_info set (Title = '" & TextBox1.Text & "',Author = '" & TextBox2.Text & "',Copyright = '" & TextBox3.Text & "',Publisher = '" & TextBox4.Text & "',Place = '" & TextBox5.Text & "',Copies = '" & TextBox6.Text & "', Subject = '" & ComboBox1.Text & "', ClassNo = '" & TextBox8.Text & "',Registered by = '" & TextBox9.Text & "',Time = '" & TextBox10.Text & "',Date = '" & TextBox11.Text & "')")
data = New DataTable()
DataGridView1.DataSource = data
da = New MySqlDataAdapter("Select * from tbl_book_info", connect)
da.Fill(data)
Catch ex As Exception
End Try
Dim x As Integer
Dim y As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = DataGridView1.SelectedCells(0).Value()
x = DataGridView1.SelectedCells(0).RowIndex
y = DataGridView1.CurrentCell.ColumnIndex
DataGridView1.Enabled = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
DataGridView1.Enabled = True
DataGridView1.Rows(x).Cells(y).Value = TextBox1.Text
End Sub
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)
Im trying to get a file to be created and named when a button is pressed. But I need the file name to contain text from a textbox.
Code:
My.Computer.FileSystem.WriteAllText("c:\temp\" + txtUser.Text + ".txt", "[" + TimeOfDay + "]" + "Email: " + txtEmail.Text + vbNewLine + "Username: " + txtUser.Text + vbNewLine + "Password: " + txtPass.Text + vbNewLine + "Secuirty: " + txtSecuirty.Text + vbNewLine + vbNewLine, True)
Simple Code:
My.Computer.FileSystem.WriteAllText("c:\temp\" + txtUser.Text + ".txt", {loads of stuff}, True)
I have loads of if functions, to stop the disallowed characters.
The error I am getting is:
Expression does not produce a value
Try to see if this will work for you.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Computer.FileSystem.WriteAllText("c:\temp\" + txtUser.Text.ToString() + ".txt", "test message", True)
End Sub
Or you can change your code to
Dim strFileText As String = ""
strFileText =
"[" & TimeOfDay.ToString() & "] " & vbCrLf &
"Email: " & txtEmail.Text.ToString() & vbCrLf &
"Username: " & txtUser.Text.ToString() & vbCrLf &
"Password: " & txtPass.Text.ToString() & vbCrLf &
"Security: " & txtSecurity.Text.ToString() & vbCrLf & vbCrLf
My.Computer.FileSystem.WriteAllText("c:\temp\" + txtUser.Text.ToString() + ".txt", strFileText, True)