Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • *Experts*
Posted

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.

Posted
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?
Posted
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?
  • 3 weeks later...
Posted

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© is TextBox) c.Clear();

 

but neither of them work. Am I doing this wrong or is this not possible?

C#
Guest mutant
Posted

This works fine for me:

Dim ctrl As Control
For Each ctrl In Me.Controls
     If TypeOf ctrl Is TextBox Then
           DirectCast(ctrl, TextBox).Clear()
     End If
Next

Posted (edited)

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© is TextBox)

{

TextBox t=(TextBox)c;

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

}

 

}

 

and this part does not compile anyways:

if(typeof©

 

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?

Edited by aewarnick
C#
  • *Gurus*
Posted

Use the code you had before:

 

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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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!!!

C#
Posted

This is how I cycled through all the controls on my tabcontrol:

 


  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:

 


 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 :-)

My website
Posted (edited)
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?

Edited by aewarnick
C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...