2

I did this in C# -

 foreach (Control ctl in this.groupBox3.Controls)
        {
            if ((ctl is Textbox) && (ctl.Name.Substring(0, 1) != "l"))
            {
                Textbox tmp= (Textbox)ctl;
                tmp.text = "whatever";

Im trying to do something similar in WPF but this time i want to find the textbox based on a string.

So I tried

TextBox temp = (TextBox).Findcontrol("txtboxNumbers");

but it complaints that the "(Textbox)" is a type but its used like a variable and it cant find the Findcontrol method :'(

Batista
  • 141
  • 1
  • 2
  • 13
  • possible duplicate of [WPF ways to find controls](http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls) – M.Babcock Feb 16 '12 at 13:39
  • http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls This question looks it may help you. – Altair Feb 16 '12 at 13:39

2 Answers2

3

Ofcource you cannot. Doing this

(TextBox).Findcontrol("txtboxNumbers");

You try to invoke method Findcontrol on Type. Instead try (in window or control *.cs file):

TextBox oTextBox = FindName("txtboxNumbers") as TextBox;
Kamil Lach
  • 4,519
  • 2
  • 19
  • 20
1

You can find the control with this.FindControl:

    TextBox txt = this.FindControl("txtboxNumbers") as TextBox;

    // check if the control was found
    if(txt != null)
    {
       txt.Text = "whatever you want";
    }
Schiavini
  • 2,869
  • 2
  • 23
  • 49