Convert input of a textbox to the output of another textbox - vb.net

I'm new to VB, been learning for about three months. I would like to take the input of a textbox, ex. “1234” and turn it into “one, two, three, four” in the second textbox (with commas). No buttons, just two textboxes. This is the code that I've been trying, it doesn't work. I get the error message:
Object reference not set to an instance of an object
If I could get a simple explanation that would be great, anything super complex will just blow my mind and make it more difficult for me to learn. You guys/gals are awesome and I'm ready to learn, thanks.
Dim boxOne = txtNumber.Text.Split(" "c)
Private Sub TxtNumber_TextChanged(sender As Object, e As EventArgs) Handles txtNumber.TextChanged
For Each i As Integer In boxOne
If txtNumber.Text = "1" Then
lblMessage.Text = "one"
End If
If txtNumber.Text = "2" Then
lblMessage.Text = "two"
End If
If txtNumber.Text = "3" Then
lblMessage.Text = "three"
End If
If txtNumber.Text = "4" Then
lblMessage.Text = "four"
End If
If txtNumber.Text = "5" Then
lblMessage.Text = "five"
End If
If txtNumber.Text = "6" Then
lblMessage.Text = "six"
End If
If txtNumber.Text = "7" Then
lblMessage.Text = "seven"
End If
If txtNumber.Text = "8" Then
lblMessage.Text = "eight"
End If
If txtNumber.Text = "9" Then
lblMessage.Text = "nine"
End If
If txtNumber.Text = "10" Then
lblMessage.Text = "ten"
End If
Next
lblMessage.Text = boxOne
End Sub

I suggest to create a mapper, a collection of items that can map/convert one value to another.
A Dictionary comes in handy for this task: given a Key, it returns the associated Value:
Private valueConverter As Dictionary(Of String, String) = New Dictionary(Of String, String) From {
{"0", "Zero"}, {"1", "One"}, {"2", "Two"}, {"3", "Three"}, {"4", "Four"},
{"5", "Five"}, {"6", "Six"}, {"7", "Seven"}, {"8", "Eight"}, {"9", "Nine"}}
A String is a collection of Chars: to parse it, you just need to loop the collection.
If you need to work with the collection of chars directly, you can use the String.ToCharArray() method (e.g., Dim charCollection = [TextBox].Text.ToCharArray()).
Check whether the current char represents a number (Char.IsNumber(char)) and map if it does:
Imports System.Text
Dim sb As New StringBuilder()
For Each part As Char In txtNumber.Text
If Not Char.IsNumber(part) Then Continue For
sb.Append(valueConverter(part) & ", ")
Next
lblMessage.Text = sb.ToString().TrimEnd({","c, " "c})
The StringBuilder class is often used when we need to cancatenate string. Since strings are immutable, each time you add a string to an existing one, you actually generate a new string each time, which needs to be garbage-collected.
For this reason, using a StringBuilder, the code performs way better.

Public Class Form1
Private Sub Str_TextChanged(sender As Object, e As EventArgs) Handles Str.TextChanged
Dim value As String = ""
For Each Str As String In Me.Str.Text
If Str = "1" Then
If value.Length = 0 Then
value = "One"
Else
value = value & ",One"
End If
ElseIf Str = "2" Then
If value.Length = 0 Then
value = "Two"
Else
value = value & ",Two"
End If
ElseIf Str = "3" Then
If value.Length = 0 Then
value = "Three"
Else
value = value & ",Three"
End If
ElseIf Str = "4" Then
If value.Length = 0 Then
value = "Four"
Else
value = value & ",Four"
End If
ElseIf Str = "5" Then
If value.Length = 0 Then
value = "Five"
Else
value = value & ",Five"
End If
ElseIf Str = "6" Then
If value.Length = 0 Then
value = "Six"
Else
value = value & ",Six"
End If
ElseIf Str = "7" Then
If value.Length = 0 Then
value = "Seven"
Else
value = value & ",Seven"
End If
ElseIf Str = "8" Then
If value.Length = 0 Then
value = "Eight"
Else
value = value & ",Eight"
End If
ElseIf Str = "9" Then
If value.Length = 0 Then
value = "Nine"
Else
value = value & ",Nine"
End If
End If
Next
'Your needed value is in variable 'value', use it to set it to another TextBox :)
End Sub
End Class

I don't think we use For Each on integer values to get individual ones. Try using For Each on string.
Here's a code:
Private Sub TxtNumber_TextChanged(sender As Object, e As EventArgs) Handles txtNumber.TextChanged
Dim value As String
For Each Str As String In TxtNumber.Text
If Str = "1" Then
If value.Length = 0 Then
value = "One"
Else
value = value & ",One"
End If
ElseIf Str = "2" Then
If value.Length = 0 Then
value = "Two"
Else
value = value & ",Two"
End If
ElseIf Str = "3" Then
If value.Length = 0 Then
value = "Three"
Else
value = value & ",Three"
End If
ElseIf Str = "4" Then
If value.Length = 0 Then
value = "Four"
Else
value = value & ",Four"
End If
ElseIf Str = "5" Then
If value.Length = 0 Then
value = "Five"
Else
value = value & ",Five"
End If
ElseIf Str = "6" Then
If value.Length = 0 Then
value = "Six"
Else
value = value & ",Six"
End If
ElseIf Str = "7" Then
If value.Length = 0 Then
value = "Seven"
Else
value = value & ",Seven"
End If
ElseIf Str = "8" Then
If value.Length = 0 Then
value = "Eight"
Else
value = value & ",Eight"
End If
ElseIf Str = "9" Then
If value.Length = 0 Then
value = "Nine"
Else
value = value & ",Nine"
End If
End If
Next
'Your needed value is in variable 'value', use it to set it to another TextBox :)
End Sub
And.... its done.

Related

How to replace letters in a textbox on time

I want to replace every letter or number in a textbox using VB.NET. This was my first try, but it only replaces one letter at a time:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Select Case True
Case TextBox1.Text.Contains("a")
TextBox1.Text = TextBox1.Text.Replace("a", "h")
Case TextBox1.Text.Contains("b")
TextBox1.Text = TextBox1.Text.Replace("b", "o")
Case TextBox1.Text.Contains("c")
TextBox1.Text = TextBox1.Text.Replace("c", "t")
Case TextBox1.Text.Contains("d")
TextBox1.Text = TextBox1.Text.Replace("d", "e")
Case TextBox1.Text.Contains("e")
TextBox1.Text = TextBox1.Text.Replace("e", "i")
Case TextBox1.Text.Contains("f")
TextBox1.Text = TextBox1.Text.Replace("f", "a")
Case TextBox1.Text.Contains("g")
TextBox1.Text = TextBox1.Text.Replace("g", "j")
Case TextBox1.Text.Contains("h")
TextBox1.Text = TextBox1.Text.Replace("h", "f")
Case TextBox1.Text.Contains("i")
TextBox1.Text = TextBox1.Text.Replace("i", "k")
Case TextBox1.Text.Contains("j")
TextBox1.Text = TextBox1.Text.Replace("j", "b")
Case TextBox1.Text.Contains("k")
TextBox1.Text = TextBox1.Text.Replace("k", "n")
Case TextBox1.Text.Contains("l")
TextBox1.Text = TextBox1.Text.Replace("l", "r")
Case TextBox1.Text.Contains("m")
TextBox1.Text = TextBox1.Text.Replace("m", "d")
Case TextBox1.Text.Contains("n")
TextBox1.Text = TextBox1.Text.Replace("n", "s")
Case TextBox1.Text.Contains("o")
TextBox1.Text = TextBox1.Text.Replace("o", "u")
Case TextBox1.Text.Contains("p")
TextBox1.Text = TextBox1.Text.Replace("p", "g")
Case TextBox1.Text.Contains("q")
TextBox1.Text = TextBox1.Text.Replace("q", "w")
Case TextBox1.Text.Contains("r")
TextBox1.Text = TextBox1.Text.Replace("r", "m")
Case TextBox1.Text.Contains("s")
TextBox1.Text = TextBox1.Text.Replace("s", "q")
Case TextBox1.Text.Contains("t")
TextBox1.Text = TextBox1.Text.Replace("t", "x")
Case TextBox1.Text.Contains("u")
TextBox1.Text = TextBox1.Text.Replace("u", "c")
Case TextBox1.Text.Contains("v")
TextBox1.Text = TextBox1.Text.Replace("v", "y")
Case TextBox1.Text.Contains("w")
TextBox1.Text = TextBox1.Text.Replace("w", "z")
Case TextBox1.Text.Contains("x")
TextBox1.Text = TextBox1.Text.Replace("x", "l")
Case TextBox1.Text.Contains("y")
TextBox1.Text = TextBox1.Text.Replace("y", "v")
Case TextBox1.Text.Contains("z")
TextBox1.Text = TextBox1.Text.Replace("z", "p")
end select
end sub
This isn't what I want so I tried this:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If TextBox1.Text.Contains("a") Then
TextBox1.Text = TextBox1.Text.Replace("a", "h")
End If
If TextBox1.Text.Contains("b") Then
TextBox1.Text = TextBox1.Text.Replace("b", "o")
End If
If TextBox1.Text.Contains("c") Then
TextBox1.Text = TextBox1.Text.Replace("c", "t")
End If
If TextBox1.Text.Contains("d") Then
TextBox1.Text = TextBox1.Text.Replace("d", "e")
End If
If TextBox1.Text.Contains("e") Then
TextBox1.Text = TextBox1.Text.Replace("e", "i")
End If
If TextBox1.Text.Contains("f") Then
TextBox1.Text = TextBox1.Text.Replace("f", "a")
End If
If TextBox1.Text.Contains("g") Then
TextBox1.Text = TextBox1.Text.Replace("g", "j")
End If
If TextBox1.Text.Contains("h") Then
TextBox1.Text = TextBox1.Text.Replace("h", "f")
End If
If TextBox1.Text.Contains("i") Then
TextBox1.Text = TextBox1.Text.Replace("i", "k")
End If
If TextBox1.Text.Contains("j") Then
TextBox1.Text = TextBox1.Text.Replace("j", "b")
End If
If TextBox1.Text.Contains("k") Then
TextBox1.Text = TextBox1.Text.Replace("k", "n")
End If
If TextBox1.Text.Contains("l") Then
TextBox1.Text = TextBox1.Text.Replace("l", "r")
End If
If TextBox1.Text.Contains("m") Then
TextBox1.Text = TextBox1.Text.Replace("m", "d")
End If
If TextBox1.Text.Contains("n") Then
TextBox1.Text = TextBox1.Text.Replace("n", "s")
End If
If TextBox1.Text.Contains("o") Then
TextBox1.Text = TextBox1.Text.Replace("o", "u")
End If
If TextBox1.Text.Contains("p") Then
TextBox1.Text = TextBox1.Text.Replace("p", "g")
End If
If TextBox1.Text.Contains("q") Then
TextBox1.Text = TextBox1.Text.Replace("q", "w")
End If
If TextBox1.Text.Contains("r") Then
TextBox1.Text = TextBox1.Text.Replace("r", "m")
End If
If TextBox1.Text.Contains("s") Then
TextBox1.Text = TextBox1.Text.Replace("s", "q")
End If
If TextBox1.Text.Contains("t") Then
TextBox1.Text = TextBox1.Text.Replace("t", "x")
End If
If TextBox1.Text.Contains("u") Then
TextBox1.Text = TextBox1.Text.Replace("u", "c")
End If
If TextBox1.Text.Contains("v") Then
TextBox1.Text = TextBox1.Text.Replace("v", "y")
End If
If TextBox1.Text.Contains("w") Then
TextBox1.Text = TextBox1.Text.Replace("w", "z")
End If
If TextBox1.Text.Contains("x") Then
TextBox1.Text = TextBox1.Text.Replace("x", "l")
End If
If TextBox1.Text.Contains("y") Then
TextBox1.Text = TextBox1.Text.Replace("y", "v")
End If
If TextBox1.Text.Contains("z") Then
TextBox1.Text = TextBox1.Text.Replace("z", "p")
End If
end sub
It doesn't work either, just with a letter at a time.
I want to be able to write, for example, "bike", in a textbox and it replaces the text in the same textbox (or another textbox) to, in this case, "pawm", but I can't see where the problem is.
Replacing the whole text doesn't work, as you have found out, because a letter can be replaced more than once. For example, the "b" is replaced with a "p" but later on you replace the "p" with an "h".
You want to do replacements character by character. Here is an example:
Imports System.Collections.Generic
Imports System.Linq
Dim replacement = New Dictionary(Of Char, Char) From
{
{"b"c, "p"c},
{"i"c, "a"c},
{"k"c, "w"c},
{"e"c, "m"c}
}
Dim word = "bike"
'For each character, we select the replacement letter
Dim letters = word.Select(Function(c) replacement(c)).ToArray()
'Construct a new string with the replaced letters
Dim newWord = New String(letters)
Imagine that the cipher was just a->b, b->c, c->d. Now, if the word is "abc", then we change all the a->b to get "bbc", then all the b->c to get "ccc" and then all the c->d to get "ddd". This is not what we wanted! Instead, we need to make a new string of letters, so have an "a" that we know ends up as "b": the new string is "b" so far. Then we look at the "b" in "abc" and we know it will become "c", so we append it to the new string to get "bc", and finally we look at the "c" in "abc", translate it to "d", append it to the new string and get "bcd". This is what we want, so we have found a method to do the enciphering.
So, we now need a way to look up the translation from the letters the user enters to the enciphered letters.
If we have a string of the letters in the alphabet in order, we can look up the position of a letter in that string by using the String.IndexOf function. So, if we ask it for the index of, say, "c" in "abcdefghijklmnopqrstuvwxyz" it will give us the number 2. It is 2 because it starts counting at zero instead of one. Now, if we have a string with the enciphered characters, we can get it to look up the character in the position we specify, so we look at the character with index 2 in the string "ypsvmdgjatwnqzkhebxurolifc" and we get "s".
Now, it is better to put little parts of code in their own methods (it means you can concentrate on a smaller piece of code if something needs correcting or changing), in this case it would be a good idea to make a function which you give the string you want enciphered and it returns the enciphered string.
So, putting those parts together, I got:
Function Encipher(s As String) As String
Dim normal = "abcdefghijklmnopqrstuvwxyz"
Dim cipher = "ypsvmdgjatwnqzkhebxurolifc"
' Make the uppercase versions too
normal = normal & normal.ToUpper()
cipher = cipher & cipher.ToUpper()
Dim newString = ""
For Each c In s
Dim arrayIndex = normal.IndexOf(c)
If arrayIndex >= 0 Then
Dim cipherChar = cipher.Chars(arrayIndex)
newString &= cipherChar
Else
newString &= c
End If
Next
Return newString
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = Encipher(TextBox1.Text)
End Sub
The If arrayIndex >= 0 Then part checks if the character was actually found, if not then the Else part appends the unaltered character.
this totally works
Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder()
For Each character As Char In RichTextBox1.Text
If character = "a"c Then
newString.Append(Chr(104))
ElseIf character = "b"c Then
newString.Append(Chr(111))
ElseIf character = "c"c Then
newString.Append(Chr(116))
ElseIf character = "d"c Then
newString.Append(Chr(101))
ElseIf character = "e"c Then
newString.Append(Chr(105))
ElseIf character = "f"c Then
newString.Append(Chr(97))
ElseIf character = "g"c Then
newString.Append(Chr(108))
ElseIf character = "h"c Then
newString.Append(Chr(102))
ElseIf character = "i"c Then
newString.Append(Chr(107))
ElseIf character = "j"c Then
newString.Append(Chr(98))
ElseIf character = "k"c Then
newString.Append(Chr(110))
ElseIf character = "l"c Then
newString.Append(Chr(114))
ElseIf character = "m"c Then
newString.Append(Chr(100))
ElseIf character = "n"c Then
newString.Append(Chr(115))
ElseIf character = "o"c Then
newString.Append(Chr(117))
ElseIf character = "p"c Then
newString.Append(Chr(103))
ElseIf character = "q"c Then
newString.Append(Chr(119))
ElseIf character = "r"c Then
newString.Append(Chr(109))
ElseIf character = "s"c Then
newString.Append(Chr(113))
ElseIf character = "t"c Then
newString.Append(Chr(120))
ElseIf character = "u"c Then
newString.Append(Chr(99))
ElseIf character = "v"c Then
newString.Append(Chr(121))
ElseIf character = "w"c Then
newString.Append(Chr(122))
ElseIf character = "x"c Then
newString.Append(Chr(108))
ElseIf character = "y"c Then
newString.Append(Chr(118))
ElseIf character = "z"c Then
newString.Append(Chr(112))
ElseIf character = " "c Then
newString.Append(Chr(32))
Else
newString.Append(Chr(Asc(character) + 2))
End If
Next
RichTextBox1.Text = newString.ToString()

Change Textbox value after selection from Dropdownlist

When I select a value from the first dropdownlist, it populates the second dropdownlist and textbox to their correct values. However, when I select a different value from the second dropdown, the textbox value still retains the same value from which it was populated with. Could anyone please point out what I am doing wrong?
If ds.Tables(0).Rows(0).Item(3).ToString = "M" Then
DropDownList2.Items.Insert(0, "Member")
DropDownList2.Items.Insert(1, "Non-Member")
DropDownList2.Items.Insert(2, "")
TextBox1.Text = "M"
ElseIf ds.Tables(0).Rows(0).Item(3).ToString = "NM" Then
DropDownList2.Items.Insert(0, "Non-Member")
DropDownList2.Items.Insert(1, "Member")
DropDownList2.Items.Insert(2, "")
TextBox1.Text = "NM"
Else
DropDownList2.Items.Insert(0, "")
DropDownList2.Items.Insert(1, "Member")
DropDownList2.Items.Insert(2, "Non-Member")
TextBox1.Text = ""
End If
If DropDownList2.SelectedValue = "Member" Then
TextBox1.Text = ""
TextBox1.Text = "M"
ElseIf DropDownList2.SelectedValue = "Non-Member" Then
TextBox1.Text = ""
TextBox1.Text = "NM"
Else
TextBox1.Text = ""
End If

Simpler Way to Building an SQL Statement from a VB.net form?

I have a form that builds an SQL statement from user input. The controls include CheckBox, TextBox, ComboBox, and DateTimePicker.
They are grouped in a global array for each section of the form. Some statements will check against multiple controls for an SQL command.
Right now I use a bunch of if...else if...else statements to build my SQL statement, checking against check states, text changes and so on. However, this method has proven to be unstable if I change the control index in the array.
I'm looking for an easier way to build the SQL statement in one go. I've thought about changing a string for each control group every time a control's status has changed, thus taking away the need to build the SQL when the user wants to run it, however I'm not sure if that is a good way to handle it.
Is there an alternative method to the one I currently use? Will the one I've been thinking about switching to worth the effort?
Edit: As requested, here some of the code I currently use.
'icControls is the class that has all of the global arrays
'DefaultStrings is the module that has the default strings in the textboxes
Dim sqlWhere As String = "Where "
If Me._icControls.chkBasic(0).Checked = True And Me._icControls.chkBasic(1).Checked = True _
And Me._icControls.chkBasic(2).Checked = True Then
sqlWhere += """Field1"" in('A', 'B', 'C')"
ElseIf Me._icControls.chkBasic(0).Checked = True And Me._icControls.chkBasic(1).Checked = True Then
sqlWhere += """Field1"" in('A', 'B')"
ElseIf Me._icControls.chkBasic(0).Checked = True And Me._icControls.chkBasic(2).Checked = True Then
sqlWhere += """Field1"" in('A', 'C')"
ElseIf Me._icControls.chkBasic(1).Checked = True And Me._icControls.chkBasic(2).Checked = True Then
sqlWhere += """Field1"" in('B', 'C')"
ElseIf Me._icControls.chkBasic(0).Checked = True Then
sqlWhere += """Field1"" in('A')"
sqlWhere += " AND (SUBSTR(""Tag #"", 1, 1) not in('#') and ""Inventory Type"" in('F'))"
ElseIf Me._icControls.chkBasic(1).Checked = True Then
sqlWhere += """Field1"" in('B')"
sqlWhere += " AND (SUBSTR(""Tag #"", 1, 1) in('R', 'A', 'Z') and ""Inventory Type"" in('R'))"
ElseIf Me._icControls.chkBasic(2).Checked = True Then
sqlWhere += """Field1"" in('')"
sqlWhere += " AND (SUBSTR(""Tag #"", 1, 1) in('W'))"
End If
If Me._icControls.chkBasic(4).Checked = True And Me._icControls.chkBasic(5).Checked = True Then
sqlWhere += " AND ""Field2"" in('A', 'B')"
ElseIf Me._icControls.chkBasic(4).Checked = True Then
sqlWhere += " AND ""Field2"" in('A')"
ElseIf Me._icControls.chkBasic(5).Checked = True Then
sqlWhere += " AND ""Field2"" in('B')"
End If
If Me._icControls.chkBasic(6).Checked = True And Me._icControls.chkBasic(7).Checked = True Then
sqlWhere += " AND ""Field3"" in('A', 'B')"
ElseIf Me._icControls.chkBasic(6).Checked = True Then
sqlWhere += " AND ""Field3"" in('A')"
ElseIf Me._icControls.chkBasic(7).Checked = True Then
sqlWhere += " AND ""Field3"" in('B')"
End If
If Me._icControls.txtMaterial(0).Text.ToString() <> DefaultStrings.Material(0) Then
sqlWhere += " AND ""Material"" in('" + Me._icControls.txtMaterial(0).Text.ToString() + "')"
End If
The solution I proposed in comments will comprise the following steps:
Create the List< Tuple < string,List >> selectValues that will contain one item per field, each item (a Tuple) composed of field name and field values.
Create the Tuple < string,List> field1Values = new Tuple <"Field1,field1Values> and the inner List. Add field1Values to selectValues
Loop on _icControls.chkBasic items and add item value to field1 values List,
Same process for field2Values, field3Values,...
Loop on SelectValues ignoring the fields with an empty list to build SQL command : get field name from element1 of the tuple and values from the list contained in element2.
I think approach with generating SQL statement only when you need it(after pressing a button) is better then generating it every time when values changes.
Then you don't need checking value every time,
check it ones and save result to the variable
Dim chkControl0 As CheckBox
Dim chkControl1 As CheckBox
Dim chkControl2 As CheckBox
Dim chkControl3 As CheckBox
Dim chkControl4 As CheckBox
Dim chkControl5 As CheckBox
Dim chkControl6 As CheckBox
Dim chkControl7 As CheckBox
Private Function GenerateField1WhereClause() As String
Const CHK0 As String = "'A'"
Const CHK1 As String = "'B'"
Const CHK2 As String = "'C'"
Dim checked As New List(Of String)()
If chkControl0.Checked = True Then checked.Add(CHK0)
If chkControl1.Checked = True Then checked.Add(CHK1)
If chkControl2.Checked = True Then checked.Add(CHK2)
If checked.Count = 0 Then Return String.Empty
If checked.Count = 1 Then
Select Case checked(0)
Case CHK0
Return String.Format("""Field1"" = {0} AND (SUBSTR(""Tag #"", 1, 1) NOT IN('#') AND ""Inventory Type"" IN ('F'))", CHK0)
Case CHK1
Return String.Format("""Field1"" = {0} AND (SUBSTR(""Tag #"", 1, 1) in('R', 'A', 'Z') and ""Inventory Type"" in('R'))", CHK1)
Case CHK2
Return String.Format("""Field1"" = {0} AND (SUBSTR(""Tag #"", 1, 1) in('R', 'A', 'Z') and ""Inventory Type"" in('R'))", CHK2)
End Select
String.Format("Field1 = {0} AND (SUBSTR(""Tag #"", 1, 1) in('W'))")
Else
Return String.Format("""Field1"" IN ({0})", String.Join(",", checked))
End If
End Function
Private Function GenerateField2WhereClause() As String
Const CHK0 As String = "'A'"
Const CHK1 As String = "'B'"
Dim checked As New List(Of String)()
If chkControl4.Checked = True Then checked.Add(CHK0)
If chkControl5.Checked = True Then checked.Add(CHK1)
If checked.Count = 0 Then Return String.Empty
Return String.Format("""Field2"" IN ({0})", String.Join(",", checked))
End Function
Private Function GenerateField3WhereClause() As String
Const CHK0 As String = "'A'"
Const CHK1 As String = "'B'"
Dim checked As New List(Of String)()
If chkControl6.Checked = True Then checked.Add(CHK0)
If chkControl7.Checked = True Then checked.Add(CHK1)
If checked.Count = 0 Then Return String.Empty
Return String.Format("""Field3"" IN ({0})", String.Join(",", checked))
End Function
Then using function and checking if result empty or not you can create your WHERE clause
Private Function GenerateWhereClause() as String
Dim where As New StringBuilder()
Dim field1 As String = Me.GenerateField1WhereClause()
If String.IsNullOrEmpty(field1) = false Then
where.Append(field1)
End If
Dim field2 As String = Me.GenerateField2WhereClause()
If String.IsNullOrEmpty(field2) = false Then
If where.length > 0 Then where.Append(" AND ")
where.Append(field2)
End If
Dim field3 As String = Me.GenerateField3WhereClause()
If String.IsNullOrEmpty(field3) = false Then
If where.length > 0 Then where.Append(" AND ")
where.Append(field3)
End If
If where.Length > 0 Then where.Insert(0, "WHERE ")
Return where.ToString()
End Function
If you need values of the checkboxes outside of your form, just create a property which return only value
Public ReadOnly Property Check0 As Boolean
Return Me.chkControl0.Checked
End property

How to replace letters on a textbox using vb.net

I want to replace every letter or number on a textbox using vb.net this was my first try, but it only replaces one letter at a time
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Select Case True
Case TextBox1.Text.Contains("a")
TextBox1.Text = TextBox1.Text.Replace("a", "c")
Case TextBox1.Text.Contains("b")
TextBox1.Text = TextBox1.Text.Replace("b", "d")
Case TextBox1.Text.Contains("c")
TextBox1.Text = TextBox1.Text.Replace("c", "e")
Case TextBox1.Text.Contains("d")
TextBox1.Text = TextBox1.Text.Replace("d", "f")
Case TextBox1.Text.Contains("e")
TextBox1.Text = TextBox1.Text.Replace("e", "g")
Case TextBox1.Text.Contains("f")
TextBox1.Text = TextBox1.Text.Replace("f", "h")
Case TextBox1.Text.Contains("g")
TextBox1.Text = TextBox1.Text.Replace("g", "i")
Case TextBox1.Text.Contains("h")
TextBox1.Text = TextBox1.Text.Replace("h", "j")
Case TextBox1.Text.Contains("i")
TextBox1.Text = TextBox1.Text.Replace("i", "k")
Case TextBox1.Text.Contains("j")
TextBox1.Text = TextBox1.Text.Replace("j", "l")
Case TextBox1.Text.Contains("k")
TextBox1.Text = TextBox1.Text.Replace("k", "m")
Case TextBox1.Text.Contains("l")
TextBox1.Text = TextBox1.Text.Replace("l", "n")
Case TextBox1.Text.Contains("m")
TextBox1.Text = TextBox1.Text.Replace("m", "o")
Case TextBox1.Text.Contains("n")
TextBox1.Text = TextBox1.Text.Replace("n", "p")
Case TextBox1.Text.Contains("o")
TextBox1.Text = TextBox1.Text.Replace("o", "q")
Case TextBox1.Text.Contains("p")
TextBox1.Text = TextBox1.Text.Replace("p", "r")
Case TextBox1.Text.Contains("q")
TextBox1.Text = TextBox1.Text.Replace("q", "s")
Case TextBox1.Text.Contains("r")
TextBox1.Text = TextBox1.Text.Replace("r", "t")
Case TextBox1.Text.Contains("s")
TextBox1.Text = TextBox1.Text.Replace("s", "u")
Case TextBox1.Text.Contains("t")
TextBox1.Text = TextBox1.Text.Replace("t", "v")
Case TextBox1.Text.Contains("u")
TextBox1.Text = TextBox1.Text.Replace("u", "w")
Case TextBox1.Text.Contains("v")
TextBox1.Text = TextBox1.Text.Replace("v", "x")
Case TextBox1.Text.Contains("w")
TextBox1.Text = TextBox1.Text.Replace("w", "y")
Case TextBox1.Text.Contains("x")
TextBox1.Text = TextBox1.Text.Replace("x", "z")
Case TextBox1.Text.Contains("y")
TextBox1.Text = TextBox1.Text.Replace("y", "a")
Case TextBox1.Text.Contains("z")
TextBox1.Text = TextBox1.Text.Replace("z", "b")
End Select
End Sub
This isn't what I want, so I tried this
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If TextBox1.Text.Contains("a") Then
TextBox1.Text = TextBox1.Text.Replace("a", "c")
End If
If TextBox1.Text.Contains("b") Then
TextBox1.Text = TextBox1.Text.Replace("b", "d")
End If
If TextBox1.Text.Contains("c") Then
TextBox1.Text = TextBox1.Text.Replace("c", "e")
End If
If TextBox1.Text.Contains("d") Then
TextBox1.Text = TextBox1.Text.Replace("d", "f")
End If
If TextBox1.Text.Contains("e") Then
TextBox1.Text = TextBox1.Text.Replace("e", "g")
End If
If TextBox1.Text.Contains("f") Then
TextBox1.Text = TextBox1.Text.Replace("f", "h")
End If
If TextBox1.Text.Contains("g") Then
TextBox1.Text = TextBox1.Text.Replace("g", "i")
End If
If TextBox1.Text.Contains("h") Then
TextBox1.Text = TextBox1.Text.Replace("h", "j")
End If
If TextBox1.Text.Contains("i") Then
TextBox1.Text = TextBox1.Text.Replace("i", "k")
End If
If TextBox1.Text.Contains("j") Then
TextBox1.Text = TextBox1.Text.Replace("j", "l")
End If
If TextBox1.Text.Contains("k") Then
TextBox1.Text = TextBox1.Text.Replace("k", "m")
End If
If TextBox1.Text.Contains("l") Then
TextBox1.Text = TextBox1.Text.Replace("l", "n")
End If
If TextBox1.Text.Contains("m") Then
TextBox1.Text = TextBox1.Text.Replace("m", "o")
End If
If TextBox1.Text.Contains("n") Then
TextBox1.Text = TextBox1.Text.Replace("n", "p")
End If
If TextBox1.Text.Contains("o") Then
TextBox1.Text = TextBox1.Text.Replace("o", "q")
End If
If TextBox1.Text.Contains("p") Then
TextBox1.Text = TextBox1.Text.Replace("p", "r")
End If
If TextBox1.Text.Contains("q") Then
TextBox1.Text = TextBox1.Text.Replace("q", "s")
End If
If TextBox1.Text.Contains("r") Then
TextBox1.Text = TextBox1.Text.Replace("r", "t")
End If
If TextBox1.Text.Contains("s") Then
TextBox1.Text = TextBox1.Text.Replace("s", "u")
End If
If TextBox1.Text.Contains("t") Then
TextBox1.Text = TextBox1.Text.Replace("t", "v")
End If
If TextBox1.Text.Contains("u") Then
TextBox1.Text = TextBox1.Text.Replace("u", "w")
End If
If TextBox1.Text.Contains("v") Then
TextBox1.Text = TextBox1.Text.Replace("v", "x")
End If
If TextBox1.Text.Contains("w") Then
TextBox1.Text = TextBox1.Text.Replace("w", "y")
End If
If TextBox1.Text.Contains("x") Then
TextBox1.Text = TextBox1.Text.Replace("x", "z")
End If
If TextBox1.Text.Contains("y") Then
TextBox1.Text = TextBox1.Text.Replace("y", "a")
End If
If TextBox1.Text.Contains("z") Then
TextBox1.Text = TextBox1.Text.Replace("z", "b")
End If
End Sub
It doesn't work either, just with a letter at a time.
I want to be able to write in a textbox for example "bike" and it replaces the text in the same textbox (or other textbox) in this case: "dkmg" but I can't see where the problem is.
To expand upon Joe's answer and pseudo code, a simple For Each loop through the string will allow you to process one character at a time. The easiest way to do this is to use the ASCII value of the character to determine what to replace it with, but as Joe noted you have to handle the edge cases, since the ASCII table contains many symbols, not just letter, and they're in a specific order.
In your posted code, you appear to be replacing each letter with the corresponding letter 2 spaces from the current letter's position (i.e., a = c, b = d, etc.).
The ASCII table uses 65-90 for A to Z, and 97-122 for a to z. Hence the edge cases for Y, Z, y and z - if you add 2 to Z, for example, you will get |, rather than B. This is where If statements can help (there are other ways to do it as well, like Select).
Sample code to illustrate this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim newString As StringBuilder = New StringBuilder()
For Each character As Char In TextBox1.Text
If character = "Y"c Then
newString.Append(Chr(65))
Else If character = "Z"c Then
newString.Append(Chr(66))
Else If character = "y"c Then
newString.Append(Chr(97))
Else If character = "z"c Then
newString.Append(Chr(98))
Else
newString.Append(Chr(Asc(character) + 2))
End If
Next
TextBox1.Text = newString.ToString()
End Sub
Pretty straightforward. I used StringBuilder as it can be a little more efficient, depending on the length of the string. The alternative:
Dim newString As String
newString = newString + Chr(character)
Would result in a new copy of newString being made for each iteration through the loop - so if you had a 10 character string, you'd wind up with 10 copies of newString - one for each loop.
The Asc function gets the ASCII value of the character, and the Chr function gets the character specified by the ASCII value. The lowercase c next to the character (i.e., "A"c) simply indicates it's a Char, not a String.
Note that the above code will handle any ASCII value, so if you want to handle only characters, you'll need to do more checking in the loop. I'll leave that as an exercise for the readers.
What you must do is to:
Loop through each character of the textbox one after the other
Each time, you take one character, "process it" (replace it in your case), then you write it in a separate string, a bit like this (pseudo-code):
new_string = ""
for char in textbox.text:
# do something with char, your 'if' routines would probably work
new_string = new_string + char
Then you assign the new string to the textbox:
textbox.text = new_string
If you always want to add 2 letters to each letter, there's a way to treat each character as an integer. There's an example here (look at the bottom). Once you have this, you can simply add "2" to your char before printing it back to the string (some conversion might be needed, i'll let you figure that out):
for char in textbox.text:
new_string = new_string + (char + 2)
This mostly works, but you'll have to treat edge cases (y, z, Y, Z) yourself. You can use this chart for reference.
Here you go:
TextBox1.Text = (From c In TextBox1.Text.ToLower
Where c >= "a"c AndAlso c <= "z"c
Select Chr(97 + (Asc(c) - 95) Mod 26)).ToArray
To change first letter to upper and the rest to lower after loss of focus:
Private Sub CapsLock(TextCaps)
'تغير اول حرف الي حرف كير
Dim fNameS As String = ""
Dim liS As String = ""
Dim lis2 As String = ""
For i = 1 To Len(TextCaps)
liS = Mid(TextCaps, i, 1)
If i > 1 Then
lis2 = Mid(TextCaps, (i - 1), 1)
End If
If i = 1 Or lis2 = " " Then
liS = liS.ToUpper
fNameS = fNameS & liS
Else
liS = liS.ToLower
fNameS = fNameS & liS
End If
Next
TextCaps2 = fNameS

How to check the length value of cell in datagrid view?

I'm able to validate the cells if they are empty but I'm not able to check the length of the cell. I want the user to enter 5 digits and if it is less than 5, show up a message box.
I tried cellvalue.length method but its not working.
Private Sub dgvResults_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvResults.CellValidating
'variables
Dim columnName As String = dgvResults.Columns(e.ColumnIndex).Name
Dim cellVal As String = e.FormattedValue.ToString()
'Datagrid view validation
If ((e.ColumnIndex = 0) And (e.FormattedValue = "") And (Not (cellVal.Length = 5))) Then
e.Cancel = True
MessageBox.Show(columnName & " must be 5 Digits Long!")
ElseIf (e.ColumnIndex = 1 And e.FormattedValue = "") Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
ElseIf (e.ColumnIndex = 2 And e.FormattedValue = "") Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
ElseIf ((e.ColumnIndex = 3) And (e.FormattedValue = "") And (Not IsNumeric(e.FormattedValue))) Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
ElseIf ((e.ColumnIndex = 4) And (Not IsNumeric(e.FormattedValue))) Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
End If
End Sub
Following causes your code to not work:
(e.FormattedValue = "") And (Not (cellVal.Length = 5))
You are checking if the value is empty and also it's length is <> 5.
But you want to ensure that cellVal.Length is = 5:
If e.ColumnIndex = 0 AndAlso cellVal.Length <> 5 Then
e.Cancel = True
MessageBox.Show(columnName & " must be 5 Digits Long!")
End If
I think you will need:
cellval.text.length