aleborg
asked on
FindControl in Windows Form
Hi!
I have a Windows Form and I need to find a control in the form but I can't find out how to do it in a Windows Form!
I tried this:
((TextBox)this.FindControl (Form1.Act iveForm,"c n")).Text = "my text";
private Control FindControl(Control Parent,string Target)
{
Control FoundIt = null;
int i=0;
do
{
if(Parent.Controls[i].Name == Target)
{
FoundIt = Parent.Controls[i];
}
else
{
if(Parent.Controls[i].Cont rols.Count > 0)
FoundIt = FindControl(Parent.Control s[i], Target);
i++;
}
}
while(FoundIt!=null || i>Parent.Controls.Count);
return FoundIt;
}
But it don't work, it says tat the refrence of the object is not set to a instance of an object (or something like that)
I have a Windows Form and I need to find a control in the form but I can't find out how to do it in a Windows Form!
I tried this:
((TextBox)this.FindControl
private Control FindControl(Control Parent,string Target)
{
Control FoundIt = null;
int i=0;
do
{
if(Parent.Controls[i].Name
{
FoundIt = Parent.Controls[i];
}
else
{
if(Parent.Controls[i].Cont
FoundIt = FindControl(Parent.Control
i++;
}
}
while(FoundIt!=null || i>Parent.Controls.Count);
return FoundIt;
}
But it don't work, it says tat the refrence of the object is not set to a instance of an object (or something like that)
Can you describe what are you trying to do? I see that you use recursion, why? Recursive function must always have stop condition, and FindControl doesn't have it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think your quit-condition is not right.
try this
while(FoundIt == null && i<Parent.Controls.Count);
to stop, if found, or no more controls...
this should work,
Thalox
...
and I would use a while-loop, not do-while, because there could be controls, with no childs
and I would use a while-loop, not do-while, because there could be controls, with no childs
ASKER
What I'm trying to do in this code is to find a textbox with a special name.
I have some "rows" in my form, each row has a drobpdownlist and some textbox, each control in a row is named txtArt1, txtText1, txtPris1
the number, 1 is the number of the row and txtArt is the DropdownList.
All dropdownlists are connected to private void txtArt1_SelectedIndexChang ed(object sender, System.EventArgs e) on selectedindexchanged.
And txtArt1_SelectedIndexChang ed looks like this:
private void txtArt1_SelectedIndexChang ed(object sender, System.EventArgs e)
{
ComboBox dd = ((ComboBox)sender);
string cn = dd.Name.Replace("txtArt"," ");
FakturaDLL.Products pr = new FakturaDLL.Products();
if(pr.GetProduct(dd.Text))
{
((TextBox)this.FindControl (this.Pare ntForm,"tx tText"+cn) ).Text = pr.ProdText.ToString();
//txtPris1.Text = pr.ProdPrice.ToString(); - do the same thing with txtPris
}
else
MessageBox.Show("Produkten kunde inte hittas!");
}
So what I'm trying to do is, that when txtArt is changed, the textboxes on the same row chould be updated.
I have some "rows" in my form, each row has a drobpdownlist and some textbox, each control in a row is named txtArt1, txtText1, txtPris1
the number, 1 is the number of the row and txtArt is the DropdownList.
All dropdownlists are connected to private void txtArt1_SelectedIndexChang
And txtArt1_SelectedIndexChang
private void txtArt1_SelectedIndexChang
{
ComboBox dd = ((ComboBox)sender);
string cn = dd.Name.Replace("txtArt","
FakturaDLL.Products pr = new FakturaDLL.Products();
if(pr.GetProduct(dd.Text))
{
((TextBox)this.FindControl
//txtPris1.Text = pr.ProdPrice.ToString(); - do the same thing with txtPris
}
else
MessageBox.Show("Produkten
}
So what I'm trying to do is, that when txtArt is changed, the textboxes on the same row chould be updated.
ASKER
Tried this:
((TextBox)this.FindControl ("txtText" +cn, this.Controls)).Text = pr.ProdText.ToString();
private Control FindControl(string Target,Control.ControlColl ection cont)
{
Control myControl = new Control ();
foreach (Control c in cont)
{
if (c.Name == Target)
{
myControl = c;
}
else
{
if(c.Controls.Count >0)
myControl=FindControl(Targ et,c.Contr ols);
}
}
return myControl;
}
But it don't work, te reason that I wan't recursion is that all controls are placed in a tabcontrol so I need to check all controls and controls in the controls in the form.
If I have all controls in the form and not in a tabcontrol and use this private Control FindControl(string Target) and don't have recursion it works!
What am I doing wrong?
((TextBox)this.FindControl
private Control FindControl(string Target,Control.ControlColl
{
Control myControl = new Control ();
foreach (Control c in cont)
{
if (c.Name == Target)
{
myControl = c;
}
else
{
if(c.Controls.Count >0)
myControl=FindControl(Targ
}
}
return myControl;
}
But it don't work, te reason that I wan't recursion is that all controls are placed in a tabcontrol so I need to check all controls and controls in the controls in the form.
If I have all controls in the form and not in a tabcontrol and use this private Control FindControl(string Target) and don't have recursion it works!
What am I doing wrong?
simply put the my code in a function which accepts a stting and returns a control.. call this function first time using:
FindControl(textBox2.Text,
and heres the FindControl
private void FindControl(string text, Control Parent)
{
Control myControl = new Control ();
foreach(Control c in Parent.Controls)
{
if (c.Name == text)
{
myControl = c;
MessageBox.Show(myControl.
}
if (c.Controls.Count >0)
FindControl(text,c); // recursive call
}
}
ASKER
That's What I've done but it complains and says that the conversion isn't valid on this line:
((TextBox)this.FindControl ("txtText" +cn, this)).Text = pr.ProdText.ToString();
My code:
private void txtArt1_SelectedIndexChang ed(object sender, System.EventArgs e)
{
ComboBox dd = ((ComboBox)sender);
string cn = dd.Name.Replace("txtArt"," ");
FakturaDLL.Products pr = new FakturaDLL.Products();
if(pr.GetProduct(dd.Text))
{
((TextBox)FindControl("txt Text"+cn, this)).Text = pr.ProdText.ToString();
((TextBox)FindControl("txt Pris"+cn, this)).Text = pr.ProdPrice.ToString();
}
else
MessageBox.Show("Produkten kunde inte hittas!");
}
private Control FindControl(string Target,Control Parent)
{
Control myControl = new Control ();
foreach (Control c in Parent.Controls)
{
if (c.Name == Target)
{
myControl = c;
}
else
{
if(c.Controls.Count >0)
myControl = FindControl(Target,c);
}
}
return myControl;
}
if i do this in FindControl:
if (c.Name == Target)
{
myControl = c;
MessageBox.Show(c.Name);
}
Then I can see that the right name is returned so nothing seems to be wrong with the function
((TextBox)this.FindControl
My code:
private void txtArt1_SelectedIndexChang
{
ComboBox dd = ((ComboBox)sender);
string cn = dd.Name.Replace("txtArt","
FakturaDLL.Products pr = new FakturaDLL.Products();
if(pr.GetProduct(dd.Text))
{
((TextBox)FindControl("txt
((TextBox)FindControl("txt
}
else
MessageBox.Show("Produkten
}
private Control FindControl(string Target,Control Parent)
{
Control myControl = new Control ();
foreach (Control c in Parent.Controls)
{
if (c.Name == Target)
{
myControl = c;
}
else
{
if(c.Controls.Count >0)
myControl = FindControl(Target,c);
}
}
return myControl;
}
if i do this in FindControl:
if (c.Name == Target)
{
myControl = c;
MessageBox.Show(c.Name);
}
Then I can see that the right name is returned so nothing seems to be wrong with the function
ASKER
Solved it:
private Control FindControl(string Target,Control Parent)
{
Control myControl = new Control ();
foreach (Control c in Parent.Controls)
{
if (c.Name == Target)
{
myControl = c;
break;
}
else
{
if(c.Controls.Count >0)
myControl = FindControl(Target,c);
}
}
return myControl;
}
private Control FindControl(string Target,Control Parent)
{
Control myControl = new Control ();
foreach (Control c in Parent.Controls)
{
if (c.Name == Target)
{
myControl = c;
break;
}
else
{
if(c.Controls.Count >0)
myControl = FindControl(Target,c);
}
}
return myControl;
}
private Control FindControl(string controlName,Control Parent)
{
Control ct = new Control();
bool flag = false;
foreach(Control c in Parent.Controls)
{
if(c.Name == controlName)
{
ct = c;
falg = true;
}
}
if(flag == true)
{
retrun ct;
}
return null;
}
{
Control ct = new Control();
bool flag = false;
foreach(Control c in Parent.Controls)
{
if(c.Name == controlName)
{
ct = c;
falg = true;
}
}
if(flag == true)
{
retrun ct;
}
return null;
}