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
Related
I did some activity but I cant figure out how to retrieve data from listbox1 to textbox1. Example in the listbox1 there are 4 names: John, Jorge, Joe. Then I want to transfer Joe from listbox1 to textbox1. I did an arraylist where I adding those 3 names in the listbox1 but I didn't know how to retrieve the name "Jorge" from listbox1 to textbox1. Send help.
Here's the code where I try to retrieve one of the name from listbox1 to textbox1
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Here's the whole code
Public Class Form1
Dim ArrayofNames() As String
Dim x As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Private Sub Addbtn_Click(sender As Object, e As EventArgs) Handles Addbtn.Click
Dim x As Integer = 0
ReDim ArrayofNames(x)
For x = 0 To ArrayofNames.Length - 1
ArrayofNames(x) = Addtextbox.Text
ListBox1.Items.Add(ArrayofNames(x))
Next
End Sub
Private Sub removeBtn_Click(sender As Object, e As EventArgs) Handles removeBtn.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
End Class
Here's the Image of the interface i try to retrieve the name Joe but it wasn'tshowing
Let's go over the code you posted.
In the retrieve button click event you are comparing and Integer to a String with textbox1.Text = ListBox1.Items.Count This won't compile with Option Strict On. You do have Option Strict On, don't you? You should always have this on.
On the next line, you assign the value of ArrayofNames(x), where x refers to your form level variable, to a text box's Text property. Since the value of x is never changed anywhere in the code this will always return the 0 index in the array. (The first element) I can't imagine why it should matter that the Textbox.Text should equal the count of the ListBox items.
In the add button click event you first declare a local variable x. Any code in the method will use this x, not the form level x. You ReDim (without the Preserve keyword) the array to an array with a single element. Your For loop is silly because the length of ArrayofNames is 1, so it is 0 To 0.
The ArrayofNames will never have more than a single element and it will be overwritten each time the add button is clicked.
Fortunately the Listbox maintains its own collection of items.
We can use this collection to simplify your code. Just add the contents of the text box to the the list items.
To retrieve a value from the list box you must first determine if the user has entered a valid index. First, is the entry a valid Integer and is it an index present in the list box.
Remember that .net collections start at index 0.
I use default names in my test program but you should continue to use meaningful names for your controls.
'Your add button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim index As Integer
If Integer.TryParse(TextBox1.Text, index) AndAlso index >= 0 AndAlso ListBox1.Items.Count - 1 >= index Then
TextBox2.Text = ListBox1.Items(index).ToString
End If
End Sub
'Your add button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox3.Text)
End Sub
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
I work on a new project that need a multiple row selection/deselection by the user in a datagridview with only a tap on a touch screen.
The form should look like this:
For exemple, if the user want to delete row 2 and 5, he only need to tap once on each line to select/deselect them. After the selection is done, he tap on "Delete Row" button.
I've already try to play with the CellClick event without success!!
Can someone have a clue how can I handle this problem?
After setting MultiSelect property to True and SelectionMode to FullRowSelect you can use a List to store which row of your DataGridView is selected.
On CellClick you can add/remove rows from your List, on RowPostPaint you can select a row if it's included in the List and on RowsRemoved you have to clear the List.
Private intSelectedRows As New List(Of Integer)
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
With CType(sender, DataGridView)
Dim intRow As Integer = .CurrentRow.Index
If Not Me.intSelectedRows.Contains(intRow) Then
Me.intSelectedRows.Add(intRow)
Else
.CurrentRow.Selected = False
Me.intSelectedRows.Remove(intRow)
End If
End With
End Sub
Private Sub DataGridView1_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
If Me.intSelectedRows.Contains(e.RowIndex) Then
CType(sender, DataGridView).Rows(e.RowIndex).Selected = True
End If
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
Me.intSelectedRows.Clear()
End Sub
If you want to clear selection you can use this code:
Private Sub btnClearSelectedRows_Click(sender As System.Object, e As System.EventArgs) Handles btnClearSelectedRows.Click
For Each intSelectedRow As Integer In Me.intSelectedRows
Me.DataGridView1.Rows(intSelectedRow).Selected = False
Next intSelectedRow
Me.intSelectedRows.Clear()
End Sub
How to change state of multiple CheckBoxes in DataGridView by pressing Shift key and select the checkbox.
Is it possible to do it in vb.net?
Thanks in advance.
Here's an example of how that could be accomplished. For this example, I just created a datagridview with two columns, one text and one checkbox, and added 5 rows to it. The key is the datagridview_click event. When that event gets called, it will check if the shift key was being held down with the click. If it was, it will uncheck every datagridviewcheckboxcell in your selection:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim c1 As New DataGridViewTextBoxColumn
DataGridView1.Columns.Add(c1)
Dim c2 As New DataGridViewCheckBoxColumn
DataGridView1.Columns.Add(c2)
For i As Integer = 1 To 5
DataGridView1.Rows.Add("test" & i.ToString, True)
Next i
End Sub
Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
If My.Computer.Keyboard.ShiftKeyDown Then
For Each cell As DataGridViewCell In DataGridView1.SelectedCells
If TypeOf cell Is DataGridViewCheckBoxCell Then cell.Value = False
Next
DataGridView1.RefreshEdit()
End If
End Sub
I have three columns, first column is textbox, second column is checkbox, third column is textbox. I want to add a click event to the third column where if a user click on that cell, it will automatically checkmark and uncheckmark the second checkbox column of that row. I tried this but it's not working.
AddHandler datagridview1.MouseClick, AddressOf form1.datagridview1_MouseClick
just need to switch your Handle type on the subroutine to "Handles DataGridView1.CellClick". Example:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim tempView = DirectCast(sender, DataGridView)
For Each cell As DataGridViewTextBoxCell In tempView.SelectedCells
If cell.ColumnIndex = 1 Then
Dim tempCheckBoxCell As DataGridViewCheckBoxCell = tempView("column1", cell.RowIndex)
tempCheckBoxCell.Value = True
End If
Next
End Sub
Also, quick note - you will need to adjust the cell type found in the for each loop to whatever type of cell you are using; in the example I had column2 set to a simple textbox type cell.
Look at the CellClick event. https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick(v=vs.110).aspx
Something like:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With DataGridView1
.Rows.Add({"John Smith", 1})
.Rows.Add({"Jane Doe", 0})
End With
AddHandler DataGridView1.CellClick, AddressOf DataGridView1_CellClick
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs)
If e.RowIndex < 0 Then Exit Sub
Dim dgv As DataGridView = CType(sender, DataGridView)
If Not TypeOf dgv.Rows(e.RowIndex).Cells(e.ColumnIndex) Is DataGridViewCheckBoxCell Then Exit Sub
Dim cell As DataGridViewCheckBoxCell = CType(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewCheckBoxCell)
cell.Value = Not CBool(cell.Value)
dgv.EndEdit()
End Sub
End Class