473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

set property of a control

Hi all

I need to change some of my controls properties at run time,

I build this function in C# which sets some simple properties

I want to add some more properties, and extend the use, but my switch
statement is getting bigger and bigger.

Am I doing it right? Is there any other way to do this?

Thank you

Sharon
SOURCE
:------------------------------------------------------------------------------------------------------------------------------------(
public const string TEXT = "text" ;
public const string BACKCOLOR = "backcolor" ;
public const string TAG = "tag" ;

private void setControlProperty(Control ctr,string property,Object val)
{

switch(property){

case TEXT :
ctr.Text = val.ToString();
break ;
case BACKCOLOR :
ctr.BackColor = Color.FromArgb((int)val);
break ;

case TAG :
ctr.Tag = val.ToString() ;
break ;
}
}

private void button1_Click(object sender, System.EventArgs e){

setControlProperty(this.button1,TEXT,"youClicked Me
!!!");
setControlProperty(this.button1,BACKCOLOR,Color.Re d.ToArgb());
}

Nov 17 '05 #1
4 1503
Sharon <Sh*******@hotmail.com> wrote:
I need to change some of my controls properties at run time,

I build this function in C# which sets some simple properties

I want to add some more properties, and extend the use, but my switch
statement is getting bigger and bigger.

Am I doing it right? Is there any other way to do this?


Well, you can do it using reflection - use typeof(Control).GetProperty,
and then call SetValue on the PropertyInfo which is returned.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Thank you Jon

I don't understand what are all the SetValue prams ?

Is it supposed to be like this:

private void setControlProperty(Control ctr,string property,Object val)

{

ctr.GetType().GetProperty(property).SetValue(val);

}

Thank you

Sharon



"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sharon <Sh*******@hotmail.com> wrote:
I need to change some of my controls properties at run time,

I build this function in C# which sets some simple properties

I want to add some more properties, and extend the use, but my switch
statement is getting bigger and bigger.

Am I doing it right? Is there any other way to do this?


Well, you can do it using reflection - use typeof(Control).GetProperty,
and then call SetValue on the PropertyInfo which is returned.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Sharon <Sh*******@hotmail.com> wrote:
Thank you Jon

I don't understand what are all the SetValue prams ?
Look in MSDN for the details.
Is it supposed to be like this:


No. You need to specify the instance to set the property on - the type
isn't specific to the instance.

There are examples in MSDN though, I believe.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
thank you Jon

it was :
Color rndColor = Color.FromArgb(rand.Next(255),
rand.Next(255),rand.Next(255) ) ;

myPropertyInfo = _myControlType.GetProperty("BackColor");

myPropertyInfo.SetValue(_myControl, rndColor, null);

Sharon


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sharon <Sh*******@hotmail.com> wrote:
Thank you Jon

I don't understand what are all the SetValue prams ?


Look in MSDN for the details.
Is it supposed to be like this:


No. You need to specify the instance to set the property on - the type
isn't specific to the instance.

There are examples in MSDN though, I believe.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Job Lot | last post by:
I have added a ProgressBar control to StatusBar control and created a custom property as below, to show the properties of the ProgressBar in Property Window of StatusBar. <Browsable(True)> _...
1
by: Georg Scholz | last post by:
Hello, The class "Control" contains a documented Property "ControlType". So for example, in a form, you can write code like this: Dim c as control set c = me.Controls("textbox1") if...
6
by: Tom Kiefer | last post by:
Question: If I have an ASP.NET User Control which defines/exposes a property that the page can use to specify a mode or data subset for the control to use, is there a way to tell the @OutputCache...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
1
by: shapper | last post by:
Hello, I am creating a user control where an Asp.Net control is used. It can be either a button, an image button or a label. I am trying to "expose" the Asp.Net control properties and events...
2
by: Benton | last post by:
Hi there, I'm creating a custom server control, inheriting from TextBox. It has this AsDateTime property that returns the textbox contents converted to the nullable DateTime data type, as...
3
by: R.A.F. | last post by:
Hi, I have a custom control in which i have a property based on CollectionBase class. like the following one : //------- XGrid.cs file --------------// public ColumnsCollection Columns {
10
by: Derek Hart | last post by:
I can set focus to my property grid by using propgrid.Focus - but how can I set the default property that is always first? Or just set focus to the default property? Can this be done?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.