Advertisment

Work with Winforms

author-image
PCQ Bureau
New Update

A primary objective of the .NET platform is to bring uniformity in development across various languages targeting it. Unlike predecessors that had their own Win32 GUI-based development methodologies, .NET aims to combine the ease of RAD (Rapid Application Development) and the power of Win32 API/MFC based development to all development environments for it. For example, VB 6 allowed you to create a simple Forms application with minimal coding, while to do the same in VC++, requires you to know the relevant Win32/MFC development techniques Thus, now if you have to create a VB.NET application, against a VC#-based application, the time required to do so is almost same for both. And not only that, the way to do so is also uniform, courtesy the BCL (Base Class Libraries) that bring consistent and uniform development.

Advertisment

A typical WinForms development

screen in VC#. The look and feel remains the same across language in VS.NET IDE

In this first WinForm (that’s what GUI development is called under .NET), we shall be focusing on some of the core WinForms-based development using the control introduced in the same. We’ll continue the same theme next month also, but will skip basic controls like Textbox and Labels, since their usage is quite evident. 

WinForms



This is a typical WinForms development screen in VC#, similar to that of VB/VB.NET. In fact, this look and feel will remain almost the same across all languages in VS.NET IDE. 

Advertisment

We begin with the LinkLabel control, which is the blue A in the toolbox to the left. The purpose of this control is to be display a hyperlink on the form such that when mouse hovers over it, it displays the hand icon, giving a feel that is quite similar to the links on a Web page. When the control is clicked, the LinkClicked event is raised which can be consumed to do the needful, as shown below: 

private void linkLabel1_Link Clicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEvent Args e)



{


Process.Start (“http://localhost/wintoolzone”);


}

This code simply invokes the browser to navigate to the specified URL. The output of this execution is captured (see screenshot on next page).

Advertisment

ContextMenu is another new control in .NET that, as the name suggests, allows you to specify the context menu for any given control. Thus, you can have custom context menus for different controls. Here’s an example of how to use it:

  • Start by having a textbox on the form and name it txtName
  • Double click the ContextMenu control on the ToolBox and it will be shown in a bar just below the Form. Select it and you will find a familiar menu designer being shown just below the title bar of the form, as used to be there in VB6. Enter your context menu options there.
  • Finally, we associate the context menu with our textbox, by selecting the ContextMenu property of the textbox (which is a dropdown listing all the ContextMenu controls on our form) and selecting the relevant context menu for it.

Output of the usage

of LinkLabel control
Advertisment

Now, when the textbox is right-clicked, the configured context menu pops up. Thus, the ContextMenu control makes it easier to let controls have their own customized context menus, unlike the VS.NET predecessors.

Another very neat control that has been introduced with WinForms is the NotifyIcon control. Remember the number of lines and structures we had to deal with, and invoke the relevant Shell APIs to get our applications icon in the system tray? Well, this new control removes all those hassles and now it’s just a single line of code that gets your application’s icon to the system tray. Let’s see how:

  • Start off by having a NotifyIcon control on the form by double clicking it in the toolbox. 
  • l Select the control and set its icon property to the icon you wish to display in the tray. Optionally, you may also set the ContextMenu property of the control so that right-clicking the icon in the tray would get the relevant menu options popped up.
Advertisment

Also, set its Visible property set to false. 

That’s all there is to configure! Now, let’s get the application icon in the tray. Draw a button on the form, on whose click we will have the application go into the tray. Here’s the code that goes to have the icon in the tray, which we write in the Click event of the button:

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



{


this.Hide();


this.nfIcon.Visible = true;


}


Advertisment

Since we have to make the application go into the system tray, we hide the form so that it goes out of visibility and then set the Visible property of the NotifyIcon control to true so that the icon becomes visible in the system tray, and you are done! No more calls to the shell, or dealing with complex structures to set the icon in the tray. Here’s how it looks like before going to 



the tray:

When the Goto Tray button is clicked, the form is hidden and the icon appears in the system tray. 

The application icon is the gray upward pointing array in the tray.

Advertisment

Now, let’s have a look at some of the modifications in WinForm controls. One noticeable change is that the Common Dialog Control has now been split into its respective components, and thus, we now have separate controls to select files and color.

Let’s exemplify this change by using the OpenFileDialog component. Double click the control in the toolbox to bring it on the form. Draw another button, on whose click, we show the file selection dialog and select a file, as shown in the code below:

private void button2_Click(object sender, System.EventArgs e)



{


// set the properties


this.openFileDialog1.Title = “PCQ Select File Dialog Example Title”;


this.openFileDialog1.Filter = “Text Files (*.txt)|*.txt”;


if (this.openFileDialog1.ShowDialog() == DialogResult.OK)


{


// display the name of the selected file


MessageBox.Show(this.openFileDialog1.FileName); 


}


}








Likewise, we can use the SaveFileDialog, ColorDialog, FontDialog etc to do relevant tasks.

So, now, we have discussed some new controls that help us be more productive and also took a peek in to some of the changes that have been made to existing controls. Next month, we conclude this WinForms introduction by delving into implementation of some other controls, new to .NET, and also see changes in some of the existing ones.

Kumar Gaurav Khanna, a Microsoft .NET MVP runs wintoolzone.com

Advertisment