I am creating a random generator of text.
It works ok but a small issue that I can't seem to resolve is occurring.
When I click my test button, every now and then a blank item appears. Here is my code.
Dim rng As New System.Random()
Dim RAND(16) As String
RAND(0) = "A"
RAND(1) = "B"
RAND(2) = "C"
RAND(3) = "D"
RAND(4) = "E"
RAND(5) = "F"
RAND(6) = "G"
RAND(7) = "H"
RAND(8) = "I"
RAND(9) = "J"
RAND(10) = "K"
RAND(11) = "L"
RAND(12) = "M"
RAND(13) = "N"
RAND(14) = "O"
RAND(15) = "P"
TextBox1.Text = RAND(rng.Next(RAND.Count()))
Your RAND(16) as declared contains 17 elements. Last one is blank, because you did not initialize it to anything. You can use a Watch window to verify:
Regarding how Nothing becomes a blank, it's a result of an implicit conversion behind the scenes.
Related
I'm trying to mix a column on a datagridview with 2 control types (Checkbox & TextBox), the data is coming from a Stored Procedure that I'm also writing so I have a little flexibility.
in the stored procedure I'm returning a blank column to act as a Selection column in the GridView, but I am encountering problems when trying to convert cells to the other type based on criteria......
I keep the problem is with the datatypes when converting between the control types, I have tried all sorts of different ways to convert value first, controls first, etc but nothing is working 100%.....
currently, I have the SP returning the string False in a column then in using this with the criteria to create a checkbox..... it works fine but the Value remains a string even after converting it to a Boolean, the datatype is also a Boolean on the checkbox but the value is String..... this has gone over my head now and I'm at a loss......
NewCntrl2 = New DataGridViewCheckBoxCell
NewCntrl2.Value = Convert.ToBoolean(DGV.Cells(0).Value)
NewCntrl2.ValueType = GetType(System.Boolean)
DGV.Cells(0) = NewCntrl2
this is the code converting the textbox-column cell to a checkbox cell
any ideas why the value of the checkbox is still a string ('False')...
the problem with what it's doing now is when I handle the cell click event I cannot check or uncheck the box using the Not Value technique
----EDIT----
This is the sub I'm using, it creates several different types of controls.
Public Sub posted_CreateControls()
Dim Cell_0 As String
Dim Cell_1 As String
Dim Cell_2 As String
Dim NewCntrl As DataGridViewButtonCell
Dim NewCntrl2 As DataGridViewCheckBoxCell
For Each Rw As DataGridViewRow In dgvPosted.Rows
With Rw
Cell_0 = .Cells(1).Value.ToString
Cell_1 = .Cells(2).Value.ToString
Cell_2 = .Cells(3).Value.ToString
'this column starts with a value 'False' returned by the SP,
'we don't want checkboxes on all rows and using false string was the only method
'i could find to easily do this
'if both assign and unassign id are present then we need a checkbox
Select Case Cell_0
Case String.Empty
.Cells(0).Value = String.Empty
Case Else
If Not Cell_1 = String.Empty Then
NewCntrl2 = New DataGridViewCheckBoxCell
NewCntrl2.Value = Convert.ToBoolean(.Cells(0).Value)
NewCntrl2.ValueType = GetType(System.Boolean)
.Cells(0) = NewCntrl2
Else
.Cells(0).Value = String.Empty
End If
End Select
'Create an Assign button for each row in columnindex 0(Assign) if ColumnIndex(2)(Edit) contains M, L or I
Select Case Cell_2
Case "M", "L", "I", "P"
NewCntrl = New DataGridViewButtonCell
.Cells(1) = NewCntrl
NewCntrl.Tag = Cell_0
NewCntrl.Value = "Assign"
End Select
'Create an UnAssign button in columnindex 1(UnAssign) if the value of columnindex 1(Unassign) is not empty
If Not Cell_1 = vbNullString Then
NewCntrl = New DataGridViewButtonCell
.Cells(2) = NewCntrl
NewCntrl.Tag = Cell_1
NewCntrl.Value = "UnAssign"
End If
'Create an Edit button on columnindex 2(Edit) if ColumnIndex 2(Edit) is not M, L or empty string
Select Case Cell_2
Case "M", "L", "P", vbNullString
'Do Nothing
Case Else
NewCntrl = New DataGridViewButtonCell
.Cells(3) = NewCntrl
NewCntrl.Tag = Cell_2
NewCntrl.Value = "Edit"
End Select
End With
Next
NewCntrl = Nothing
End Sub
This creates the checkboxes how I want them but the values remain as a string that is causing the cell. Click event to fail because I'm trying to set the checkbox value to true and the .value = not .value part is failing because for some reason the value remains a string but the checkboxes value type is in fact Boolean.....
Instead of returning false try returning:
`select CAST(0 as bit) as somefield`
Which will return a boolean field. I'd also recommend adding a check box column instead of adding the individual cells.
This code is translating all the words in a cell, but only the first should be forced to Proper case, the other words should keep the case written by the user, but instead it is forcing the first word to proper case and all the other words in the cell to lower case. All the other words should mantain its original case.
Sub TraAdd()
Dim translate As Object 'scritping.Dictionary
Set translate = CreateObject("Scripting.Dictionary")
translate("modens") = "modems"
translate("Modens") = "Modems"
translate("modens,") = "modems,"
translate("Modens,") = "Modems,"
translate("Fruteira,") = "Fruit bowl,"
translate("fruteira,") = "fruit bowl,"
translate("Fruteira") = "Fruit bowl"
translate("fruteira") = "fruit bowl"
translate("muletas") = "crutches"
translate("Muletas") = "Crutches"
translate("muletas,") = "crutches,"
translate("Muletas,") = "Crutches,"
Dim Words As Variant
Dim I As Integer
Words = Split(LCase(activecell.Value))
For I = LBound(Words) To UBound(Words)
If translate(Words(I)) <> "" Then Words(I) = translate(Words(I))
Next
activecell.Value = Join(Words)
activecell.Value = Ucase$(Left$(activecell.Value, 1)) & Right$(activecell.Value, Len(activecell.Value) - 1)
End Sub
Any ideas?
You have made all of your content lowercase when you split it into an array.
Remove LCase when you split the cell content to Words and it should work as you intend:
Words = Split(activecell.Value)
What is shortcut of checking string value like this.
If midtxt = "a" Then
midtxt = "apple"
ElseIf midtxt = "b" Then
midtxt = "ball"
ElseIf midtxt = "c" Then
midtxt = "cat"
.....
ElseIf midtxt = "z" Then
midtxt = "zebra"
End If
MsgBox midtxt
Is there any way I can do this using two arrays.
[a, b, c....z] and [apple, ball, cat.....zebra]
Edit
I need reproducible function for my task.
I think a for apple is not right example for me.
This is updated array for me.
[ap, bl, ca,... zr] [apple, ball, cat... zebra]
means the two letter code is derived from the corresponding string but it is not uniformly derived.
A dictionary may be worthwhile here, as long as the [a, b, ...z] set is unique.
In the VBA IDE, go to Tools, References, and select Windows Scripting Runtime.
Public gdctAnimals As Dictionary
Public Sub SetUpAnimalDictionary()
Set gdctAnimals = new Scripting.Dictionary
gdctAnimals.Add "a", "apple"
gdctAnimals.Add "b", "ball"
gdctAnimals.Add "c", "cat"
gdctAnimals.Add "z", "zebra"
End Sub
Public Sub YourProc(midtxt As String)
If gdctAnimals Is Nothing Then
SetUpAnimalDictionary
End If
If gdctAnimals.Exists(midtxt) Then
MsgBox gdctAnimals(midtxt)
Else
MsgBox "Item not found in dictionary", vbExclamation
End if
End Sub
Use the Select Case or Switch function
Function SwapString(strInput As String)
SwapString= Switch(strInput = "a", "Apple", strInput = "b", "Banana", strInput = "c", "Cherry")
End Function
In your case, if you can only have 26 combinations (a-z) the easiest way is to do this:
Public Function ReturnString(strIn As String) As String
Select Case strIn
Case "a"
ReturnString = "apple"
Case "b"
ReturnString = "ball"
Case "c"
ReturnString = "cat"
' .............
Case Else
ReturnString = "UNKNOWN"
End Select
End Function
and you call your fonction like this
MyLongString = ReturnString "a"
But there are many more possibililities that I won't detail because you have not detailed enough your question:
You can use 2 arrays or a 2D array
you can use an array of private types
you can use a dictionary as specified in another answer
No need for an external component or tedious population, you are looking for something based on an ordinal value; a=>z is the character code range 97=>122 so you can use a simple efficient array lookup by converting the character code to a value within the bounds of the array:
'//populate (once)
Dim map() As String: map = Split("apple,ball,cat,...,zebra", ",")
'//lookup
midtxt = "a"
midtxt = map(Asc(Left$(midtxt, 1)) - 97)
'=>apple
midtxt = "c"
midtxt = map(Asc(Left$(midtxt, 1)) - 97)
'=>cat
If needed check the value starts with a character first with if midtxt like "[a-z]*" then ...
This is probably a simple one, but I can't seem to find out what's wrong:
if temp_array(1) = temp_string2 & temp_array(2) = "w" then
....
end if
the values are:
temp_array(1) = "test1"
temp_string2 = "test2"
temp_array(2) = "w"
I get a type mismatch error highlighting the conditional comparisons...
& is string concatenation, not logical and.
if temp_array(1) = temp_string2 And temp_array(2) = "w" then
I am creating a for each loop to take the words from a string and place them each into a text box. The program allows for up to "9" variables What I am trying to attempt is.
Foreach word in Words
i = i +1
Varible & i = word
txtCritical1.Text = variable & i
any ideas on a way to make this work?
Have a look through the MSDN article on For Each. It includes a sample using strings.
https://msdn.microsoft.com/en-us/library/5ebk1751.aspx
So i went with a simple if statement this does the trick. Each text box is filled in.
Dim details As String = EditEvent.EditDbTable.Rows(0).Item(13).ToString()
Dim words As String() = details.Split(New Char() {"«"})
Dim word As String
For Each word In words
i = i + 1
v = word
If i = 1 Then
txtCritical1.Text = v
ElseIf i = 2 Then
txtCritical2.Text = v
ElseIf ....
ElseIf i = 9 then
txtCritical2.text = v
Else
....
End If
Next