Solved

User control event fires twice

Posted on 2012-03-23
9
1,119 Views
Last Modified: 2012-04-04
In my vb.net windows forms program I have the following code that creates multiple text boxes at runtime.

                'Scrap Qty Textbox
                Dim txtTemp2 As New TextBox
                With txtTemp2
                    .Name = "txtItemQty" & intRowCounter
                    .Location = New System.Drawing.Point(215, (intRowPos * intRowCounter) + 15)
                    .Size = New System.Drawing.Size(60, 30)
                    .AutoSize = False
                End With
                AddHandler txtTemp2.Enter, AddressOf txtItemQty_Enter
                pnlData.Controls.Add(txtTemp2)

Open in new window


As you can see, it adds the event handler to fire txtItemQty_Enter when the txtbox is "entered".

Here is the code for that subroutine..

    Private Sub txtItemQty_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWONumber.Enter
        Try
            _CurrentTextBox = sender
                pnlData.Controls.Add(UcNumPad1)
                UcNumPad1.Location = New System.Drawing.Point(280, _CurrentTextBox.Location.Y + 20)
            UcNumPad1.BringToFront()
            UcNumPad1.Visible = True
            UcNumPad1.Focus()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Open in new window


For some reason, the event is getting fired twice when a user first enters the field, and then once when the user clicks off of the field.

I do not understand why... Any ideas?
0
Comment
Question by:ScottParker
  • 5
  • 4
9 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Don't see anything wrong with the code posted...

You have "Handles txtWONumber.Enter" at the end of line #1 though.  Are you also wiring up "txtWONumber" via code somewhere else?  That would cause that handler to fire twice, but only for that particular control.
0
 
LVL 3

Author Comment

by:ScottParker
Comment Utility
I took off the "Handles txtWONumber.Enter", that was there for a textbox that was placed on the screen during design time.

The event is still firing twice for the textbox's that are placed on the screen during runtime.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Hmmm...do a search for other AddHandler calls?

I wonder if the event is getting fired because focus is switching between the TextBox and your NumPad UserControl (more often than you think)?
0
 
LVL 3

Author Comment

by:ScottParker
Comment Utility
I did the search and there are no other AddHandler calls.

I suspect the focus is the issue because I have now noticed some more "unexpected" behavior.

When you first click in to the textbox, the event gets fired twice and the user control is shown. (ucNumPad1)

Then when you click  onto the form (to get the user control to hide) the event gets fired once more. But the user control is hidden.
There is no handler for txtTemp2.Leave.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Author Comment

by:ScottParker
Comment Utility
Slight edit to the last post.

When you click off of the user control, it will call the event again, only if you have "Cleared" the textbox.  If you add to the textbox and then click off of the user control, it does not call the event again.

But if you hit the "Clear" button on the user control, it does wipe the field, and then closes the user control, and then calls the txtTemp2.Enter event..
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
I'd either need to see a lot more code or actually be able to play with a sample project to figure it out.  We've looked for all the obvious things already ...  =\
0
 
LVL 3

Author Comment

by:ScottParker
Comment Utility
Main Form.....

Imports System.Data

Public Class frmMain
    Private _SendErrorMessageTo As String
    Private _emailserver As String
    Private bcheck As Boolean
    Private myData As New DataSet
    Private myMSSQLdata As myMSSQLdata
    Private myJDEdata As myJDEdata
    Private myJDEComData As myJDEdata

    'Public _CurrentTextBox As String
    Private _CurrentTextBox As TextBox = Nothing


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        lblItemDescription.Text = ""
        lblItemNumber.Text = ""
        lblWorkCenter.Text = ""
        _SendErrorMessageTo = Configuration.ConfigurationManager.AppSettings("SendErrorMessageTo")
        _emailserver = Configuration.ConfigurationManager.AppSettings("emailServer")
        Dim SQLServer As String = Configuration.ConfigurationManager.AppSettings("SQLServer")
        Dim connString As String = "Network Library=DBMSSOCN;Data Source=" & SQLServer & ",1433;" & _
                                "Initial Catalog=Internal_Defects;User ID=xxxx;Password=xxxxxx"
        myMSSQLdata = New myMSSQLdata(connString)
        connString = "DataSource=172.16.100.5;DefaultCollection=xxxx;UserID=xxxx;Password=xxxx"
        myJDEdata = New myJDEdata(connString)
        connString = "DataSource=172.16.100.5;DefaultCollection=grtcomc1;UserID=ODBC400;Password=ODBC"
        myJDEComData = New myJDEdata(connString)

        AddHandler UcNumPad1.VisibleChanged, AddressOf UcNumPad1_VisibleChanged


    End Sub

    Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click

        Try
            Dim strSQL As String = ""

            strSQL = "SELECT walitm,WAMCU, IMDSC1, WAWR01, WAWR02 " & _
                " FROM F4801 Join F4101 on WAITM = IMITM " & _
                " WHERE WADOCO = " & txtWONumber.Text & " and WADCTO = 'WO'"

            myJDEdata.fillDataSet(strSQL, "F4801", myData)

            If myData.Tables("F4801").Rows.Count > 0 Then

                strSQL = "SELECT WMCPIL, WMDSC1 ,Decimal(WMOPSQ/100,15,2) as WMOPSQ, WMLOCN " & _
                    " FROM F3111 " & _
                    " WHERE WMLNTY = 'S' and WMDCTO = 'WO' AND WMDOCO = " & txtWONumber.Text & _
                    " And Substr(WMCPIL,1,2) not in ('51','16','LB','76','56')" & _
                    " Order by WMCPIL,  WMOPSQ"
                myJDEdata.fillDataSet(strSQL, "F3111", myData)

                strSQL = "Select  IXLITM, decimal(IXQNTY/10000,15,4) as QtyPer ,IXUM " & _
                    " from F3002 WHERE IXEFFT > (select ONDTEJ from F00365A) AND IXCMCU = '" & myData.Tables("F4801").Rows(0).Item("WAMCU") & _
                    "' AND IXKITL = '" & myData.Tables("F4801").Rows(0).Item("WALITM") & "'"
                myJDEdata.fillDataSet(strSQL, "F3002", myData)

                strSQL = "Select Item_Number, Count(*) as Qty from Defects where WorkOrder_Id = " & txtWONumber.Text & " Group by Item_Number"
                myMSSQLdata.fillDataSet(strSQL, "Defects", myData)

                strSQL = "Select Item_Number, Sum(Qty) from Scrapped where WorkOrder_Id = " & txtWONumber.Text & " Group by Item_Number"
                myMSSQLdata.fillDataSet(strSQL, "Scrapped", myData)

                fillcbOPSQ()

                lblItemNumber.Text = Trim(myData.Tables("F4801").Rows(0).Item("WALITM"))
                lblItemDescription.Text = Trim(myData.Tables("F4801").Rows(0).Item("IMDSC1"))
                lblWorkCenter.Text = Trim(myData.Tables("F4801").Rows(0).Item("WAWR01")) & Trim(myData.Tables("F4801").Rows(0).Item("WAWR02"))

            Else
                lblItemDescription.Text = "WO does not exist."
            End If
        Catch ex As Exception
            Logerror("frmMain.btnSubmit_Click", "Error: ", ex, True)
        End Try

    End Sub

    Private Sub fillcbOPSQ()
        If myData.Tables.Contains("wrkOPSQ") Then
            myData.Tables.Remove("wrkOPSQ")
            GC.Collect()
        End If
        myData.Tables.Add(myData.Tables("F3111").DefaultView.ToTable("wrkOPSQ", True, "WMOPSQ"))
        Dim addrow As DataRow
        With myData.Tables("wrkOPSQ")
            addrow = .NewRow
            addrow("WMOPSQ") = 0
            .Rows.Add(addrow)
        End With

        Dim dvSorted As DataView
        dvSorted = myData.Tables("wrkOPSQ").DefaultView
        dvSorted.Sort = "WMOPSQ"

        cbOpSeq.DisplayMember = "WMOPSQ"
        cbOpSeq.DataSource = dvSorted

    End Sub

    Private Sub createDetailTable()
        If myData.Tables.Contains("WrkDetail") Then
            myData.Tables.Remove("wrkDetail")
            GC.Collect()
        End If

        myData.Tables.Add("WrkDetail")
        With myData.Tables("WrkDetail").Columns
            .Add("bDefect", GetType(Boolean))
            .Add("bScrap", GetType(Boolean))
            .Add("decOpSeq", GetType(Decimal))
            .Add("intQty", GetType(Integer))
            .Add("decQtyPer", GetType(Decimal))
            .Add("txtUOM", GetType(String))
            .Add("txtComponentNumber", GetType(String))
            .Add("txtComponentDescription", GetType(String))
            .Add("intTotalDefectQty", GetType(Integer))
            .Add("intTotalScrapQty", GetType(Integer))
        End With

    End Sub

    Private Sub loadDetailTable()
        Try
            Dim drF3111 As DataRow
            Dim addRow As DataRow

            For Each drF3111 In myData.Tables("F3111").Rows
                With myData.Tables("wrkDetail")
                    addRow = .NewRow
                    addRow("bDefect") = False
                    addRow("bScrap") = False
                    addRow("decOpSeq") = drF3111("WMOPSQ")
                    addRow("intQty") = 0
                    Dim foundF3002 As DataRow()
                    Dim strSQL As String = "IXLITM = '" & drF3111("WMCPIL") & "'"
                    foundF3002 = myData.Tables("F3002").Select(strSQL)
                    If foundF3002.Length > 0 Then
                        addRow("decQtyPer") = foundF3002(0).Item("QtyPer")
                        addRow("txtUOM") = Trim(foundF3002(0).Item("IXUM"))
                    Else
                        addRow("decQtyPer") = 0
                        addRow("txtUOM") = ""
                    End If
                    addRow("txtComponentNumber") = Trim(drF3111("WMCPIL"))
                    addRow("txtComponentDescription") = Trim(drF3111("WMDSC1"))

                    Dim intDefectQty As Integer = 0
                    Dim foundDefect As DataRow()
                    strSQL = "Item_Number = '" & Trim(drF3111("WMCPIL")) & "'"
                    foundDefect = myData.Tables("Defects").Select(strSQL)
                    If foundDefect.Length > 0 Then
                        Dim x As Integer = 0
                        For x = 0 To foundDefect.Length - 1
                            intDefectQty += foundDefect(x).Item("Qty")
                        Next
                    End If
                    addRow("intTotalDefectQty") = intDefectQty

                    Dim intScrappedQty As Integer = 0
                    Dim foundScrap As DataRow()
                    strSQL = "Item_Number = '" & Trim(drF3111("WMCPIL")) & "'"
                    foundScrap = myData.Tables("Scrapped").Select(strSQL)
                    If foundScrap.Length > 0 Then
                        Dim x As Integer = 0
                        For x = 0 To foundScrap.Length - 1
                            intScrappedQty += foundScrap(x).Item("Qty")
                        Next
                    End If
                    addRow("intTotalScrapQty") = intScrappedQty


                    .Rows.Add(addRow)
                End With
            Next
        Catch ex As Exception
            Logerror("frmMain.loadDetailTable", "Error: ", ex, True)
        End Try

    End Sub

    Private Sub cbOpSeq_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbOpSeq.SelectedIndexChanged
        createDetailTable()
        loadDetailTable()
        FillpnlData()

    End Sub

    Private Sub FillpnlData()

        ' Loop though the distinct list of Operation Sequences
        ' For each OpSeq first add a line for that one, 
        '  then add lines for the parts in it.

        Dim intCount As Int16 = 0

        If cbOpSeq.Text = "" Or cbOpSeq.Text = "0" Then
            intCount = cbOpSeq.Items.Count
        Else
            intCount = 1
        End If

        Dim dvSorted As DataView
        dvSorted = myData.Tables("wrkOPSQ").DefaultView
        dvSorted.Sort = "WMOPSQ"


        pnlData.Controls.Clear()
        '
        'Add Heading Labels
        '
        Dim lblHTemp1 As New Label
        With lblHTemp1
            .Name = "lblHDefect"
            .Location = New System.Drawing.Point(105, 5)
            .Size = New System.Drawing.Size(50, 16)
            .Text = "Defect"
        End With
        pnlData.Controls.Add(lblHTemp1)

        Dim lblHTemp2 As New Label
        With lblHTemp2
            .Name = "lblHScrap"
            .Location = New System.Drawing.Point(160, 5)
            .Size = New System.Drawing.Size(50, 16)
            .Text = "Scrap"
        End With
        pnlData.Controls.Add(lblHTemp2)

        Dim lblHTemp3 As New Label
        With lblHTemp3
            .Name = "lblHQty"
            .Location = New System.Drawing.Point(215, 25)
            .Size = New System.Drawing.Size(50, 16)
            .Text = "Qty."
        End With
        pnlData.Controls.Add(lblHTemp3)

        Dim lblHTemp4 As New Label
        With lblHTemp4
            .Name = "lblHCompontent"
            .Location = New System.Drawing.Point(280, 25)
            .Size = New System.Drawing.Size(100, 16)
            .Text = "Component"
        End With
        pnlData.Controls.Add(lblHTemp4)

        Dim lblHTemp7 As New Label
        With lblHTemp7
            .Name = "lblHQtyPer"
            .Location = New System.Drawing.Point(560, 5)
            .Size = New System.Drawing.Size(55, 16)
            .Text = "BOM"
        End With
        pnlData.Controls.Add(lblHTemp7)

        Dim lblHTemp5 As New Label
        With lblHTemp5
            .Name = "lblHQtyPer"
            .Location = New System.Drawing.Point(530, 25)
            .Size = New System.Drawing.Size(55, 16)
            .Text = "Qty. Per"
        End With
        pnlData.Controls.Add(lblHTemp5)

        Dim lblHTemp6 As New Label
        With lblHTemp6
            .Name = "lblHCompontent"
            .Location = New System.Drawing.Point(585, 25)
            .Size = New System.Drawing.Size(50, 16)
            .Text = "UOM"
        End With
        pnlData.Controls.Add(lblHTemp6)

        Dim lblHTemp8 As New Label
        With lblHTemp8
            .Name = "lblHTotal"
            .Location = New System.Drawing.Point(720, 5)
            .Size = New System.Drawing.Size(50, 16)
            .Text = "Total"
        End With
        pnlData.Controls.Add(lblHTemp8)

        Dim lblHTemp9 As New Label
        With lblHTemp9
            .Name = "lblHTotalDefects"
            .Location = New System.Drawing.Point(680, 25)
            .Size = New System.Drawing.Size(50, 16)
            .Text = "Defects"
            .BackColor = Color.Pink
        End With
        pnlData.Controls.Add(lblHTemp9)

        Dim lblHTemp10 As New Label
        With lblHTemp10
            .Name = "lblHTotalScrap"
            .Location = New System.Drawing.Point(750, 25)
            .Size = New System.Drawing.Size(50, 16)
            .Text = "Scap"
            .BackColor = Color.PeachPuff
        End With
        pnlData.Controls.Add(lblHTemp10)

        '
        ' Add OPSQ (Heading) Detail Lines
        '
        Dim intRowPos As Integer = 55
        Dim intRowCounter As Integer = 1

        For intOpSeqRow As Integer = 1 To intCount

            Dim lblTemp1 As New Label
            With lblTemp1
                .Name = "lblOpSeq" & intRowCounter
                .Location = New System.Drawing.Point(5, (intRowPos * intRowCounter) + 15)
                .Size = New System.Drawing.Size(85, 20)
                .AutoSize = False

                If intCount = 1 Then
                    If cbOpSeq.Text = "0" Then
                        .Text = " Entire BOM "
                    Else
                        .Text = "Op. Seq. " & cbOpSeq.Text
                    End If

                Else
                    If intOpSeqRow - 1 = 0 Then
                        .Text = " Entire BOM "
                    Else
                        .Text = "Op. Seq. " & dvSorted(intOpSeqRow - 1).Item("WMOPSQ")
                    End If
                End If
            End With
            pnlData.Controls.Add(lblTemp1)

            'Scrap Checkbox
            Dim cbTemp2 As New CheckBox
            With cbTemp2
                .Name = "cbScrap" & intRowCounter
                .Location = New System.Drawing.Point(160, intRowPos * intRowCounter)
                .Size = New System.Drawing.Size(50, 50)
                .AutoSize = False
                .Checked = False
                .BackColor = Color.PeachPuff
                .CheckAlign = System.Drawing.ContentAlignment.MiddleCenter
            End With
            AddHandler cbTemp2.Click, AddressOf cbScrap_Click
            pnlData.Controls.Add(cbTemp2)

            'Scrap Qty.
            Dim txtTemp1 As New TextBox
            With txtTemp1
                .Name = "txtItemQty" & intRowCounter
                .Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                .Location = New System.Drawing.Point(215, (intRowPos * intRowCounter) + 15)
                .Size = New System.Drawing.Size(60, 30)
                .AutoSize = False
            End With
            AddHandler txtTemp1.Enter, AddressOf txtItemQty_Enter
            pnlData.Controls.Add(txtTemp1)

            '
            ' Add component Detail Lines for the current OPSQ.
            '
            Dim drFoundRows() As DataRow
            Dim drCurrentRow As DataRow
            Dim strSQL As String = ""
            If cbOpSeq.Text = "" Or cbOpSeq.Text = "0" Then
                strSQL = "decOpSeq = " & dvSorted(intOpSeqRow - 1).Item("WMOPSQ")
            Else
                strSQL = "decOpSeq = " & cbOpSeq.Text
            End If

            drFoundRows = myData.Tables("wrkDetail").Select(strSQL)

            For Each drCurrentRow In drFoundRows
                intRowCounter += 1
                ' Op Seq  - Hidden
                Dim lblDTemp1 As New Label
                With lblDTemp1
                    .Name = "lblOpSeq" & intRowCounter
                    .Location = New System.Drawing.Point(5, (intRowPos * intRowCounter) + 15)
                    .Size = New System.Drawing.Size(45, 20)
                    .AutoSize = False
                    .Visible = True
                    .Text = drFoundRows(0).Item("decOPSeq")
                End With
                pnlData.Controls.Add(lblDTemp1)

                'Defect Button
                Dim btnTemp1 As New Button
                With btnTemp1
                    .Name = "btnDefect" & intRowCounter
                    .Location = New System.Drawing.Point(105, intRowPos * intRowCounter)
                    .Size = New System.Drawing.Size(50, 50)
                    .AutoSize = False
                    .BackColor = Color.Pink
                    .Text = "Defect"
                End With
                AddHandler btnTemp1.Click, AddressOf btnDefect_Click
                pnlData.Controls.Add(btnTemp1)

                'Scrap Checkbox
                Dim cbTemp4 As New CheckBox
                With cbTemp4
                    .Name = "cbScrap" & intRowCounter
                    .Location = New System.Drawing.Point(160, intRowPos * intRowCounter)
                    .Size = New System.Drawing.Size(50, 50)
                    .AutoSize = False
                    .Checked = False
                    .BackColor = Color.PeachPuff
                    .CheckAlign = System.Drawing.ContentAlignment.MiddleCenter
                End With
                AddHandler cbTemp4.Click, AddressOf cbScrap_Click
                pnlData.Controls.Add(cbTemp4)

                'Scrap Qty Textbox
                Dim txtTemp2 As New TextBox
                With txtTemp2
                    .Name = "txtItemQty" & intRowCounter
                    .Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                    .Location = New System.Drawing.Point(215, (intRowPos * intRowCounter) + 15)
                    .Size = New System.Drawing.Size(60, 30)
                    .AutoSize = False
                End With
                AddHandler txtTemp2.Enter, AddressOf txtItemQty_Enter
                'AddHandler txtTemp2.Leave, AddressOf txtItemQty_Leave
                pnlData.Controls.Add(txtTemp2)

                'Component Item Number
                Dim lblTemp3 As New Label
                With lblTemp3
                    .Name = "lblItem" & intRowCounter
                    .Location = New System.Drawing.Point(280, (intRowPos * intRowCounter) + 15)
                    .Size = New System.Drawing.Size(100, 20)
                    .AutoSize = False
                    .Text = drCurrentRow("txtComponentNumber")
                End With
                pnlData.Controls.Add(lblTemp3)

                'Component Description
                Dim lblTemp4 As New Label
                With lblTemp4
                    .Name = "lblItem" & intRowCounter
                    .Location = New System.Drawing.Point(385, (intRowPos * intRowCounter) + 15)
                    .Size = New System.Drawing.Size(150, 20)
                    .AutoSize = False
                    .Text = drCurrentRow("txtComponentDescription")
                End With
                pnlData.Controls.Add(lblTemp4)

                'Component Qty Per
                Dim lblTemp5 As New Label
                With lblTemp5
                    .Name = "lblQtyPer" & intRowCounter
                    .Location = New System.Drawing.Point(540, (intRowPos * intRowCounter) + 15)
                    .Size = New System.Drawing.Size(50, 20)
                    .AutoSize = False
                    .Text = drCurrentRow("decQtyPer")
                End With
                pnlData.Controls.Add(lblTemp5)

                'Component Qty Per UOM
                Dim lblTemp6 As New Label
                With lblTemp6
                    .Name = "lblQtyPerUom" & intRowCounter
                    .Location = New System.Drawing.Point(595, (intRowPos * intRowCounter) + 15)
                    .Size = New System.Drawing.Size(50, 20)
                    .AutoSize = False
                    .Text = drCurrentRow("txtUOM")
                End With
                pnlData.Controls.Add(lblTemp6)

                ' Total defects 
                Dim lblTemp9 As New Label
                With lblTemp9
                    .Name = "lblTotalDefects" & intRowCounter
                    .Location = New System.Drawing.Point(690, (intRowPos * intRowCounter) + 15)
                    .Size = New System.Drawing.Size(50, 16)
                    .Text = drCurrentRow("intTotalDefectQty")
                    .BackColor = Color.Pink
                End With
                pnlData.Controls.Add(lblTemp9)

                ' Total Scrap
                Dim lblTemp10 As New Label
                With lblTemp10
                    .Name = "lblTotalScrap" & intRowCounter
                    .Location = New System.Drawing.Point(760, (intRowPos * intRowCounter) + 15)
                    .Size = New System.Drawing.Size(50, 16)
                    .Text = drCurrentRow("intTotalScrapQty")
                    .BackColor = Color.PeachPuff
                End With
                pnlData.Controls.Add(lblTemp10)

            Next

            intRowCounter += 1
        Next

        Dim lblTemp7 As New Label
        With lblTemp7
            .Name = "lblEndOfRecords"
            .Location = New System.Drawing.Point(5, (intRowPos * intRowCounter) + 15)
            .Size = New System.Drawing.Size(100, 20)
            .AutoSize = False
            .Text = "End of Item List"
        End With
        pnlData.Controls.Add(lblTemp7)
    End Sub

    Private Sub btnDefect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btnTemp As Button = sender

        Dim strRowNumber As String = Mid(btnTemp.Name, 10, Len(btnTemp.Name) - 9)
        pnlData.Controls.Add(UcDefects1)
        UcDefects1.txtWorkCenter = lblWorkCenter.Text
        UcDefects1.txtWorkOrderNumber = txtWONumber.Text
        'lblItem
        Dim lblTemp() As Control = pnlData.Controls.Find("lblItem" & strRowNumber, True)
        UcDefects1.txtItemNumber = lblTemp(0).Text
        UcDefects1.Location = New System.Drawing.Point(btnTemp.Location.X + 60, btnTemp.Location.Y - 10)
        UcDefects1.BringToFront()
        UcDefects1.Visible = True
        UcDefects1.selectDefects()

    End Sub

    Private Sub cbScrap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim cbSender As CheckBox = sender
        Dim bValue As Boolean = cbSender.Checked

        ' First check to see what type of OPSQ row the sender is.  Entire BOM, Op. Seq XX, or XX.

        Dim intRowNumber As Integer = Mid(cbSender.Name, 8, cbSender.Name.Length - 7)

        Dim strOpSeqNumber As String = ""
        Dim bOpSQControlExists As Boolean = False

        Dim lblOpSeq() As Control = pnlData.Controls.Find("lblOpSeq" & intRowNumber.ToString, True)

        If Trim(lblOpSeq(0).Text) = "Entire BOM" Then
            strOpSeqNumber = "ALL"
        End If
        If Mid(lblOpSeq(0).Text, 1, 3) = "Op." Then
            strOpSeqNumber = "OP" & Mid(lblOpSeq(0).Text, 10, lblOpSeq(0).Text.Length - 9)
        End If
        If lblOpSeq(0).Text.Length <= 3 Then
            strOpSeqNumber = lblOpSeq(0).Text
        End If


        For Each ctrl As Control In pnlData.Controls
            If (ctrl.GetType() Is GetType(CheckBox)) And Mid(ctrl.Name, 1, 7) = "cbScrap" Then
                Dim cpTemp As CheckBox = ctrl
                If strOpSeqNumber = "ALL" Then
                    cpTemp.Checked = bValue
                Else
                    If Mid(strOpSeqNumber, 1, 2) = "OP" Then
                        'only change the ones in this op seq
                        Dim intRowNumber2 As Integer = Mid(ctrl.Name, 8, ctrl.Name.Length - 7)
                        Dim lblOpSeq2() As Control = pnlData.Controls.Find("lblOpSeq" & intRowNumber2.ToString, True)
                        If lblOpSeq2(0).Text = Mid(strOpSeqNumber, 3, strOpSeqNumber.Length - 2) Then
                            cpTemp.Checked = bValue
                        End If
                    End If
                End If
            End If

        Next
    End Sub

    Private Sub txtItemQty_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles txtWONumber.Enter
        Try
            _CurrentTextBox = sender
            If _CurrentTextBox.Name = "txtWONumber" Then
                Me.Controls.Add(UcNumPad1)
                UcNumPad1.Location = New System.Drawing.Point(140, _CurrentTextBox.Location.Y + 30)
            Else
                pnlData.Controls.Add(UcNumPad1)
                UcNumPad1.Location = New System.Drawing.Point(280, _CurrentTextBox.Location.Y + 20)
            End If
            UcNumPad1.BringToFront()
            UcNumPad1.Visible = True
            UcNumPad1.Focus()

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub

    Private Sub txtItemQty_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWONumber.Enter
        'Try
        '_CurrentTextBox = sender
        'If _CurrentTextBox.Text = "" Then
        '    For Each ctrl As Control In pnlData.Controls
        '        If Mid(ctrl.Name, 1, 10) = "txtItemQty" Then
        '            ctrl.Enabled = True
        '        End If
        '    Next
        'Else
        '    For Each ctrl As Control In pnlData.Controls
        '        If Mid(ctrl.Name, 1, 10) = "txtItemQty" And ctrl.Name <> _CurrentTextBox.Name Then
        '            ctrl.Text = ""
        '            ctrl.Enabled = False
        '        End If
        '    Next
        'End If

        'Catch ex As Exception
        '    MsgBox(ex.Message)
        'End Try
    End Sub

    Private Sub UcNumPad1_Clear() Handles UcNumPad1.Clear
        If Not IsNothing(_CurrentTextBox) Then
            _CurrentTextBox.Clear()
        End If
    End Sub

    Private Sub UcNumPad1_Character(value As String) Handles UcNumPad1.Character
        If Not IsNothing(_CurrentTextBox) Then
            _CurrentTextBox.SelectedText = value
        End If
    End Sub

    Private Sub UcNumPad1_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        If UcNumPad1.Visible = True Then
            _CurrentTextBox.BackColor = Color.Plum
        Else
            _CurrentTextBox.BackColor = Color.White
            If _CurrentTextBox.Text <> "" Then
                For Each ctrl As Control In pnlData.Controls
                    If Mid(ctrl.Name, 1, 7) = "cbScrap" Or Mid(ctrl.Name, 1, 10) = "txtItemQty" Then
                        If ctrl.Name <> _CurrentTextBox.Name Then
                            ctrl.Enabled = False
                        End If
                    End If
                Next
            Else
                For Each ctrl As Control In pnlData.Controls
                    If Mid(ctrl.Name, 1, 7) = "cbScrap" Or Mid(ctrl.Name, 1, 10) = "txtItemQty" Then
                        If ctrl.Name <> _CurrentTextBox.Name Then
                            ctrl.Enabled = True
                        End If
                    End If
                Next

            End If
        End If
    End Sub

    Private Sub UcDefects1_AccumulateValues() Handles UcDefects1.AccumulateValues


        Dim strSQL As String = "Select Item_Number, Count(*) as Qty from Defects where WorkOrder_Id = " & txtWONumber.Text & " Group by Item_Number"
        myMSSQLdata.fillDataSet(strSQL, "Defects", myData)

        For Each ctrl As Control In pnlData.Controls
            If Mid(ctrl.Name, 1, 15) = "lblTotalDefects" Then

                Dim rowNumber As String = Mid(ctrl.Name, 16, ctrl.Name.Length - 15)

                Dim lblTemp() As Control = pnlData.Controls.Find("lblItem" & rowNumber, True)



                Dim intDefectQty As Integer = 0
                Dim foundDefect As DataRow()
                strSQL = "Item_Number = '" & lblTemp(0).Text & "'"
                foundDefect = myData.Tables("Defects").Select(strSQL)
                If foundDefect.Length > 0 Then
                    Dim x As Integer = 0
                    For x = 0 To foundDefect.Length - 1
                        intDefectQty += foundDefect(x).Item("Qty")
                    Next
                End If
                ctrl.Text = intDefectQty
            End If
        Next

    End Sub


    Private Sub btnScrap_Click(sender As System.Object, e As System.EventArgs) Handles btnScrap.Click

        Dim intEntireBOM As Integer = 0


        For Each ctrl As Control In pnlData.Controls

        Next

    End Sub

    Private Sub btnProcess_Click(sender As System.Object, e As System.EventArgs) Handles btnProcess.Click

    End Sub
End Class

Open in new window


User Control....
Public Class ucNumPad
    Public Event Clear()
    Public Event Character(ByVal value As String)
    Private WithEvents _MyFilter As MyFilter

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _MyFilter = New MyFilter
        Application.AddMessageFilter(_MyFilter)
    End Sub

    Private Sub btn_Click(sender As Object, e As System.EventArgs) Handles _
        btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, _
        btn6.Click, btn7.Click, btn8.Click, btn9.Click, btnDec.Click, btnClear.Click

        If sender Is btnClear Then
            RaiseEvent Clear()
        Else
            RaiseEvent Character(DirectCast(sender, Button).Text)
        End If
    End Sub

    Private Sub _MyFilter_MouseDown() Handles _MyFilter.MouseDown
        If Me.Visible Then
            Dim RC As Rectangle = Me.RectangleToScreen(Me.ClientRectangle)
            If Not RC.Contains(Cursor.Position) Then
                Me.Visible = False
            End If
        End If
    End Sub

    Private Class MyFilter
        Implements IMessageFilter

        Public Event MouseDown()

        Private Const WM_LBUTTONDOWN As Integer = &H201

        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
            Select Case m.Msg
                Case WM_LBUTTONDOWN
                    RaiseEvent MouseDown()

            End Select
            Return False
        End Function

    End Class


End Class

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
When you click off the usercontrol to make it disappear, focus is going to move back to one of the controls on the form.  This is likely to be the textbox causing the event to fire again?

Maybe if the usercontrol raised another custom event requesting to be hidden, then the form can trap that event and set a boolean variable so that the textboxes know to ignore the fact that they have gotten focus (and reset the flag).  The form would hide the usercontrol instead of the usercontrol directly hiding itself.
0
 
LVL 3

Author Closing Comment

by:ScottParker
Comment Utility
I never could figure out why the event is firing twice.  It would fire, right when I first clicked inside a textbox that it was hooked up to call the event.
I think it has something to do with how I am building the dynamic controls or something...

I changed the functionality in the txtItemQty_Enter event so that it does not matter that its called twice anymore.   I really needed to "Move On" and get this program working. (boss is hounding me)

Your last comment is what I would have tried next and it seems like the "Right" work around... so That is why I awarded the points.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now