views:

63

answers:

2

Hi, I have a user control with a textbox in a win forms application. I would like to change the property of that textbox using the properties window of visual studio . I am using that control in various forms of same project ,is it possible? I have set the modifier property of text box as public and set following property in the user control:

  public TextBox mytextBox
        {
            get { return textBox1; }
            set { textBox1 = value; }
        }

Thanks in Advance.

A: 
[TypeConverter(typeof(ExpandableObjectConverter))]
public TextBox mytextBox
{
    get { return textBox1; }
    set { textBox1 = value; }
}

Notes:

  1. From the perspective of the PropertyGrid, the setter has no benefit in this case; the properties of the already-assigned TextBox are being modified in-place.
  2. Remember to create an initial value, and to add the TextBox to the UserControl's control-collection. If you used the VS designer to create the TextBox, this should have been done already. If you find that the VS designer method InitializeComponents() is undoing your changes, create and add the control yourself.
  3. You may have to rebuild the project and/or reopen the Forms designer for the change to be visible.
  4. Off-topic: Use Pascal-case for properties, and the auto-implemented get;set; pattern for readability, if at all possible.
Ani
the property does gets changed in the design time but when i run the program all the custom properties that I set are lost!
Thunder
This because of the InitializeComponents() call; you can see the changes in your Form1.designer.cs file. One way to solve this would be to not rely on the designer for this, add the control yourself in the constructor.
Ani
+1  A: 

What is the intent of doing this? Are you trying to have "one TextBox control shared by multiple forms" (that is not really practical). However you can set up your forms in such a way as to have all forms update in response to a single change.

Swanny
@Swanny It has a very practical use ,I am creating a control that has a text box ,and some other buttons and labels ,I use this control in many palces ,but however in some implementation I would like the text box to be for example multiline , or Backcolor changed for instance .
Thunder
So do you want to use a TextBox that already exists on each form? What about having a string property so you can set it to the name of the TextBox you want to use, and then at run time, after initialisation, find the control and get a referece to it. Alternatively you could have the TextBox in the control and simply expose the properties you want to alter that text box as properties of your control. The later is probably the more conventional.
Swanny