Add a Property for a UserControls
-
Hello all and Happy new Year :) I have a UserControl which contains a Label and a TextBox and I want to set the Label's Text in my main form. I know I can get a string as parameter in UC's constructor or define the Label, publicly. But I wonder if I can set the Text in Design mode. I mean is there any way to add a property for my UC so when I open Properties tab I can see that and change its value? I hope I could explain well. Thanks a lot :)
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
-
Hello all and Happy new Year :) I have a UserControl which contains a Label and a TextBox and I want to set the Label's Text in my main form. I know I can get a string as parameter in UC's constructor or define the Label, publicly. But I wonder if I can set the Text in Design mode. I mean is there any way to add a property for my UC so when I open Properties tab I can see that and change its value? I hope I could explain well. Thanks a lot :)
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
Have you tried it?
-
Have you tried it?
Actually I didn't know from where I should start. I tried constructor but I have error adding UC in my windows form. I defined the Label publicly but I could only access the Label this way:
UserControl.Label
. I searched MSDN for Property and Attribute but I couldn't find anything yet. Thanks for replyI died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
-
Hello all and Happy new Year :) I have a UserControl which contains a Label and a TextBox and I want to set the Label's Text in my main form. I know I can get a string as parameter in UC's constructor or define the Label, publicly. But I wonder if I can set the Text in Design mode. I mean is there any way to add a property for my UC so when I open Properties tab I can see that and change its value? I hope I could explain well. Thanks a lot :)
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
You can override the Text property of the usercontrol so as to set the entered text into the label. Something like what is given below. [Browsable(true)] public override string Text { set { label1.Text = value; } } This will let you set the text in design time.
-
You can override the Text property of the usercontrol so as to set the entered text into the label. Something like what is given below. [Browsable(true)] public override string Text { set { label1.Text = value; } } This will let you set the text in design time.
-
You can override the Text property of the usercontrol so as to set the entered text into the label. Something like what is given below. [Browsable(true)] public override string Text { set { label1.Text = value; } } This will let you set the text in design time.
:( I created two instances of my UC in my Form and when I change the Text value of one of them, both will change! I wrote code below:
[Browsable(true)]
public string LabelText
{
get { return label1.Text; }
set { label1.Text = value; }
}I couldn't understand what is the problem, do you have any idea?
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
-
:( I created two instances of my UC in my Form and when I change the Text value of one of them, both will change! I wrote code below:
[Browsable(true)]
public string LabelText
{
get { return label1.Text; }
set { label1.Text = value; }
}I couldn't understand what is the problem, do you have any idea?
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
I just tried using the same code that u gave and it seems to work fine for me. Can you give me the code for the control and also the code for ur form. It might help.
-
:( I created two instances of my UC in my Form and when I change the Text value of one of them, both will change! I wrote code below:
[Browsable(true)]
public string LabelText
{
get { return label1.Text; }
set { label1.Text = value; }
}I couldn't understand what is the problem, do you have any idea?
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
-
How is the label1 defined in your UC, is it static or any other reasons why it could be shared along the instances?
The need to optimize rises from a bad design.My articles[^]
Solved Mika and Member 4470354, I think I was changed PropertyBinding, DatBinding or I don't know, something similar... :laugh: I reset the values. Thank you both.
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog