vnarod Posted April 7, 2003 Posted April 7, 2003 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? Quote
*Experts* Volte Posted April 7, 2003 *Experts* Posted April 7, 2003 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. Quote
vnarod Posted April 7, 2003 Author Posted April 7, 2003 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? Quote
*Gurus* divil Posted April 7, 2003 *Gurus* Posted April 7, 2003 What makes you think you can? You can't just trigger delegates that belong to something else like that. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
vnarod Posted April 7, 2003 Author Posted April 7, 2003 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? Quote
aewarnick Posted April 27, 2003 Posted April 27, 2003 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? Quote C#
Guest mutant Posted April 27, 2003 Posted April 27, 2003 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 Quote
aewarnick Posted April 27, 2003 Posted April 27, 2003 (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 April 27, 2003 by aewarnick Quote C#
*Gurus* divil Posted April 27, 2003 *Gurus* Posted April 27, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted April 27, 2003 Posted April 27, 2003 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!!! Quote C#
hog Posted April 27, 2003 Posted April 27, 2003 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 :-) Quote My website
aewarnick Posted April 27, 2003 Posted April 27, 2003 Thanks for posting that. Although I am not a VB fan I can understand it. Quote C#
aewarnick Posted April 27, 2003 Posted April 27, 2003 (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 April 27, 2003 by aewarnick Quote C#
*Gurus* divil Posted April 28, 2003 *Gurus* Posted April 28, 2003 You don't have to perform any check, every control has a Controls collection you can enumerate through to get access to its subcontrols. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.