0

IDE: Visual Studio, C# .net 4.0

I have two identical user control uc1 and uc2 and both are having a textbox called txtbox1
now see this code and textbox1 is public in designer so it is assessable in form1.cs, Form 1 is simple windows form which is having uc1 and uc2.

In form1 see this function which i am calling in onLoad_form1 method.

UserControl currentUC; //Global variable;  

public void loadUC(string p1)
{
  //Here I want:
  if(p1 == "UC1)
  {
      currentUC = uc1;
  }
  if(p1 == "UC2)
  {
      currentUC = uc2;
  }
}

Than another function which calls update the textbox1 based on currentUC value

//on load
currentUC.textbox1.text = "UC1 called";

//Here I am getting error "currentUc does not contains definition for textbox1"  

If i do: uc1.textbox1.text = "UC1 text"; uc2.textbox1.text = "UC1 text"; //it works, But based on p1 string variable I want to make control as uc1 or uc2 than I want to access its child control. please suggest how to perform this.

please don't tell if else blocks, because This functionality I have to use in various places.

Thanks.

@Lee Answer: - works just for textbox, but I am having two usercontrols i.e. two different usercontrols not instance of it. UserControlLeft and UserControlRight and both are having same textboxes, listboxes etc (with minor design changes), and I want to access/load this based on some string "left" and "right".

yogeshkmrsoni
  • 315
  • 7
  • 21
  • In the load you have to set CurrentUc to something default, otherwise you are trying to use an unassigned variable. – Kevin Cook Jun 05 '14 at 12:13

4 Answers4

3

Since the textboxes have the same name you can look them up in the Controls collection:

TextBox tb = (TextBox)currentUC.Controls["textbox1"];
tb.Text = "UC1 called";

a better solution would be to add a property to your user control class which sets the internal text property e.g.

public class MyUserControl : UserControl
{
    public string Caption
    {
        get { return this.textbox1.Text; }
        set { this.textbox1.Text = value; }
    }
}
Lee
  • 142,018
  • 20
  • 234
  • 287
  • I am trying this but I can't use var as global variable, I need global level because in various function I have to use this variable name "tb" – yogeshkmrsoni Jun 05 '14 at 12:12
0

I think you're mixing a couple of things here.

  1. First of all, you say that you have 2 exactly the same usercontrols, do you mean the ascx files are the same, or that you have 2 instances of the same usercontrol on the page?

Let's go with all the valid options:

1. To find a control and cast it:

Assume you have the following aspx snippet:

<div>
    <uc1:MyCustomUserControl id="myControl" runat="server" />
    <uc1:MyCustomUserControl id="myControl2" runat="server" />
</div>

If you now want to access the control, you should do the following:

public void Page_Load()
{
    var myControl ((MyCustomUserControl)FindControl("MyControlName"));

    // On 'myControl' you can now access all the public properties like your textbox.
}
Complexity
  • 5,682
  • 6
  • 41
  • 84
0

In WPF you can do it like this:

//on load MAINFORM

public void SetText(string text)
{
   CLASSOFYOURCONTROL ctrl = currentUC as CLASSOFYOURCONTROL ;

   ctrl.SetText(text);
}

// in your control SUB

public void SetText(string text)
{
   textbox1.text = "UC1 called"

}

i think this should work in winforms also. And is more clean than accessing the controls from your sub-control directly

Bjego
  • 665
  • 5
  • 14
0

@Lee's method is good. Another method will be to use a public property with a public setter (and textbox doesn't need to be public this way).

or an interface (this way you don't care what class you have at the given moment - and no ifs):

public interface IMyInterface
{
  void SetTextBoxText(string text);
}

public partial class UC1: UserControl, IMyInterface
{
   public void SetTextBoxText((string text)
   {
       textBox1.Text=text;
   }

//...
}

public partial class UC2: UserControl, IMyInterface
{
   public void SetTextBoxText((string text)
   {
       textBox1.Text=text;
   }

//...
}

using the code:

((IMyInterface)instanceOfUC1).SetTextBoxText("My text to set");
((IMyInterface)instanceOfUC2).SetTextBoxText("My text to set");
Arie
  • 5,251
  • 2
  • 33
  • 54
  • This is nice way, but of each child control such as textbox, listbox I have to explicitly type cast it back. I have to put if else block. How will you handle this. – yogeshkmrsoni Jun 05 '14 at 12:45
  • @user3593678 this is an interesting way of doing something based on a type of the object: http://stackoverflow.com/a/4478535/891715 – Arie Jun 06 '14 at 06:24