Thursday, April 18, 2013

Some Windows Forms magic in .NET


While my primary focus is mostly on classic “heads-down” style data-entry applications, sometimes I reflect back upon my college days when I used to enjoy programming with stuff like graphics, games, networking, cryptography, etc..

In this brief article we will look at some creative things that could be performed with .NET WinForms using C# or VB.NET.

  1. Broken Arrow: If you are aware of the Form’s Region property, you must already know what all shapes you can give to your Form. You can override the Form’s default graphics-path which is a rectangle, and change it into the shape of your choice such as a polygon. In the below example, we convert the shape of the Form to a broken arrow by creating an array of System.Point objects and creating a polygon:



C#:
private void frmBrokenArrow_Load(object sender, EventArgs e)
{
Point[] points = new Point[3];
points[0] = new Point(0, 0);
points[1] = new Point(150, 0);
points[2] = new Point(0, 200);

System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddPolygon(points);

this.Region = new Region(path);
}

VB.NET:

Private Sub frmBrokenArrow_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim points() As Point = New Point(3) {}
points(0) = New Point(0, 0)
points(1) = New Point(150, 0)
points(2) = New Point(0, 200)
Dim path As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()
path.AddPolygon(points)
Me.Region = New Region(path)
End Sub



Figure-1: A Broken Arrow Form



  1. TransparencyKey: While Opacity is a property which is well known for making your Windows Forms transparent, TransparencyKey is lesser known and used. This property is used to make only some parts of your Form transparent. Basically, this is a color property which makes all controls transparent which has this color set in its background. For instance, in case of below Form, I’ve set the TransparencyKey property to RGB(255, 224, 192). The same color is also set as the BackColor of Label1 (which is represented by the broken window in between).



Figure-2: The Form with a broken Window


  1. Opacity: The Opacity property is straightforward. It sets the transparency of your Form. The Form with Opacity set to 1 (100%) is fully opaque, whereas 0 (0%) means fully transparent. Below is a demo of a Form becoming gradually transparent, and then back to being opaque.


C#:


private void frmOpacity_Load(object sender, EventArgs e)
{
this.Opacity = 0;
for (double i = -1; i <= 1; i+= 0.005)
{
this.Opacity = System.Math.Abs(i);
Application.DoEvents();
System.Threading.Thread.Sleep(5);
}
}


VB.NET:


Private Sub frmOpacity_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.Opacity = 0
Dim i As Double
For i = -1 To 1 Step i+= 0.005
Me.Opacity = System.Math.Abs(i)
Application.DoEvents()
System.Threading.Thread.Sleep(5)
Next
End Sub





Figure-3: Form being gradually getting opaque.


  1. Scrollable Form: If you have many controls on a Form, and the contents are getting out of view, all you have to do is set the AutoScroll property of the Form to True and you will get a scrollbar you can use to scroll through your contents:


Figure-4: Form with AutoScroll property set to True.


  1. Form Ownership: If, for any reason, you want keep one Form always into the focus, (for example: the Solution Explorer in the Visual Studio Form) then you have two options. One way is to set the TopMost property of the Form to true. The other is to set one Form to own the others. You can do this by either in the child Form by setting its Owner property, or the parent Form by calling the AddOwnedForm() method.


  1. MDI Forms: Using MDI Forms is another way of handling Form ownership. The advantage of an MDI interface is that multiple forms will be clubbed inside a single parent. This comes quite handy in several types of applications.


Here is the entire source-code in C# for all the concepts just discussed.

5 comments:

  1. How to download vb.net?
    Where is the link?

    ReplyDelete
  2. Here is the link for visual studio downloads:

    http://www.microsoft.com/visualstudio/eng/downloads

    ReplyDelete
  3. Hi Prahlad,

    Cool blog ,nice article in very detail .Thanks for sharing the link.





    Dotnetnuke cms








    ReplyDelete
  4. thank u for sharing such good and valuable information on DotNet...a good training center for DOTnet Training.

    online training for Dot Net

    ReplyDelete



  5. Hi there, awesome site. I thought the topics you posted on were very interesting. I tried to add your RSS to my feed reader and it a few. take a look at it, hopefully

    I can add you and follow.


    Dot Net Training in Chennai

    ReplyDelete