Search:

Custom Search
_________________________________________________________________

Friday, May 30, 2008

Advance C# programming, overload unary plus.

C# let’s you overload the behaviour of some unary operator such as: unary plus, minus, prefix increment, decrement, true keyword, false keyword and more.

Ok, now let’s make a small example on how to overload the unary plus. For this, you need to define a method (in the class that you want to implement the overload) containing a few things: the return type, the operator keywords (in this case +), the parameter that usually is the object or structure where the method is being defined.

Let’s make now a small example:

-First we create a small class and define the overload plus:

public class MyClass
{

public double value;

public static MyClass operator +(MyClass Mc)

{
MyClass myC = new MyClass();
if (Mc.value <0)
myC.value = -Mc.value;

else
myC.value = Mc.value;

return myC;
}

}

-Main:

static void Main(string[] args)
{

MyClass MyCs = new MyClass();
MyClass MyCsP = new MyClass();

//Both negative and positive values to see effect
MyCs.value = -10.80;
MyCsP.value = 300;

Console.WriteLine("Before: " + MyCs.value);
Console.WriteLine("\t" + MyCsP.value);

//Using the + operator triggers the method
MyCs = +MyCs;
MyCsP = +MyCsP;

Console.WriteLine("After: " + MyCs.value);
Console.WriteLine("\t" + MyCsP.value);

Console.Read();

}

-Console:

Monday, May 19, 2008

MouseDown Event in C#.

The MouseDown event is the one I personally use every time a control needs both events for left and right click (middle click is also supported).
So lest say we want to add the mouseDown event to a Button control somewhere in a windows form. So you can go ahead and add this event.

This header shows appear in the form:

private void button2_MouseDown(object sender, MouseEventArgs e)

So lets proceed to add some code here.
The way to see which button has been clicked is by using a switch statement:

switch (e.Button)
{

case MouseButtons.Left:

//Place here event for left click

break;

case MouseButtons.Right:

//Place here event for right click

break;

default:

break;

}

Great, now your application can distinguish between lest and right click.
To make sure this works, you can add a messageBox saying what event should run.

Complete Code:

private void button2_MouseDown(object sender, MouseEventArgs e)
{

switch (e.Button)
{

case MouseButtons.Left:

MessageBox.Show(this, "Left Button Click");

break;

case MouseButtons.Right:

MessageBox.Show(this, "Right Button Click");

break;

default:

break;

}
}

Wednesday, May 7, 2008

Creating Tooltip to Button Control from scratch

First of all, start by creating a new Windows form application in Visual Studio 2005. (You can download the Express Edition clicking the link on My Links section).

After that, place a Button Control somewhere in the form as you see below:

Ok, now we can proceed to add the tooltip event to the button1. This is actually very easy. I will show you the way I use for doing this.

Navigate through the toolbox until you find the tooltip control. Grab the control and place it somewhere in the form.

You will see that the Control is placed in a bar below the form. Also the Tooltip Control is created with the name tooltip1. Ok, now let’s add some event and write a few code lines.

Click the button1 and under properties double click the MouseHover event. This creates the method that we use for the tooltip control. Now in the method body add this line of code:

toolTip1.SetToolTip(button1, "Information for Button1");

Great, now run the app and see what happens. It should look something like this:


Some other cool features on this C# Control:
-
toolTip1.IsBalloon = true;
(Converts to a ballon window)



- toolTip1.ToolTipIcon = ToolTipIcon.Error;
(Assign icon to the tooltip)

- toolTip1.ToolTipTitle = "This is the Title";
(Assigns a title)

Complete Code:

private void button1_MouseHover(object sender, EventArgs e)
{
toolTip1.SetToolTip(button1, "Information for Button1");
toolTip1.IsBalloon = true;
toolTip1.ToolTipIcon = ToolTipIcon.Error;
toolTip1.ToolTipTitle = "This is the Title";
}