473,323 Members | 1,550 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,323 software developers and data experts.

Access to color variable in a class

Hi,
I have the following code. ( Sorry if it is a little rough or improperly
written ).

<########## CODE BEGIN ##########>

public class TestClass : System.Windows.Forms.UserControl
{
public TestClass( ref Color InputColor )
{
Button btn1 = new Button();
btn1.Text = "Button";
btn1.Click += new System.EventHandler( btn1_Click );
this.Controls.Add( btn1 );
}

private void btn1_Click( object sender, EventArgs e )
{
}
}

<########## CODE END ##########>

I wish to change 'InputColor' in the button click event handler, but if I
try to change 'InputColor' with the above code it fails to compile with an
error similar to "InputColor does not exist in the class or namespace blah
blah". Sure, I can understand that, however, can you please advise me of the
best way to achieve what I have described.
Thank you for any help.
Nov 15 '05 #1
3 2471
I've modified your code slightly. You need to have a class level field in
order to expand the scope of InputColor. You can assign the ref value of
InputColor to the field because it creates a shallow copy so when you modify
inputColor, as long as you don't use "new" or some method that creates a new
color rather than assigns a color, your change will show up in InputColor.

Dale
public class TestClass : System.Windows.Forms.UserControl
{
private Color inputColor;
public TestClass( ref Color InputColor )
{
inputColor = InputColor;
Button btn1 = new Button();
btn1.Text = "Button";
btn1.Click += new System.EventHandler( btn1_Click );
this.Controls.Add( btn1 );
}

private void btn1_Click( object sender, EventArgs e )
{
//Change the inputColor value here.
}
}

"Anonymous" <an*******@anonymous.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Hi,
I have the following code. ( Sorry if it is a little rough or improperly written ).

<########## CODE BEGIN ##########>

public class TestClass : System.Windows.Forms.UserControl
{
public TestClass( ref Color InputColor )
{
Button btn1 = new Button();
btn1.Text = "Button";
btn1.Click += new System.EventHandler( btn1_Click );
this.Controls.Add( btn1 );
}

private void btn1_Click( object sender, EventArgs e )
{
}
}

<########## CODE END ##########>

I wish to change 'InputColor' in the button click event handler, but if I
try to change 'InputColor' with the above code it fails to compile with an
error similar to "InputColor does not exist in the class or namespace blah
blah". Sure, I can understand that, however, can you please advise me of the best way to achieve what I have described.
Thank you for any help.

Nov 15 '05 #2
"Anonymous" <an*******@anonymous.com> wrote:
Hi,
Unfortunately this does not appear to be working. I have written and
attatched ( in code form ) a small application to demonstrate the behaviour.
The application has a panel and 2 buttons. The panel starts off as being
yellow. My user control has a button which attempts to change the referenced
color to red. The button on the main form attempts to change the color of
the panel via the referenced color 'PanelColor'.
When I run the application it is clear that the referenced color is not
being changed in the way that I expect.

Thank you for your help.


What you're trying wouldn't work for several reasons. You'd have to
do something similar to the following instead.

public class TestClass : System.Windows.Forms.UserControl
{

private Panel myPanel;

public TestClass(Panel p)
{
myPanel = p;
}

private void TurnPanelRedButton_Click( object sender, EventArgs e )
{
myPanel.BackColor = Color.Red;
}

}
Nov 15 '05 #3
Thank you everyone for your help.
Nov 15 '05 #4

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

Similar topics

9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
6
by: Christian H | last post by:
Hi! I've created a custom control (myDrawControl) that deals with drawing. This control is then added to a form( myMainForm) Now, whenever something is dragged and dropped onto myDrawControl ,...
4
by: Al Murphy | last post by:
I have a windows application called "WindowsApplication1". I have a variable of tyoe DataSet called myDataSet as shown below: namespace WindowsApplication1 { public class Form1 :...
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
5
by: Harold Hsu | last post by:
Hi all, What's the default access type of a property declared in an interface? The one I'm looking at is IBindingList: Public Interface IBindingList .... ReadOnly Property AllowEdit As...
2
by: Corey B | last post by:
Is there a way for an instance of a custom class to access an ASPX page level variable? I know that I can access a Session variable from within a class using the following code: myClassVar =...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
9
by: LamSoft | last post by:
Class B { public B() {} } Class A : B { public static string ABC = "myABC"; public A() {} }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.