vb.net - How to get value from each line in richtextbox and show each values into textboxes - vb.net

i have a question in my code, Question is
How to get value from each line in richtextbox and show each values into textboxes?
my code is :
Imports System.IO
Public Class Form1
Private Results As String
Private Sub UpdateText()
Dim xList As New List(Of KeyValuePair(Of String, String))
Results = Results.Replace(vbLf, "")
Dim LineSplit() As String = Results.Split(vbCr)
For Each xLine As String In LineSplit
If xLine <> "" Then
xList.Add(New KeyValuePair(Of String, String)(xLine.Split("=")(0), xLine.Split("=")(1).Trim.Replace(" ", "")))
End If
Next
'do some work here to put the values in the right textboxes
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
UpdateText()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Results = RichTextBox1.Text
End Sub
End Class
sorry for my bad english, i am from Indonesia, thanks..

Instead of List you could loop over controls of Textbox type.
If number of lines is Always equal to number of textboxes you could loop over your lines or textboxes or even do a static for x=0 to 11 then simply put line x into textbox x.
As we don't know how you named these textboxes I'll show you a way that should work for you:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim counter As Integer
For Each controlOnForm In Me.Controls
If TypeOf controlOnForm Is TextBox Then
if counter <12 then 'depending how you ordered them you may need to fix this sign
controlOnForm.text = TextBox1.Lines(counter).Split("=")(1)
counter += 1
end if
End If
Next
End Sub

Related

How change Compare Multiple Textbox

I have 2 codes to change the color to Textbox, and unfortunately none of them work. What's wrong here? Why the code is not good. Every time I try the code, not worked, The first code changes the color to Textbox if there is a value between 1 and 7, and the second changes the value in ascending order from the lowest to the highest and assigns a corresponding color. Please tell me if these 2 codes are written correctly, or there is a write error.
Code 1: Image: http://www.imagebam.com/image/5ac5ee1073004874
Public Class TextBoxColors
Private ColorTable As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
Public Sub New()
ColorTable.Add("1", Color.Red)
ColorTable.Add("2", Color.Aqua)
ColorTable.Add("3", Color.Chocolate)
ColorTable.Add("4", Color.BlanchedAlmond)
ColorTable.Add("5", Color.BurlyWood)
ColorTable.Add("6", Color.BlueViolet)
ColorTable.Add("7", Color.DarkBlue)
End Sub
Public Function GetColor(ColorMap As String) As Color
Return If(ColorTable.Keys.Contains(ColorMap), ColorTable(ColorMap), Color.White)
End Function
End Class
Private txtColor As TextBoxColors = New TextBoxColors()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each txtDraw As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(txt) txt.Name.StartsWith("txtDraw"))
AddHandler txtDraw.TextChanged,
Sub()
If Not String.IsNullOrEmpty(txtDraw.Text) Then
txtDraw.BackColor = txtColor.GetColor(txtDraw.Text)
End If
End Sub
Next
End Sub
Code 2: This 2nd code must be changed based on my text box, which starts with SumtxtDraw
- Image: http://www.imagebam.com/image/92a4091073004904
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillColorList()
End Sub
Private Sub ColorTextBoxes()
FillTextBoxList(16, 23)
Dim SortedList As List(Of TextBox) = SortList()
Dim index As Integer
For Each txt As TextBox In SortedList
txt.BackColor = lstColor(index)
index += 1
Next
End Sub
Private Sub FillColorList()
lstColor.Add(Color.Red) 'for lowest number
lstColor.Add(Color.BlanchedAlmond)
lstColor.Add(Color.PaleGreen)
lstColor.Add(Color.Chartreuse)
lstColor.Add(Color.CadetBlue)
lstColor.Add(Color.Orange)
lstColor.Add(Color.DarkMagenta)
lstColor.Add(Color.Violet) 'for highest number
End Sub
Private Sub FillTextBoxList(StartNumber As Integer, EndNumber As Integer)
lstTextBox.Clear()
For suffix = StartNumber To EndNumber
lstTextBox.Add(DirectCast(Controls("TextBox" & suffix.ToString), TextBox))
Next
End Sub
Private Function SortList() As List(Of TextBox)
Dim orderedList = From txt In lstTextBox Order By CInt(txt.Text) Descending Select txt '$"{scorer.Score} - {scorer.Name}"
Dim SortedList As List(Of TextBox) = orderedList.ToList
Return SortedList
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ColorTextBoxes()
End Sub

Change Color a Textboxes from the Lower of the Highest

I have 2 codes to change the color to Textbox, and unfortunately none of them work. What's wrong here? Why the code is not good. Every time I try the code, it tells me there are no variables. The first code changes the color to Textbox if there is a value between 1 and 7, and the second changes the value in ascending order from the lowest to the highest and assigns a corresponding color.
Images: http://www.imagebam.com/image/5ac5ee1073004874
http://www.imagebam.com/image/92a4091073004904
Code 1:
Public Class TextBoxColors
Private ColorTable As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
Public Sub New()
ColorTable.Add("1", Color.Red)
ColorTable.Add("2", Color.Aqua)
ColorTable.Add("3", Color.Chocolate)
ColorTable.Add("4", Color.BlanchedAlmond)
ColorTable.Add("5", Color.BurlyWood)
ColorTable.Add("6", Color.BlueViolet)
ColorTable.Add("7", Color.DarkBlue)
End Sub
Public Function GetColor(ColorMap As String) As Color
Return If(ColorTable.Keys.Contains(ColorMap), ColorTable(ColorMap), Color.White)
End Function
End Class
Private txtColor As TextBoxColors = New TextBoxColors()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each txtDraw As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(txt) txt.Name.StartsWith("txtDraw"))
AddHandler txtDraw.TextChanged,
Sub()
If Not String.IsNullOrEmpty(txtDraw.Text) Then
txtDraw.BackColor = txtColor.GetColor(txtDraw.Text)
End If
End Sub
Next
End Sub
Code 2: This 2nd code must be changed based on my text box, which starts with SumtxtDraw
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillColorList()
End Sub
Private Sub ColorTextBoxes()
FillTextBoxList(16, 23)
Dim SortedList As List(Of TextBox) = SortList()
Dim index As Integer
For Each txt As TextBox In SortedList
txt.BackColor = lstColor(index)
index += 1
Next
End Sub
Private Sub FillColorList()
lstColor.Add(Color.Red) 'for lowest number
lstColor.Add(Color.BlanchedAlmond)
lstColor.Add(Color.PaleGreen)
lstColor.Add(Color.Chartreuse)
lstColor.Add(Color.CadetBlue)
lstColor.Add(Color.Orange)
lstColor.Add(Color.DarkMagenta)
lstColor.Add(Color.Violet) 'for highest number
End Sub
Private Sub FillTextBoxList(StartNumber As Integer, EndNumber As Integer)
lstTextBox.Clear()
For suffix = StartNumber To EndNumber
lstTextBox.Add(DirectCast(Controls("TextBox" & suffix.ToString), TextBox))
Next
End Sub
Private Function SortList() As List(Of TextBox)
Dim orderedList = From txt In lstTextBox Order By CInt(txt.Text) Descending Select txt '$"{scorer.Score} - {scorer.Name}"
Dim SortedList As List(Of TextBox) = orderedList.ToList
Return SortedList
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ColorTextBoxes()
End Sub

Moving average method VB

i'd try to make moving average in vb
i want to check the cells and set the value to text box
but the result is all the text box has the same value
how to make my first check value (penjualan/bulan) is inputed into first text box and the second check (penjualan/bulan) to second text box.
here is my code
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = 5 Then
tb1.Text = DataGridView1.CurrentRow.Cells(3).Value
tb2.Text = DataGridView1.CurrentRow.Cells(3).Value
tb3.Text = DataGridView1.CurrentRow.Cells(3).Value
End If
End Sub
thanks.
You set EVERY time the cellClicked-event is raised all 3 Textboxes to the same value, CurrentRow.Cells(3).Value.
Another problem is that your code will set the text in the Textboxes always. It dont check if the Checkbox is checked or not. it just updated every time you click in any cell in this column, the text in the 3 boxes to the value of your currently selected row.
Here you have a solution. Its not perfect but should work, although you should try to understand and optimize it.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeDgv()
End Sub
Private Sub InitializeDgv ()
Dim row as String() = New String(){"2016",240}
DataGridView1.Rows.Add(row)
row = New String(){"2017",223}
DataGridView1.Rows.Add(row)
row = New String(){"2015",54}
DataGridView1.Rows.Add(row)
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
if e.ColumnIndex=2
Dim checkedRows=(From dgv as DataGridViewRow in DataGridView1.Rows where dgv.Cells(2).Value=True select dgv).ToList()
Dim controlsList As new List(of TextBox)
controlsList.Add(TextBox1)
controlsList.Add(TextBox2)
controlsList.Add(TextBox3)
for Each item in controlsList
item.Text=String.Empty
Next
for i=0 to checkedRows.Count-1
controlsList(i).Text=checkedRows.Item(i).Cells(0).Value
Next
End If
End Sub
End Class

Suggest Append Count in combobox (vb.net)

I'm searching for a way to count my remaining suggest appends in my combobox.
In my example I have a list of 7 items
When I start to type with the suggest append function, this list gets narrowed down. But I don't see any possibility to count these remaining appends.
What my main objective is, is that I do an action once I have only 1 suggest append remaining.
But I can only check on the selectedindex, which is in this case always -1, or my comboboxcount is still 7. I don't see a way to count the remaining suggest appends.
Any idea?
Supposing that your combobox listitems are of string type then this code will do that. First you should create a list of string with combobox items. Then on keyup event of combobox you should create the searchtext which you use to filter list then count. See code below (i have shown also searchtext just to see its value):
Dim lst As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each it In ComboBox1.Items
lst.Add(it)
Next
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
Dim seltext = ComboBox1.SelectedText
Dim searchtext = ""
If seltext <> "" Then
searchtext = ComboBox1.Text.ToLower.Replace(seltext, "")
Else
searchtext = ComboBox1.Text.ToLower
End If
Label1.Text = lst.Where(Function(d) d.ToLower.StartsWith(searchtext)).Count & " - " & searchtext
End Sub
If your combobox listitems are of different object type then you have to populate list with text field of listitem.
I had the same basic idea as Shurki, except I didn't use a list or replace the selected text with a zero-length string.
I use the SelectionStart property of the ComboBox to get a substring from the ComboBox's Text property..
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ComboBox1.Items.Add("Candy")
ComboBox1.Items.Add("Car")
ComboBox1.Items.Add("Crush")
ComboBox1.Items.Add("Canned")
ComboBox1.Items.Add("Can")
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
Dim query As IEnumerable(Of Object) =
From item As Object In ComboBox1.Items
Where item.ToString().ToUpper().StartsWith(ComboBox1.Text.Substring(0, ComboBox1.SelectionStart).ToUpper())
Select item
Debug.WriteLine("Number of items: " & query.Count())
End Sub
End Class

How to populate ListBox from Dictionary Values?

Net
I am attempting to create a function that will allow a user to input text into a RTB and if that text exists in a Dictionary as a Key then a listbox is populated by all the values of the dictionary whose key they are related to , each value populates the listbox in a new line.
the 1st line is highlighted and the user can press the enter button and replace the text in the RTB with the highlighted text .
I'm new to VB so I do not know much .
this is what I have so far.
Public Class Oxnay
Private Sub Oxnay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Tsort()
End Sub
Private TDictionary As Dictionary(Of String, String())
Public Sub Tsort()
TDictionary = New Dictionary(Of String, String())
TDictionary.Add("ape", {"pl", "tz", "xu"})
TDictionary.Add("lor", {"tv", "px"})
End Sub
Private Sub RichtextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
Dim lastword As String = RichTextBox1.Text.Split(" ").Last
If RichTextBox1.ContainsKey(lastword) Then
'display each string of the dictionary array related to lastword in different lines
'highlight first line
'Some[Code]
Else
ListBox1.Text = ""
End If
End Sub
End Class
For the first "lookup" part, try something like:
Private Sub RichtextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
Dim lastword As String = RichTextBox1.Text.Trim.Split(" ").Last
ListBox1.Items.Clear()
If Not IsNothing(TDictionary) AndAlso TDictionary.ContainsKey(lastword) Then
ListBox1.Items.AddRange(TDictionary(lastword))
End If
End Sub
Then to replace the currently selected text with the selection from the ListBox:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If ListBox1.SelectedIndex <> -1 Then
If RichTextBox1.SelectedText <> "" Then
RichTextBox1.SelectedText = ListBox1.SelectedItem.ToString
End If
End If
End Sub