VncSharp Logo

VncSharp: Quick Start Guide

The following is a guide to getting started using the VncSharp RemoteDesktop control. Instructions are provided for Visual Studio.NET users as well as for those using Command-Line compilers. You are encouraged to view the API documentation and source code for more detailed information.

UPDATE: I have also added an example client app in C# to demonstrate how to use the VncSharp.dll file in your project. You can download it here.

1a) Setup for Use with Visual Studio.NET

1b) Setup for Use with Command-Line Compilers (csc, mcs, etc.)

  1. You must add a run-time reference to the VncSharp.dll file by including the following switch in your compiler options: /res:\path\to\VncSharp.dll
  2. Include the VncSharp Namespace at the top of your source file by adding the code: using VncSharp;
  3. In a Windows Forms derived class, add a member variable of type VncSharp.RemoteDesktop and instantiate this object in the form's constructor.

2) Coding the RemoteDesktop Control

You must add code to tell the RemoteDesktop to connect to a VNC host. In your form's Constructor (or where ever you want the connection to occur), add the following code:

// assuming your RemoteDesktop control is named rd
rd.ConnectComplete += 
	new ConnectCompleteHandler(ConnectComplete);
rd.ConnectionLost += 
	new EventHandler(ConnectionLost);
rd.Connect("host_name_or_ip");
			

The first two lines add Event Handlers for the two events raised by the RemoteDesktop control. The ConnectComplete event occurs when a connection has been made. Similarly, the ConnectionLost event indicates that the host has dropped the connection, or that the connection has otherwise been lost. Both events are useful for adjusting your form's UI, for example:

protected void ConnectComplete(object sender,
			       ConnectEventArgs e)
{
   // Update Form to match geometry of remote desktop
   ClientSize = new Size(e.DesktopWidth,
                         e.DesktopHeight);

   // Change the Form's title to match desktop name
   Text = e.DesktopName;
}
		
protected void ConnectionLost(object sender,
                              EventArgs e)
{
   // Let the user know of lost connection
   MessageBox.Show("Lost Connection to Host!");
}
			

You connect to a remote VNC server like this:

   rd.Connect("host_name_or_ip");
   // Optionally you can specify a display number too:
   // rd.Connect("unix_host_listening_on_display_2", 2);

All GUI and Network updates are handled automatically by the RemoteDesktop control. However, you will sometimes need to interact with the server manually. For example, if your client screen gets corrupted or needs a complete refresh (i.e., most updates from the server are only partial to save bandwidth), you can ask for a Full Screen Update like this:

   // NOTE: available in VncSharp v. 0.85
   rd.FullScreenUpdate();

You might also want to send various key combos, for example, CTRL+ALT+DEL:

   // NOTE: available in VncSharp v. 0.85
   rd.SendSpecialKeys(SpecialKeys.CtrlAltDel);

When your user closes the form, or clicks a Disconnect menu option, you can close the connection:

protected override void OnClosing(CancelEventArgs e)
{
   rd.Disconnect();
   base.OnClosing (e);
}