FindControl.

Hi everyone,

Does anyone know what would be the similar of FindControl method from Web library to from Windows.Forms library I need to get what controls do I have inside a panel, like TextBox, etc.

Thanks in advancing.

Doria




Answer this question

FindControl.

  • Andy Harjanto

    Gotcha!

    Thanks a lot!



  • updates

    I found a new problem:

    The example above (mp_control) returns a String. But I need something that returns a Control (the current control inside the loop). I found some method like GetNextControl, but it does not help. Follow my code:

    +++

    public virtual void bindControl_Object (Control container)

    {

    PropertyInfo [ ] campos = this.GetType().GetProperties();

    foreach (PropertyInfo c in campos)

    {

    Control ctr = container.FindControl // for System.Web.UI

    Control ctr = container. // for System.Windows.Forms

    if (ctr is DropDownList)

    {

    // TODO

    }

    }

    }

    +++

    The GetType().Assembly.GetName().Name == " " returns a String and I can not see how to make it work. If someone could help me, I appreciate. Also, what is the best tool for the developer inside VS 2005 gets the all whole picture of System.Windows.Forms; docs, what methods, examples.

    Thanks again,

    Doria



  • Terrence Chan

    You're welcome!

    Only click in th yes button, please!


  • Vasillio

    please refrain from using the type as string in testing an instance of an object if it is of this kind or that use as instead and test for null.

    foreach(Control control in this.Controls)
    {
    TextBox textBox = control as TextBox;
    if(textBox != null)
    {
    //do whatever...
    }
    }


  • Frederic01

    Tabas, thanks a lot for your help. It really helped me a lot!

    Today, I passed some steps about that code and make it work just fine, but now I have a kind of new issue. Lot the code below:

    public virtual void vincularDadosObjeto(DataRow dr)
    {
    PropertyInfo[] campos = this.GetType().GetProperties();
    foreach (PropertyInfo c in campos)
    {
    if (!this.ehPropriedadePrivada(c.Name))
    {
    if (this.ehPropriedadeIdObjeto(c.Name))
    {
    Persistente obj = (Persistente)c.GetValue(this, null);
    obj.id = Convert.ToInt64(dr[c.Name]);
    obj.buscar();
    }
    else
    {
    //MessageBox.Show(c.Name);
    //MessageBox.Show(Convert.ToString(dr[c.Name]));

    c.SetValue(this, dr[c.Name], null);
    //Erros.Add(new ErroBD(2001, erro.Message));
    }
    }
    }
    }

    Well, the line c.SetValue DOES NOTHING, even I know that the two message box above SHOWS the values. Why could happened it I already spend a lot of time in the debug mode, looking the values from database ok and waiting to map to the object this. But even after I see the values with MessageBox, I can not get the SetValue to map correct the values between the layers. Any ideas

    Thanks in advancing.

    Doria



  • indortas

    Hi Doria,

    I see that you want change the value of a property. I use the following code to do this:

    PropertyDescriptorCollection props;
    PropertyDescriptor propParent;


    QPictureBackGround mp_object = new QPictureBackGround();
    props = TypeDescriptor.GetProperties(mp_object);
    propParent = props["PropertyNameToChangeValue"];
    propParent.SetValue(mp_object, NewValue);

    If you look the code placed above, I'm using a PropertyDescriptorCollection object to get all properties from an object. Using the PropertyDescriptor  class, i can manipulate these properties using the SetValue() method.

     

    I hope that this code gives an idea to you!

    Regards

    CFQM


  • Pico48

    Thanks, I will try it and let you know. The major difficult is to know "all the possibilities"; documentation and examples of methods I can use from a specific library like System.Windows.Forms.

    Doria



  • Joao Paulo Oliveira

    Guess I could not be exactly. Here goes again my code that works for Web controls. I would like to make it work for Windows Controls too:

    +++

    public virtual void vincularControleObjetoW(System.Windows.Forms.Control container)
    {
    PropertyInfo[] campos = this.GetType().GetProperties();

    foreach (PropertyInfo c in campos)
    {
    if (!this.ehPropriedadePrivada(c.Name))
    {
    //System.Web.UI.Control ctr2 = container.FindControl(c.Name);
    //WHAT I NEED TO CHANGE IS BELOW...THE LINE ABOVE WORKS!

    System.Windows.Forms.Control ctr = container.Controls;

    if (ctr is System.Windows.Forms.ListBox)
    {
    System.Windows.Forms.ListBox ddl = (System.Windows.Forms.ListBox)ctr;
    this.setObjetoAssociado(c, ddl.SelectedValue);
    }
    else if (ctr is System.Windows.Forms.TextBox)
    {
    System.Windows.Forms.TextBox txt = (System.Windows.Forms.TextBox)ctr;
    this.setObjetoAssociado(c, txt.Text);
    }
    }
    }
    }

    +++

    That’s was the error:


    Error 1 Cannot implicitly convert type 'System.Windows.Forms.Control.ControlCollection' to 'System.Windows.Forms.Control'


    +++



  • Ed Dudenhoefer

    Irating a collection using foreach...next statement allows to you access directly to the control.

    foreach (TextBox mp_control in this.Controls)

    {

    //If you want to set a text in the textbox....

    if mp_control.Name == "txtUserName"

    {

    mp_control.Text = "Put your name here...";

    // set the focus in the control...

    mp_control.Focus();

    //....whatever....

    }

    }


  • SPeddi

    Could you give to me a simple example about what do you mean

    Thanks,

    Doria



  • RichKorea

    Hi,

    Exceuse me for the intromission but here is an example:

    foreach (Control mp_control in this.Controls)

    {

    if (mp_control.GetType().Assembly.GetName().Name == "TextBox")

    {

    // ToDo: some task or statement

    }

    }

    Or you maybe want search for an specific typel:

    foreach (TextBox mp_control in this.Controls)

    {

    // ToDo: some task or statement with each textbox control

    }

    Regards


  • Mhsn

    This line is firing the exception:

     System.Windows.Forms.Control ctr = container.Controls;

     

    You can't set into a Control object a collections of controls. I don't understand the reason why you does not use the code placed above. When you use a foreach statement to "navigate" or iterate into a collection you pointer variable is accessing currently a control into the collection. So..., only you need check the type of the control and do anything!.

    string m_assemblyname = "";

    foreach (Control mp_control in this.Controls)

    {

    m_assemblyname = mp_control.GetType().Name;

    switch (m_assemblyname)

    {

    // If the assembly name is a listbox control

    case "ListBox":

    this.setObjetoAssociado(c, mp_control.SelectedValue);

    break;

    //if is a texbox control

    case "TexBox":

    this.setObjetoAssociado(c,mp_control.Text);

    break;

    }

    }

    Regards

    Tabas

     


  • VB net Beginner

    iterate on the control collection as the previous posts:

    foreach(Control control in container.Controls)
    {
    //whatever...
    }



  • John S1

    Apparently this method was left out of windows forms. Too bad, I can see cases where it would be useful. You will just have to iterate recursively through the Controls collection of your form.

  • FindControl.