Accessing events

vnarod

Regular
Joined
Mar 22, 2002
Messages
84
Is it possible to access an event of a control without knowing its name in advance:

for each c in Me.Controls
if TypeOf c is ListBox then
lst=c
end if
next

How do I raise a DoubleClick event for lst?
 
I think your best bet is to put all the code for the DoubleClick event
into a seperate, public sub in the form (or possibly, just make the
doubleclick event handler a public sub), and then call that. I don't
think there is a simple way to artificially cause a double click event.
 
I don't have the code for Doubleclick event. I am trying to access a third party control. It has the event but How do I get to it?
 
What makes you think you can? You can't just trigger delegates that belong to something else like that.
 
I don't know if iI can do it from .NET. I was thinking that since the control was designed to process a certain windows message, I could simulate that message. Perhaps, capture its handle and use SendMessage API? Any thoughts?
 
I have been trying to do this same type of thing for a long time now and am anxious to find out if I can do this:

foreach(Control c in this.Controls)
if(c is TextBox) c.Clear();

I also tried it like this:

foreach(Control c in this.Controls)
if(typeof(c) is TextBox) c.Clear();

but neither of them work. Am I doing this wrong or is this not possible?
 
This works fine for me:
Visual Basic:
Dim ctrl As Control
For Each ctrl In Me.Controls
      If TypeOf ctrl Is TextBox Then
            DirectCast(ctrl, TextBox).Clear()
      End If
Next
 
I put a message box in my for loop and the only cotrol it loops is the tab control and the loop ends. How do I iterate though all the controls on my form.

foreach(Control c in this.Controls)
{

MessageBox.Show(""+c.Name);

if(typeof(c) is TextBox)
{
TextBox t=(TextBox)c;
MessageBox.Show(""+t.Name);
}

}

and this part does not compile anyways:
if(typeof(c)

If I take typeof out it compiles fine.

Also, I tried to add the textboxes as components but to no avail. When I add them, they dissapear from the form because their location is all messed up, even if I add them from initializeComponents.

What is wrong? How can I add the text boxes to the Components without their settings getting all messed up?
 
Last edited:
Use the code you had before:

C#:
if (c is TextBox)
    ((TextBox)c).Clear();

But in order to loop through all controls on a form you have to recurse on the Controls collection of the Form, then the Controls collection of every control you find from then on.
 
I have a small lesson that everyone should remember: If you have a tab control on your form remember that the controls in that tab are located in that tabs Controls not the forms.

That was my problem. Right on divil. Thank you very much!!!
 
This is how I cycled through all the controls on my tabcontrol:

Visual Basic:
   Private Sub GetControlHashCodes()

        ' obtain the name and a unique hashcode of each control for reference purposes when updating the
        ' form with job data

        Dim pnlPanel, tbcTabControl, tbpTabPage, ctlControl As Object
        Dim intX As Integer

        Try
            ' cycle through panel controls, only one in this instance

            For Each pnlPanel In Me.Controls

                ' cycle through tabcontrol controls, only one in this instance

                For Each tbcTabControl In pnlPanel.Controls

                    ' cycle through tabpages of tabcontrol

                    For Each tbpTabPage In tbcTabControl.Controls

                        ' cycle through controls on each tabpage

                        For Each ctlControl In tbpTabPage.Controls

                            ' store the controls name and hashcode in table, filter required controls only

                            Select Case Microsoft.VisualBasic.Left(ctlControl.name, 3)

                                Case "txt", "rtb", "cbo", "dtm", "tdm", "chk", "pic"

                                    m_htControlsHashTable.Add(ctlControl.Name, ctlControl)

                            End Select

                        Next

                    Next

                Next

            Next

        Catch objException As Exception

            ShowError("Location:   Class frmJob" & ControlChars.CrLf & ControlChars.CrLf & _
                      "Procedure:  GetControlHashCodes()" & ControlChars.CrLf & _
                      ControlChars.CrLf & "Error Text: " & objException.Message)

        End Try

    End Sub

Then when I need to reference them I use this:

Visual Basic:
  Private Sub ClearTabPages()

        Dim intX As Integer, intPageIndex As Integer = 11

        Try

            ' cycle through the hashtable and clear/initialise referenced controls

            For intX = 0 To intPageIndex

                DirectCast(m_htControlsHashTable("txtDueDate" & intX + 1), TextBox).Text = String.Empty
                DirectCast(m_htControlsHashTable("txtSupplier" & intX + 1), TextBox).Text = String.Empty
                DirectCast(m_htControlsHashTable("tdmJobTime" & intX + 1), DateTimePicker).Text = "00:00"
                DirectCast(m_htControlsHashTable("txtJobRef" & intX + 1), TextBox).Text = String.Empty
                DirectCast(m_htControlsHashTable("txtInvoiceNo" & intX + 1), TextBox).Text = String.Empty
                DirectCast(m_htControlsHashTable("txtInvoiceAmount" & intX + 1), TextBox).Text = 0
                DirectCast(m_htControlsHashTable("txtEngineer" & intX + 1), TextBox).Text = String.Empty
                DirectCast(m_htControlsHashTable("txtGRN" & intX + 1), TextBox).Text = String.Empty
                DirectCast(m_htControlsHashTable("rtbComments" & intX + 1), RichTextBox).Text = String.Empty
                DirectCast(m_htControlsHashTable("dtmJobDate" & intX + 1), DateTimePicker).Text = Today
                DirectCast(m_htControlsHashTable("dtmInvoiceDate" & intX + 1), DateTimePicker).Text = Today
                DirectCast(m_htControlsHashTable("cboType" & intX + 1), ComboBox).Text = String.Empty
                DirectCast(m_htControlsHashTable("picCompleted" & intX + 1), PictureBox).Visible = False

            Next

            DirectCast(m_htControlsHashTable("txtTotalJobs"), TextBox).Text = String.Empty
            DirectCast(m_htControlsHashTable("txtTotalJobsCompleted"), TextBox).Text = String.Empty
            DirectCast(m_htControlsHashTable("txtPOCost"), TextBox).Text = String.Empty
            DirectCast(m_htControlsHashTable("txtInvoicedToDate"), TextBox).Text = String.Empty
            DirectCast(m_htControlsHashTable("txtOverCharged"), TextBox).Text = String.Empty
            DirectCast(m_htControlsHashTable("txtTotalHours"), TextBox).Text = String.Empty

        Catch objException As Exception

            ShowError("Location:   Class frmJob" & ControlChars.CrLf & ControlChars.CrLf & _
                      "Procedure:  ClearTabPages()" & ControlChars.CrLf & _
                      ControlChars.CrLf & "Error Text: " & objException.Message)

        End Try

    End Sub

Works wonders :-)
 
But in order to loop through all controls on a form you have to recurse on the Controls collection of the Form, then the Controls collection of every control you find from then on.

Divil, how exacly would I do that? Do I have to use checks to see if a control is a panel or tab page using if is Panel / if is TabPage?

Or is there a better way?
 
Last edited:
You don't have to perform any check, every control has a Controls collection you can enumerate through to get access to its subcontrols.
 
Back
Top