help Newbie. How do I reference a forms control from another class

R

RobW

I am a newbie to the wonders of C# programmming and I would be
grateful if someone could help me! I would like to create a class that
will change the properties of a label or text control (such as back
color) during a mouse over event. I would like the class to be generic
so that it may be used in other projects. I looked at an example from
VS help and........ it doesn't work. The class does not seem to be
able to reference the control.

public class myMouseEventClass
{
protected Label label1;
// Function signatures must match the signature of the
// MouseEventHandler class.
public void myMouseDown(object sender, MouseEventArgs ex)
{
if (ex.Button == MouseButtons.Left)
{
// Insert code to execute when a mouse down event is
// received, and the left button is clicked.
}
}
// A method that shows you where you are in the control
// that is receiving the event.
public void myWhereAreWe(object sender, MouseEventArgs ex)
{
label1.Text = "Mouse is at X: " + ex.X + " Y: " + ex.Y ;
}
}

[Visual Basic, C#] You must create an instance of this class. You must
also
set the event handlers; you can do this in your constructor.

[C#]
myMouseEventClass myEvents = new myMouseEventClass() ;

public Form1()
{
InitializeComponent();

textBox1.MouseDown += new
MouseEventHandler(myEvents.myMouseDown);
textBox1.MouseMove += new
MouseEventHandler(myEvents.myWhereAreWe);
}
When one of the specified events is raised in the control, the
attached
method is called and the application can execute code in response to
the
event.


Can anyone help with this as this is confusing the hell out of me?

Thanks in advance
 
R

RJ

You should avoid directly referencing controls which are contained within
another class. This is not a good design approach.
Instead, create public properties, in your form class, which are visible,
and safer to access from the external class.
As for how do you get a reference to the form object... Declare a variable
of type Form ( System.Windows.Forms.Form)
..
Create an instance of the form object, assigning the instance to the Form
variable you declared. This variable must have the proper access modifier
( Public, etc.) to allow the desired visibility in your project or solution.
 
B

Bjorn A

...
I would like to create a class that will change the
properties of a label or text control (such as back
color) during a mouse over event. I would like the
class to be generic so that it may be used in other
projects.

If you wan't to use an "outside class" to handle this in a generic way, you
must handle the controls in a generic way as well.
if (ex.Button == MouseButtons.Left)

This refers to which button was clicked on the mouse, not any "button" on
your form.
label1.Text = "Mouse is at X: " + ex.X + " Y: " + ex.Y ;

Here you refer to the Label-instance you have inside your handler-class.

In no case you're actually refering to the control, which is very easy. The
first argument sent to the method is the sender-object, such as your
textbox. Just do a typecast of the reference inside your method:

Control c = (Control) sender;

Then you can change whatever you like on the Control.

Simple example:

/// Handler.cs /////////////////////////////////////

using System;
using System.Windows.Forms;
using System.Drawing;

namespace Handlers
{
public class MouseHandler
{
Color backColor;
Color foreColor;
Color oldBackColor;
Color oldForeColor;

public MouseHandler(Color b, Color f)
{
backColor = b;
foreColor = f;
}

public void mouseEnter(object sender, EventArgs ex)
{
Control c = (Control) sender;
oldBackColor = c.BackColor;
oldForeColor = c.ForeColor;

c.BackColor = backColor;
c.ForeColor = foreColor;
}

public void mouseLeave(object sender, EventArgs ex)
{
Control c = (Control) sender;
c.BackColor = oldBackColor;
c.ForeColor = oldForeColor;
}
}
}
/////////////////////////////////////////////////////////

In your Form you now have to do a couple of things:

Instantiate the above mentioned class:

private Handlers.MouseHandler h =
new Handlers.MouseHandler(Color.Firebrick, Color.Gold);

Add the handler to the events:

textBox1.MouseEnter +=new EventHandler(h.mouseEnter);
textBox1.MouseLeave +=new EventHandler(h.mouseLeave);

HTH
Bjorn A
 
P

Phil

I think its all covered in the above posts. My tuppenceworth is that you
don't need to create an instance of the mouseEventHandler class if you
declare the methods in it as static.

ie (following assumes two textboxes on Form1, back colors change as cursor
hovers over them)

namespace MouseEvent
{
/// <summary>
/// Summary description for myMouseEvent.
/// </summary>
public class myMouseEvent
{
public myMouseEvent()
{
//
// TODO: Add constructor logic here
//
}
public static void myMouseEnter(object sender, EventArgs e)
{
((Control)sender).BackColor = Color.Aqua;
}
public static void myMouseLeave(object sender, EventArgs e)
{
((Control)sender).BackColor = Color.White;
}
}
}

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
textBox1.MouseEnter +=new EventHandler(myMouseEvent.myMouseEnter);
textBox1.MouseLeave +=new EventHandler(myMouseEvent.myMouseLeave);
textBox2.MouseEnter +=new EventHandler(myMouseEvent.myMouseEnter);
textBox2.MouseLeave +=new EventHandler(myMouseEvent.myMouseLeave);
}

Phil

RobW said:
I am a newbie to the wonders of C# programmming and I would be
grateful if someone could help me! I would like to create a class that
will change the properties of a label or text control (such as back
color) during a mouse over event. I would like the class to be generic
so that it may be used in other projects. I looked at an example from
VS help and........ it doesn't work. The class does not seem to be
able to reference the control.

public class myMouseEventClass
{
protected Label label1;
// Function signatures must match the signature of the
// MouseEventHandler class.
public void myMouseDown(object sender, MouseEventArgs ex)
{
if (ex.Button == MouseButtons.Left)
{
// Insert code to execute when a mouse down event is
// received, and the left button is clicked.
}
}
// A method that shows you where you are in the control
// that is receiving the event.
public void myWhereAreWe(object sender, MouseEventArgs ex)
{
label1.Text = "Mouse is at X: " + ex.X + " Y: " + ex.Y ;
}
}

[Visual Basic, C#] You must create an instance of this class. You must
also
set the event handlers; you can do this in your constructor.

[C#]
myMouseEventClass myEvents = new myMouseEventClass() ;

public Form1()
{
InitializeComponent();

textBox1.MouseDown += new
MouseEventHandler(myEvents.myMouseDown);
textBox1.MouseMove += new
MouseEventHandler(myEvents.myWhereAreWe);
}
When one of the specified events is raised in the control, the
attached
method is called and the application can execute code in response to
the
event.


Can anyone help with this as this is confusing the hell out of me?

Thanks in advance
 
R

RobW

Cheers Guys!

But how for instance would you reference other controls in the form
(as in the VS help example). So if a MouseDown event occurs in the
textBox1 control the label1 text property displays the X Y location of
the mouse.

Sorry my first post probably lacked clarity.

myForm1 has two controls

TextBox name: textBox1
Label name: label1

If a mousedown event is fired from textBox1 set the Text or the
BackColor property of label1 to a value from a class outside that of
myForm1.

The process that I don't understand is how you access the properties
of a control on a form other than that which the event was fired from
a class outside the form.

Thanks so much for the help guys!
 
B

Bjorn Abelli

"RobW" ...
But how for instance would you reference other controls in the form
(as in the VS help example). So if a MouseDown event occurs in the
textBox1 control the label1 text property displays the X Y location of
the mouse.

Lock back to the post "RJ" wrote:

"You should avoid directly referencing controls which are
contained within another class. This is not a good design
approach.
Instead, create public properties, in your form class,
which are visible, and safer to access from the external
class."

Let's say your Form-class is named "MyForm".

Then look again at my example. In my example the constructor of "Handler"
took only two Colors as arguments, but it could very well take yet another
argument, such as a "MyForm", which you can assign to an instance-variable
of that type in "Handler".

In the eventhandling methods in "Handler" you then have access to all the
public properties of your form.


// Bjorn A
 
R

RobW

Is there any chance that you could show an example of the above. Sorry
I really am rather new at this programming lark!
 
B

Bjorn Abelli

again...
Is there any chance that you could show an example
of the above. Sorry I really am rather new at
this programming lark!

A snipped example of how it can be done. There are several other ways as
well, but this might show some ideas on how it can be done.

-------------------------------------------
// Create an interface that declares methods
// for receiving the result of the event(s):

interface MouseCallbacker
{
void MouseClick(int x, int y);
}

-------------------------------------------
// Create the class that defines the methods
// that implements the eventhandling. This could (should?)
// also be defined in an interface first to make it
// a truly generic example, but I skip
// that for this example...

class MouseHandler
{
MouseCallbacker callback;

public MouseHandler(MouseCallbacker mc)
{
callback = mc;
}
public void MouseDown(object sender, MouseEventArgs ev)
{
callback.MouseClick(ev.X, ev.Y);
}
}
-------------------------------------------
NB! The example above is just an example. With some reading on the
eventhandling and what arguments each eventhandler must implement, it can be
expanded to any type...
-------------------------------------------
// Modify your Form-class to implement
// the interface MouseCallbacker...

public class Form1 : System.Windows.Forms.Form, MouseCallbacker
{
// Code unnecessary for the example is snipped away...

...

public void MouseClick(int x, int y)
{
// This is just an example, presuming
// you have an object textBox1...

this.textBox1.Text = "X:" + x + " Y:" + y;
}

public Form1()
{
InitializeComponent();

// Instantiate the eventhandling object.
// As the constructor takes a MouseCallbacker
// and your form now implements it, just
// send in itself...

MouseHandler mh = new MouseHandler(this);

// Associate the appropriate method in the
// eventhandling object with any event you fancy.
// In this example a Mouse-click on the form
// itself triggers the event...

this.MouseDown += new MouseEventHandler(mh.MouseDown);
}

// More code unnecessary for the
// example is snipped away...

...

}
}

-------------------------------------------

The interface MouseCallbacker can of course be exluded, as you could
exchange it for "Form1" directly in the "MouseHandler".

But in this manner it shows from start that you can send any object that
implements that interface if you choose not to show the result in your form,
but send it to another object instead (more generic this way).

If you have any more questions on this now, I really suggest you read some
beginner-tutorials on object-oriented programming and eventhandling... ;-)

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top