Groupbox lostfocus event

I have a form with 12 groupboxes. Each groupbox has seven textboxes that need to total and calculate a percent. It is going to entail 12 subs that do calculations and 84 textbox.lostfocus events to call one of the 12 subs for calculations.

I would rather tell it to do the calulation when any textbox in a groupbox looses focus or the focus ends up out of a groupbox.

Is there someway to say

dim ctrl as groupbox3.control

then on ctrl.lostfocus, do a calculation

OR

on groupbox3.lostfocus do calculation3

I have set a stop in the groupbox3.lostfocus event but I don't know how to make it fire. I am just being lazy, I don't want to code 84 places to do the same thing.

Thanks Rich




Answer this question

Groupbox lostfocus event

  • Alle

    That's pretty cool Dave. Unfortunatly all of the textboxes already exist and have names that correspond to fields in a table.

    BUT

    That "AddHandler .Leave, AddressOf SumTotals" opened up an avenue that will save me much code!

    Thanks

    Rich



  • Disk4mat

    Have a look at the following which may give you some ideas. Just paste the whole lot into a new project.

    Public Class Form1
    Dim GB(11) As GroupBox
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim F As New Font("Arial", 10, FontStyle.Bold)
    Me.WindowState = FormWindowState.Maximized
    For count As Integer = 0 To 11
    GB(count) = New GroupBox
    GB(count).Name = "GB" & (count + 1).ToString
    GB(count).Size = New Size(200, 200)
    GB(count).Location = New Point(50 + 250 * CInt(Int(count / 3)), 20 + 220 * (count Mod 3))
    GB(count).Text = (count + 1).ToString
    Me.Controls.Add(GB(count))
    Next
    For Each Box As GroupBox In Me.Controls
    For count As Integer = 1 To 7
    Dim T As New TextBox
    With T
    .Size = New Size(100, 20) : .Location = New Point(50, 20 + 20 * (count - 1)) : .Font = F
    AddHandler .Leave, AddressOf SumTotals
    End With
    Box.Controls.Add(T)
    Next
    Dim L As New Label
    With L
    .Location = New Point(50, 20 + 20 * 7) : .AutoSize = False : .BackColor = Color.Yellow : .Name = "Total" : .TextAlign = ContentAlignment.MiddleCenter
    End With
    Box.Controls.Add(L)
    Next
    End Sub
    Private Sub SumTotals(ByVal sender As Object, ByVal e As EventArgs)
    Dim Sum As Integer
    For Each B As Control In Me.Controls
    If TypeOf B Is GroupBox Then
    Sum = 0
    For Each C As Control In B.Controls
    If TypeOf C Is TextBox And IsNumeric(C.Text) Then
    Sum = Sum + CInt(C.Text)
    End If
    Next
    B.Controls("Total").Text = Sum.ToString
    End If
    Next
    End Sub
    End Class


  • Groupbox lostfocus event