what textboxes are on my form?
-
i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.
-
i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.
Let me give you a few clues. I am new at this so bear with me. 1. Read through all controls on the form, like this .. private ArrayList m_VisibleList = new ArrayList(); //poor name, it is all controls not just visible ones ... public void BuildListOfAllControlsOnForm() {//build a list of all controls on the form m_VisibleList.Clear(); m_VisibleList.Add(this); ArrayList aPendingControlListTemp = new ArrayList();//used to avoid aPendingControlListTemp.Add(this); System.Windows.Forms.Control CtrlParent = null; System.Windows.Forms.Control CtrlChild = null; for(int i=0; i < aPendingControlListTemp.Count; i++) {//progress thru for loop for the form and any tab pages or frame CtrlParent = (System.Windows.Forms.Control)aPendingControlListTemp[i]; for(int iIter = 0; iIter < CtrlParent.Controls.Count; iIter++) { CtrlChild = CtrlParent.Controls[iIter]; Debug.WriteLine("Parent hash " + CtrlParent.GetHashCode() + " Child hash " + CtrlChild.GetHashCode()); m_VisibleList.Add(CtrlChild); if(CtrlChild.Controls.Count > 0) {//tab page or frame control aPendingControlListTemp.Add(CtrlChild); } } } aPendingControlListTemp.Clear(); int iCount = 0; foreach(System.Windows.Forms.Control Ctrl in m_VisibleList) { Debug.WriteLine(++iCount + " " + Ctrl.Text ); } }//BuildListOfAllControlsOnForm 2nd. Loop through your list and ask which are Textboxes, that code would be something like this ... System.Windows.Forms.Control Traverse; where Traverse would point to an element in VisibleList. ... if(Traverse.GetType() == typeof(CheckBox) || Traverse.GetType().IsSubclassOf(typeof(CheckBox))) {//do special case handling for check box System.Windows.Forms.CheckBox pCheck = (System.Windows.Forms.CheckBox)Traverse; if(pCheck.Checked== false) { Traverse.Focus(); break; } } Let me know if this is too much and I will try to refine it for you.
-
i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.
static void GetTextBoxes(Control parent, ArrayList list)
{
foreach (Control ctrl in parent.Controls)
{
if (ctrl as TextBox != null)
list.Add(ctrl.Name);
GetTextBoxes(ctrl, list);
}
}leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it. -
i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.
This should work:
ArrayList names = new ArrayList();
foreach(Control ctrl in myForm.Controls)
{
if(ctrl is TextBox) names.Add(ctrl.Name);
}
I don't need no steenkin' LSD. If I want to see technicolour smears, I just give my daughter pizza for dinner. -Jamie Hale
-
i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.
All of the solutions given to you will work, if you just want textbox controls on the "FORM". But if your form contains tab-controls or frame-controls and you would like to know what/if these containers also contain textbox controls, then I think you need the solution I gave you. Lastly, if you do not care about textbox controls nested inside tab pages that are not active you could use GetNextControl(). Here is an example. public void SetFocusOnFirstEmptyControl() { int iCount=0; System.Windows.Forms.Control Traverse = this; //System.Reflection while(Traverse != null) { Debug.WriteLine(++iCount + " " + Traverse.Text ); //myChild.GetType().BaseType.Equals(typeof(myFatherClass)) if(Traverse.GetType() == typeof(CheckBox) || Traverse.GetType().IsSubclassOf(typeof(CheckBox))) {//do special case handling for check box System.Windows.Forms.CheckBox pCheck = (System.Windows.Forms.CheckBox)Traverse; if(pCheck.Checked== false) { Traverse.Focus(); break; } } if(Traverse.Text.Length == 0) {//test controls like textbox and dropdown Traverse.Focus(); break; } Traverse = GetNextControl(Traverse, true); }//while }//SetFocusOnFirstEmptyControl You have a lot of good people helping you. An answer is assured.
-
All of the solutions given to you will work, if you just want textbox controls on the "FORM". But if your form contains tab-controls or frame-controls and you would like to know what/if these containers also contain textbox controls, then I think you need the solution I gave you. Lastly, if you do not care about textbox controls nested inside tab pages that are not active you could use GetNextControl(). Here is an example. public void SetFocusOnFirstEmptyControl() { int iCount=0; System.Windows.Forms.Control Traverse = this; //System.Reflection while(Traverse != null) { Debug.WriteLine(++iCount + " " + Traverse.Text ); //myChild.GetType().BaseType.Equals(typeof(myFatherClass)) if(Traverse.GetType() == typeof(CheckBox) || Traverse.GetType().IsSubclassOf(typeof(CheckBox))) {//do special case handling for check box System.Windows.Forms.CheckBox pCheck = (System.Windows.Forms.CheckBox)Traverse; if(pCheck.Checked== false) { Traverse.Focus(); break; } } if(Traverse.Text.Length == 0) {//test controls like textbox and dropdown Traverse.Focus(); break; } Traverse = GetNextControl(Traverse, true); }//while }//SetFocusOnFirstEmptyControl You have a lot of good people helping you. An answer is assured.
:) you're right, i really appreciate your help on this and i have learned a LOT of stuff from everyone helping and i thank you. I got exactly what i needed and didn't even think about what you said regarding the textboxes on panels and tabs. i just thought it would show everything, but i was wrong. Thanks very much for everyone's help. :) .gonad.