Issue with checkbox and textbox - vb.net

I'm having a little issue with my code not displaying correctly. Right now if there's text in the textbox and I select something from the checkbox list, what I selected from the checkboxlist overrides what's in the textbox. I want to keep what's in the textbox and just keep adding on what's selected.
For example: Honda's in the textbox ... I select Dodge and Mazda I want to show
Honda, Dodge, Mazda
Dim i As Integer = 0
Dim strText As String = ""
For i = 0 To cbCars.Items.Count - 1
If cbCars.Items(i).Selected Then
If strText = "" Or strTeethText = Nothing Then
strText += cbTeeth.Items(i).Text
Else
strText += ", " & cbCars.Items(i).Text
End If
End If
Next
txtCars.Text = strText.ToString()

Try
txtCars.Text += strText;
or
txtCars.AppendText(strText);

Change
Dim strText As String = ""
to
Dim strText As String = txtCars.Text
You forgot to initialize your string to the value of the textbox, which is why the textbox was getting overwritten on your click handler.

Related

how to check checklistbox items using datagridview vb.net?

I'm just a beginner for coding and I want to programmatically check items in checklistbox using datagridview.
Data grid view values are seperated with commas like this jhon,Metilda,saman,.
Checklistbox name as chklistinput and please help me to solve this ?
'Full coding is here..............................
Private Sub TextBox10_TextChanged(sender As Object, e As EventArgs) Handles TextBox10.TextChanged
'this is ok and searching as I want
Dim SearchV As String = TextBox10.Text
SearchV = "%" + TextBox10.Text + "%"
Me.PassIssuingRecordTableAdapter.FillBy(Me.Database4DataSet.PassIssuingRecord, SearchV)
'But the problem bigins here
Dim areasback As String = DataGridView1.Rows(0).Cells(6).Value.ToString
Dim areasback1 As String() = areasback.Split(",")
For Each x In areasback1
For i = 0 To areasback.Count - 1
If chklistInput.Items(i).ToString() = x.ToString() Then
chklistInput.SetItemChecked(i, False)
End If
Next
Next
End Sub
You have to loop over chklistInput.Items.Count - 1 instead of areasback.Count - 1
use the following code:
Dim areasback As String = DataGridView1.Rows(0).Cells(6).Value.ToString
Dim areasback1 As String() = areasback.Split(",")
Dim intCount as integer = 0
For each str as string in areasback1
For intCount = 0 To chklistInput.Items.Count - 1
If chklistInput.Items(intCount).ToString() = str Then
chklistInput.SetItemChecked(intCount , True)
End If
Next
Next
chklistInput.Refresh()
Note: comparing is case sensitive

Multiline Textbox to Datagridview

I have a multi-line textbox with this appearance:
A#B
C#
D#E
and I want to populate my datagridview to something like this:
_________
|A|BC|D|E|
I mean, I want to split when there's a "#" , but I don't want multi-line cells in datagridview.
I tried this code:
Dim sup() As String = TextBox1.Text.Split(vbCr, vbLf, vbTab, " "c, "#")
DataGridView1.Rows.Add(sup(0), sup(1), sup(2),sup(3))
but it says it goes outta bounds ... Thanks!
edit:
"Index out of range exception" Error.
If i paste the textbox values to microsoft word they come like this:
If you are sure you will be splitting your textbox with 3 #'s, you could use something like this after creating 3 columns in your datagridview
Dim myStr As String
Dim substring As String
Dim strArray() As String
Dim columnInt as Integer = 0
myStr = Textbox1.Text
strArray = myStr.Split("#")
For i = 0 to strArray.Length - 1
Datagridview.Rows(0).Cells(columnInt).Value = strArray(i)
columnInt += 1
next
As I am not quite sure how you want this data to appear exactly, you may also need to declare the count of your columns should it be larger than 3. Add this code before the first For Statement:
For i = 0 to strArray.Length - 1
DataGridView.Columns.Add("YourText","YourText")
Next
Untested but it should get you in the right spot!
*Edit: Updated after testing
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
If DataGridView1.RowCount = 1 Then DataGridView1.Rows.Clear()
Call dgvstuff()
Timer1.Stop()
End Sub
Sub dgvstuff()
Dim sup2 = TextBox2.Text.Replace("#", "").Replace(">", " "c)
Dim sup() = sup2.Split(" "c, "#", vbCrLf, vbTab)
With DataGridView1
.Rows(0).Cells(0).Value = sup(1).ToString
.Rows(0).Cells(1).Value = sup(7).ToString
.Rows(0).Cells(3).Value = sup(4).ToString
End With
End Sub
I really don't know why but it works like this. Anyone knows a better/cleaner way? Tks

Showing the difference between two RichTextBox controls

I'm trying to compare both richtextbox text and show the difference into the 3rd richtextbox. After i do some changes to the code that i get from this forum, it still have some problems, which is there are words that are no different showing out at my 3rd richtextbox.... the right hand side of the rich text box is from a text file that have been checked in regex function before displayed in the box.
this is the source code that use for compare:
Dim txt1(DispBox.Text.Split(" ").Length) As String
Dim txt2(DispBox2.Text.Split(" ").Length) As String
txt1 = DispBox.Text.Split(" ")
txt2 = DispBox2.Text.Split(" ")
Dim diff1 As String = "" 'Differences between 1 and 2
Dim diff2 As String = "" 'Differences between 2 and 1
Dim diffPosition As Integer ' Set where begin to find and select in RichTextBox
diffPosition = 1 ' Initialize
For Each diff As String In txt1
If Array.IndexOf(txt2, diff.ToString) = -1 Then
diff1 += diff.ToString & " "
With DispBox
.Find(diff, diffPosition, RichTextBoxFinds.None) ' Find and select diff in RichTextBox1 starting from position diffPosition in RichtextBox1
.SelectionFont = New Font(.Font, FontStyle.Bold) ' Set diff in Bold
.SelectionColor = Color.Blue ' Set diff in blue instead of black
.SelectionBackColor = Color.Yellow ' highlight in yellow
End With
End If
diffPosition = diffPosition + Len(diff) ' re-Initialize diffPostion to avoid to find and select the same text present more than once
Next
DispBox3.Visible = True
DispBox3.Text = diff1
this is my upload button code to check the regex function
Dim result As DialogResult = OpenFileDialog1.ShowDialog()
' Test result.
If result = Windows.Forms.DialogResult.OK Then
' Get the file name.
Dim path As String = OpenFileDialog1.FileName
Try
' Read in text.
Dim text As String = File.ReadAllText(path)
Dim postupload As String = Regex.Replace(text, "!", "")
DispBox2.Text = postupload
' For debugging.
Me.Text = text.Length.ToString
Catch ex As Exception
' Report an error.
Me.Text = "Error"
End Try
End If
because inside the text file there will be "!" between the line, I would like to replace the "!" with "breakline/enter".
My problem is :
why the "Building & hostname" words count as wrong words.
why the new word that display in 3rd richtextbox is not in new line if the words is found in the middle of the line.
the other wrong words are not color, bold n highlight.....
Your code is splitting all the words based on a space, but it's ignoring the line breaks, so it makes "running-confing building" look like one word.
Try it this way:
Dim txt1 As String() = String.Join(" ", DispBox.Lines).Split(" ")
Dim txt2 As String() = String.Join(" ", DispBox2.Lines).Split(" ")

Reference an object using string (the object is under the tab control)

I have a working code here that can put test to all textboxes from 1 to 10. The name of the textboxes are Tb_Hour_1, Tb_Hour_2, Tb_Hour_3, Tb_Hour_4, etc. up to 10.
Dim textboxes As TextBox
For i As Integer = 1 To 10
textboxes = Me.Controls("TB_Hour_" & i)
textboxes.Text = "Test" & i
Next
But the problem comes in when I put my text boxes under the tab control, by searching I've found out that by adding in I can specify the place of the object that I want to call. Here's the working code.
For Each c As TextBox In TabPage1.Controls.OfType(Of TextBox)()
If c.Text = String.Empty Then c.Text = "a"
Next
My question is how can I integrate the 2 codes so I can have something like this
Dim textboxes As TextBox In TabPage1.Controls.OfType(Of TextBox)()
For i As Integer = 1 To 10
textboxes = Me.Controls("TB_Hour_" & i)
textboxes.Text = "Test" & i
Next
My VB is a little rusty, but it should be something like this. You can loop through all of the controls on the tab, look for ones that starts with "TB_Hour_", cast it to a textbox and do whatever it is you want to do.
For Each c As Control In TabPage1.Controls
If c.Name.StartsWith("TB_Hour_") Then
' it's a textbox
Dim tb as Textbox = DirectCast(c, Textbox)
' do whatever you're going to do
End If
Next
Now that you tucked the TextBoxes into a TabContol, don't use Me.Controls(). Instead try:
Dim textboxes As TextBox
For i As Integer = 1 To 10
textboxes = TabPage1.Controls("TB_Hour_" & i)
textboxes.Text = "Test" & i
Next
Otherwise, you can do something like:
Dim textboxes As TextBox
For i As Integer = 1 To 10
textboxes = Me.TabControl1.Controls("TabPage1").Controls("TextBox" & i)
textboxes.Text = "Test" & i
Next

tOGGLE cASE For Textbox?

How can I add tOGGLE cASE to textboxes, for example, I click a button and it changes the text in a textbox to tOGGLE cASE (hello -> hELLO), basically it takes first letter and lower cases it and the rest upper cases it.
Here is a method using .NET Culture functions to first convert to Title Case and then invert the case to your "tOGGLE cASE"
Private Sub btn_ConvertTotOGGLEcASE_Click(sender As Object, e As EventArgs) Handles btn_ConvertTotOGGLEcASE.Click
'Get the current value of the textbox
Dim MyText As String = MyTextBox.Text
'Convert it to Title Case using built in .NET tools
Dim MyTextInfo As System.Globalization.TextInfo = New System.Globalization.CultureInfo("en-US", False).TextInfo
MyText = MyTextInfo.ToTitleCase(MyText)
'Then invert the case of all the characters
Dim InvertedText As Char() = MyText.Select(Function(c) If(Char.IsLetter(c), If(Char.IsUpper(c), Char.ToLower(c), Char.ToUpper(c)), c)).ToArray()
'Finally convert it back to a string
MyTextBox.Text = New String(InvertedText)
End Sub
You can split you string in an array, iterate in the array with lcase(mid(string,1,1) & ucase(mid(string,2, len(string)-1)), and recompose your array in a string
Public function ToogleText(myStr as string) as string
dim str() as string
str = split(myStr," ")
dim toogleStr as string
toogleStr = ""
for each substr as string in str
toogleStr = toogleStr & lcase(mid(substr,1,1)) & ucase(mid(substr, 2,len(substr)-1)) & " "
next substr
if len(toogleStr) > 0 then
ToogleText = mid(toogleStr,1,len(toogleStr)-1)
else
ToogleText =""
end if
end function