How to display rowcount in a textbox in vb.net - vb.net

I am using Textbox1.Text = DataGridView1.RowCount() to display(in Textbox1) the total number of items in the datagridview on my vb.net app. Now i want Textbox2 to display the position at the datagridview. How will i do that? Eg I want something like 6 out of 50. So "6" will be displayed at Textbox1. Are you getting me?

You should handle that in the below event
Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
If dgv.CurrentRow isnot Nothing
Textbox1.Text = dgv.CurrentRow.Index + 1
End If
End Sub

For i As Integer = 0 To DataGridView1.RowCount
i = +i
Textbox1.Text = i
Next

Use DataGridView.CurrentRow.Index property.

Related

Click Button and Count Vb.Net

I want to Count how many times I click a button and display the number, but the format should be like: 0001 if reach 9999 restart to 0001 again and count till 9999.
I tried:
Private ButtonClickCount As Integer = 0
ButtonClickCount += 1
Label5.Text = "0000" & ButtonClickCount
And
Label5.Text = "0000" + Val(Label5.Text) + 1
This was the dumbest way I tried.lol
And the result was disapointing.
Is there any easy way to do it?
If you use a Static variable for the count, you can keep it in the button click event handler. (Static variables aren't used all that often, but it works here, assuming you don't need the variable anywhere else.)
If you check if the counter has reached 10000, you can set it back to 1.
You can format the number in the .ToString method.
Perhaps something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static clickCount As Integer = 0
clickCount += 1
If clickCount = 10000 Then
clickCount = 1
End If
lblClickCount.Text = clickCount.ToString("0000")
End Sub
You could use the Format command, which controls how many zeroes you want in the output:
Dim ButtonClicks As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ButtonClicks += 1
Label1.Text = Format(ButtonClicks, "0000")
End Sub
Dim ButtonClicks As Integer = 0
Private sub Button1_click(sender as object, e as EventArgs) handles Button 1.click
Buttonclick +=1
If buttonclick ="9999" then
Buttonlclick=1
Endif
Label1.text =format(Buttonclick, "0000")
End sub
You will need to keep track of the counter somewhere, in this case I would suggest storing the counter in the control's Tag property (documentation). In your button's click event, you would do the following:
Get the Tag
Convert the value to an Integer
Increment the value by 1
Check if the new value is greater than 9999
Optionally reset the value to 1 if 4 is true
Set the Text of the Button to the formatted result
Set the Tag of the button to the new value
Here is an example:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim clickedButton = DirectCast(sender, Button)
Dim tagValue = clickedButton.Tag
If (tagValue Is Nothing) Then
tagValue = 0
End If
Dim conversion As Integer
If (Not Integer.TryParse(tagValue.ToString(), conversion)) Then
conversion = 0
End If
conversion += 1
If (conversion > 9999) Then
conversion = 1
End If
clickedButton.Text = converstion.ToString("0000")
clickedButton.Tag = conversion
End Sub
Well, sorry guys, just realized that I could use the PadLeft function... And worked fine.
ButtonClickCount += 1
Label5.Text = ButtonClickCount.ToString().PadLeft(5, "0")
But Still don't know if it reaches 9999 will restart from 0001

Visual Basic Autotyper, Outputting listbox items

I'm trying to make a simple auto typer in Visual Basic. I want it to take the ListBox items and output at the user's choice of interval.
The problem is I don't know how to make the Timer's Tick event send each line and restart at the top of the list and continue to loop this way.
Form Design:
I have not listed much code because there really isn't much to list yet.
Private Sub intervalTimer_Tick(sender As Object, e As EventArgs) Handles intervalTimer.Tick
End Sub
Here is a code, but you need to replaced the ListBox with a ListView, it is the same look, this is just for the code to work.
This code is going to take each line from the ListView and output it using MsgBox using a timer that has an interval of user's choice, just remove the message box and output the result where ever you need, and you're all good :
First define this variable in your public class :
Dim ListedItems As String
Here is when you press the set button to set the interval :
Try
intervalTimer.Interval = intervalTextBox1.Text * 1000
Catch
MsgBox("The interval must be in purly seconds only!")
End Try
Here is when you press the start button :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each LVI As ListViewItem In ListView1.Items
If ListedItems = "" Then
ListedItems = LVI.Text
Else
ListedItems &= vbLf & LVI.Text
End If
Next
Timer1.Start()
End Sub
Here is when the timer tick event :
Private Sub intervalTimer_Tick(sender As Object, e As EventArgs) Handles intervalTimer.Tick
intervalTimer.Stop()
Dim SeprateLine As String
Dim Separator As Integer
If ListedItems.Contains(vbLf) Then
Separator = ListedItems.IndexOf(vbLf)
SeprateLine = ListedItems.Remove(Separator)
ListedItems = ListedItems.Substring(Separator + 1)
Else
SeprateLine = ListedItems
ListedItems = ""
End If
MsgBox(SeprateLine)
If ListedItems <> "" Then
intervalTimer.Start()
End If
End Sub
Finally i hope that this will help you with what you need :)

how to read characters one by one in textbox in vb.net

I want to read the value given by user in text box in vb.net.
if they give value ABC , i want to read the string one by one and display the particular character(ex. 3rd character(c)) in another text box.
Assuming you have 2 text boxes and a button:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim textBox1InputString As String
textBox1InputString = TextBox1.Text
' Call ToCharArray function.
Dim array() As Char = textBox1InputString.ToCharArray
' If we input ABC and we want C
TextBox2.Text = array(2)
End Sub
For i = 0 To TextBox1.Text.Length
If i = 2 Then
TextBox2.Text = TextBox1.Text(i)
End If
Next

How to use CellEndEdit event in datagridview using VB.Net?

In the data grid,I have two columns A and B.
I need only one column to be filled in a row.
In this,after filling column A,if i try to fill column B or vice-verse, I need the other column to be empty.
Note: I am filling the grid manually.
Please help me out of this and thanks in advance.
The DataGridViewCellEventArgs parameter you get from that event tells you which column and row you are currently editing, so you can easily wipe out the other column with that information:
Private Sub dgv_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) _
Handles dgv.CellEndEdit
If e.ColumnIndex = 0 Then
dgv.Rows(e.RowIndex).Cells(1).Value = String.Empty
ElseIf e.ColumnIndex = 1 Then
dgv.Rows(e.RowIndex).Cells(0).Value = String.Empty
End If
End Sub
To do this when the user starts to edit the cell, simply use the CellBeginEdit event instead:
Private Sub dgv_CellBeginEdit(sender As Object, _
e As DataGridViewCellCancelEventArgs) _
Handles dgv.CellBeginEdit
If e.ColumnIndex = 0 Then
dgv.Rows(e.RowIndex).Cells(1).Value = String.Empty
ElseIf e.ColumnIndex = 1 Then
dgv.Rows(e.RowIndex).Cells(0).Value = String.Empty
End If
End Sub

checkbox from datagridview to textbox

A new issue !!
I have a checkbox column in the datagridview and also a column named "partqty" which displays the quantity of materails available.. I would want that when the user checks on the checkbox, the quantities of checked rows should be added and displayed in the textbox..
I tired something like this.. But it displays 0(zero) in the textbox.. Please help..
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'For Each _rw As DataGridViewRow In dataGridView1.Rows
' If _rw.Cells(0).Value = True Then
Dim totalSum As Integer
For i As Integer = 0 To HemDatabase1DataSet5.Tables(0).Rows.Count - 1
totalSum += HemDatabase1DataSet5.Tables(0).Rows(i).Item("partqty")
Next
textBox5.Text = totalSum.ToString()
' End If
'Next
End Sub
Hey !! This is something i could think upon trying !! however, there is no error during compile time, but there is an error at runtime, which says :
Operator '+' is not defined for type 'DBNull' and type 'Boolean'
Here is the code i tried :
For Each _rw As DataGridViewRow In dataGridView1.Rows
If _rw.Cells(0).Value = True Then
Dim totalSum As Integer
For i As Integer = 0 To dataGridView1.Rows.Count - 1
totalSum += dataGridView1.Rows(i).Cells(5).Value
Next
textBox5.Text = totalSum.ToString()
End If
Next
It gives error on this line :
totalSum += dataGridView1.Rows(i).Cells(5).Value
You first problem is that you are trying to add apples and oranges together.
In the line:
totalSum += dataGridView1.Rows(i).Cells(5).Value
totalSum is an integer while the Value property of a DataGridView cell is of type object.
This is a similar problem to what you see when trying to use the DataTable except there your Item property is givin you a DBNull rather than an object back.
Here is the code that you want (I'm mainly a C# dev so the VB.Net might be lacking elegance but it works).
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim sum As Integer
sum = 0
For Each row As DataGridViewRow In DataGridView1.Rows
Dim current As Integer
If Integer.TryParse(row.Cells("OrderQty").Value.ToString, current) Then
sum += current
End If
Next
TextBox1.Text = sum.ToString
End Sub
Some things worth noting about that code:
I use a For Each rather than a for. The two perform basically identically but I find the foreach more readable
I use Integer.TryParse on the cell value before using it. This is safer than just casting the cell value directly.
Rather then using column indexes which is a little fragile I use column names.
With your check on whether or not the checkbox is checked you can do something similar:
Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb)
One final point to note is that if you try to execute this code in the cellclick method of the DataGridView you will run into problems - the checkbox state is not committed until after the cellclick, so the most recently checked cell will not get added to your count. Instead you will want to handle the CellDirtyStateChanged and commit the edits, then doing the sum in the CellValueChanged handler.
To illustrate that try this code:
Private Sub dgv_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim sum As Integer
sum = 0
Dim cb As Boolean
cb = False
If DataGridView1.Columns(e.ColumnIndex).Name <> "IsChecked" Then
Return
End If
For Each row As DataGridViewRow In DataGridView1.Rows
If Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb) Then
If cb Then
Dim current As Integer
If Integer.TryParse(row.Cells("OrderQty").Value.ToString, current) Then
sum += current
End If
End If
End If
Next
TextBox1.Text = sum.ToString
End Sub
The sum will always lag one click behind (actually in VB.Net it appears to just nor really work - but I'm more a c# dev so that could be me missing something).
Instead you want:
Sub dataGridView1_CurrentCellDirtyStateChanged( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles DataGridView1.CurrentCellDirtyStateChanged
If DataGridView1.IsCurrentCellDirty Then
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
' If a check box cell is clicked, this event handler disables
' or enables the button in the same row as the clicked cell.
Public Sub dataGridView1_CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
If DataGridView1.Columns(e.ColumnIndex).Name = "IsChecked" Then
Dim sum As Integer
sum = 0
Dim cb As Boolean
For Each row As DataGridViewRow In DataGridView1.Rows
If Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb) Then
If cb Then
Dim current As Integer
If Integer.TryParse(row.Cells("OrderQty").Value.ToString, current) Then
sum += current
End If
End If
End If
Next
TextBox1.Text = sum.ToString
End If
End Sub
This is the code that worked..
Dim iSselected As Boolean
Dim CurrentCellValue As String
Dim CumulativeSummer As Integer
Dim i As Integer
With Me.dataGridView1
For i = 0 To .RowCount - 1
iSselected = .Rows(i).Cells(0).Value
CurrentCellValue = .Rows(i).Cells(5).Value
If CurrentCellValue = "" Then CurrentCellValue = 0
If iSselected = True Then
CumulativeSummer += Convert.ToInt32(CurrentCellValue)
End If
Next
End With
Me.textBox5.Text = CumulativeSummer
Thank u for your help !! :)