How can I update a label's text when a row in the datagridview is changed? - vb.net

On my form I have a label (lblBalance) and I have a DGV which is populated from an Access database. If I add, delete or update a row, how can I refresh the label text so it reflects the new balance?
This is my frmMain code so far:
Public Class frmMain
Private Sub TransactionsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TransactionsBindingNavigatorSaveItem.Click
Me.Validate()
Me.TransactionsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CheckingDataSet)
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'CheckingDataSet.Transactions' table. You can move, or remove it, as needed.
Me.TransactionsTableAdapter.Fill(Me.CheckingDataSet.Transactions)
lblTotalCredits.Text = BalanceTotal().ToString("c")
End Sub
Private Function BalanceTotal() As Double
Dim tot As Double = 0
Dim i As Integer = 0
For i = 0 To TransactionsDataGridView.Rows.Count - 1
tot = tot + Convert.ToDouble(TransactionsDataGridView.Rows(i).Cells(3).Value)
Next i
Return tot
End Function
End Class

The BindingSource control has a ListChanged event you can try using to update your balance label.
Private Sub TransactionsBindingSource_ListChanged(_
sender As Object, _
e As ListChangedEventArgs) _
Handles TransactionsBindingSource.ListChanged
lblTotalCredits.Text = BalanceTotal().ToString("c")
End Sub

Related

Latency picking up click event

I am having a little problem with latency when I check a checkbox on and try to drag and drop. When I select one checkbox and try to move it over it won't move. If I have click that checkbox and click on a different row then try to move it will work. It works the same no matter how many I check it won't get the newest row without clicking somewhere else first. Do I need to add another event to handle or pick up that the box now has been checked?
Private Sub datagridview_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagridview.MouseDown
mouseDownPosition = e.Location
End Sub
Private Sub datagridview_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagridview.MouseMove
If CheckMouseMovement(sender, datagridview, e) Then
listofBuilds = New List(Of Build)
For Each row As DataGridViewRow In dataGridView.Rows
If Convert.ToBoolean(row.Cells.Item(0).Value) Then
Dim t As Build = DirectCast(row.DataBoundItem, Build)
listofBuilds.Add(t)
End If
Next
If listofBuilds.Count > 0 Then
dataGridView.EndEdit()
dataGridView.DoDragDrop(sender, dropEffect)
End If
End If
End Sub
Private Sub TabControl_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragEnter
e.Effect = DragDropEffects.All
End Sub
Private Sub TabControl_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TabControl.DragDrop
Dim DropPage As TabPage = GetTabPageByTab(TabControl.PointToClient(New Point(e.X, e.Y)))
If DropPage IsNot TabControl.SelectedTab Then
If DropPage Is Page1 Then
If DropPage Is Page2 Then
If DropPage Is Page3 Then
//etc
End If
End If
End Sub
Private Function GetTabPageByTab(ByVal point As Point) As TabPage
For i As Integer = 0 To TabControl.TabPages.Count - 1
If TabControl.GetTabRect(i).Contains(point) Then
Return TabControl.TabPages.Item(i)
End If
Next
Return Nothing
End Function
Adding a call to datagridview.EndEdit() at the beginning of the datagridview_MouseMove method will commit the current edit operation and update the source data so that you can see the updated value in your code.

loop through list box in vb and get a total amount

I’m trying to loop through a list of prices on a list box and print the total in a textbox on the form. The loop I have works, but the figures it produces are all wrong!. can any one help me??
the figures look ok but then when i add more items to the list box the total amount goes all wrong.
Public Class f
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'LoftDataSet.Services' table. You can move, or remove it, as needed.
Me.ServicesTableAdapter.Fill(Me.LoftDataSet.Services)
'Loop through all the rows that are in the dataset
For Each dr As DataRow In LoftDataSet.Services.Rows
Dim btn As New Button 'Instantiate a button
btn.Text = dr("service_name").ToString 'UserName is a field in my Users Table
btn.Size = New Size(140, 80)
btn.Tag = dr("ID") 'Here we set the tag to the primary key (ID)
'Since we're using a flowlayoutpanel, we don't need to worry about setting the location property
FlowLayoutPanel1.Controls.Add(btn) 'Add the button to the flow layout panel
AddHandler btn.Click, AddressOf UserClick 'Here we give the button a handler for the click event
Next
End Sub
Public Class Product
Public Property ProductName As String
Public Property ProductCost As Decimal
Public Overrides Function ToString() As String
Return ProductCost & " " & ProductName
'Here you could build a formatted string to the user to include cost
End Function
End Class
'Here we write our method for the click event of the button(s) we created
Dim SelectedProduct As New Product
Private Sub UserClick(ByVal sender As Object, ByVal e As EventArgs)
'We set a filter to the binding source passing it ID=<and whatever is stored in the tag property>
ServicesBindingSource.Filter = "ID = " & DirectCast(sender, Button).Tag.ToString
SelectedProduct.ProductName = DirectCast(sender, Button).Text
SelectedProduct.ProductCost = DirectCast(ServicesBindingSource(0), DataRowView).DataView(0)(2)
ListBox1.Items.Add(SelectedProduct)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For i As Integer = ListBox1.SelectedIndices.Count - 1 To 0 Step -1
ListBox1.Items.RemoveAt(ListBox1.SelectedIndices(i))
Next
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aValue As Decimal
For Each item As Product In ListBox1.Items
aValue += CDec(item.ProductCost)
Next
TextBox1.Text = aValue
End Sub
End Class
Cycle through each item using an integer as your count.
For i As Integer = 0 To ListBox1.Items.Count - 1
MsgBox(ListBox1.Items(i).ToString)
Next
Or you can add it to an integer/double value
For i As Integer = 0 To ListBox1.Items.Count - 1
MyInteger += Double.Parse.ListBox1.Items(i).ToString)
Next
MsgBox(MyInteger)
'Now the messagebox will show your total
EDIT:
Do not use ListBo1.Items.Item(i). Access directly through Listbox1.Items(i)

Removing item from listbox

I have a form that has 9 texbox that when I click on a certain button it adds whatever is in it to a listbox, I also have a remove button that removes an item from listbox, is there a way I can remove the item and clear the textbox it came from?
Remove item from ListBox:
ListBox1.Items.Remove(sItemtext)
ListBox1.Items.RemoveAt(indexItem)
Clear the TextBox:
TextBox1.Text = ""
Try this ..
Public Class Form1
Dim oLB As TextBox
Dim aList As New List(Of TextBox)
Sub GetLB(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
oLB = CType(sender, TextBox)
End Sub
Private Sub btnMoveToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveToList.Click
ListBox1.Items.Add(oLB.Text)
aList.Add(oLB)
End Sub
Private Sub btnRemoveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveItem.Click
Dim n As Integer = ListBox1.SelectedIndex
aList(n).Text = ""
ListBox1.Items.RemoveAt(n)
aList.RemoveAt(n)
End Sub
End Class

Get selected row of inactive datagridview

I have situation that have to know which exact row of datagridview1 (MultiSelect = False) is selected on another (inactive) datagridview1 when I am in some event handler of datagridview2 which is currently active.
I try a bunch of attempts but without correct result.
Private Sub DataGridView2_CellDoubleClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView2.CellDoubleClick
Dim myindex1 As Integer = DataGridView1.CurrentCell.RowIndex
OR
Dim myindex1 As Integer = DataGridView1.CurrentRow.Index
None of this don't work like it works when datagridview1 is active and I get index from their event handlers.
What should I do and how accuratelly get selected row of first datagridview from event handler of second datagridview?
EDIT:
Added code:
Private Sub datagridview1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
selrow1 = e.RowIndex
End Sub
Private Sub datagridview1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus
End Sub
Private Sub datagridview1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.GotFocus
DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.FromKnownColor(KnownColor.Highlight)
End Sub
Private Sub datagridview1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.LostFocus
DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.FromKnownColor(KnownColor.InactiveCaption)
End Sub

How can I count the number of times a button is pressed while an item is selected from a ListBox

The List Box has three candidates and a record Button. Every time the record button is hit I need it to add those button clicks for each candidate that is selected in the List Box. My code keeps counting all the clicks no matter which candidate I am selecting in the List Box. How can I differentiate between each selected item in the List Box.
Here is an image of how the application should look: http://i.imgur.com/N8zM2.jpg
Public Class Form1
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Dim candidatevotes(2) As Integer
Dim vote
Dim total
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
candListBox.Items.Add("Mark Stone")
candListBox.Items.Add("Sheima Patel")
candListBox.Items.Add("Sam Perez")
candListBox.SelectedIndex = 0
End Sub
Private Sub recordButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recordButton.Click
candidatevotes(vote) = candListBox.SelectedIndex
total += candidatevotes(vote)
Dim outfile As IO.StreamWriter
outfile = IO.File.AppendText("voteinfo.txt")
outfile.WriteLine(Convert.ToString(candListBox.SelectedItem))
outfile.Close()
End Sub
Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click
Dim infile As IO.StreamReader
If IO.File.Exists("voteinfo.txt") = True Then
infile = IO.File.OpenText("voteinfo.txt")
infile.Close()
End If
markLabel.Text = total.ToString
sheimaLabel.Text = total.ToString
samLabel.Text = total.ToString
End Sub
End Class
candidatevotes(vote) = candListBox.SelectedIndex
total += candidatevotes(vote)
should be
candidatevotes(candListBox.SelectedIndex) += 1
and
markLabel.Text = total.ToString
sheimaLabel.Text = total.ToString
samLabel.Text = total.ToString
should be
markLabel.Text = candidatevotes(0)
sheimaLabel.Text = candidatevotes(1)
samLabel.Text = candidatevotes(2)