Dialog Resource to C# Form Converter

G’day everyone.

I’ve written this DevStudio Add-In for two reasons – the main one is that I started learning C# by doing a conversion of something I’d written in C++ in Visual Studio, so I wanted to easily convert all the dialog boxes to C# forms. The second reason was that I had assumed that I needed to because I hadn’t found the WinDes forms editor.

So the other night I wrote this Add-In which will take a dialog that is open in the dialog editor, and convert it to C# source. It should keep me going until the beta of Visual Studio.net comes out. I haven’t used the WinDes editor much yet, but I’ll be pretty disappointed if it can’t use rulers and guidelines like in the dialog editor.

The aim was to be simple and quick. It supports Labels, Edit Controls, Check Boxes, Radio Buttons, List Boxes and Combo Boxes. Just plonk them on the dialog, and hit the button.

Adding support for other controls should be very easy. Just add a method like DialogConverter::ConvertLabel(), and call it from DialogConverter::ConvertDialog().

If you want it to support setting all the various flags on the controls you can easily make it do that by adding lines like this example from the DialogConverter::ConvertListBox() method…

if (strFlags.Find("LBS_NOINTEGRALHEIGHT")!=-1)
{
 strConstructor += "        "
                + strVariable
                + ".IntegralHeight=false;\r\n";
}

The way Ive converted DLUs to pixels is good enough for my purposes, but feel free to make it do it properly. (Note: make the labels a bit longer than they need to be to fit their text).

Other things to note…

  • The controls are converted and their tab order set in the tab order of the dialog so dont forget to set it.
  • Their resource IDs defined names are used to create the names of the variables in the form class.
  • You can use rulers, and guidelines.

Here is an example

In the dialog editor…

The generated code…

//
// Generated by the NetForm Visual Studio Add-In.
// Nev Delap, October 2000. Send feedback to Nev@Delap.com
//

using System;
using System.Drawing;
using System.WinForms;

public class ContactsEntryForm : Form
{
 private Label nameLabel;
 private TextBox nameTextBox;
 private Label addressLabel;
 private TextBox addressTextBox;
 private Label occupationLabel;
 private ComboBox occupationComboBox;
 private Label sexLabel;
 private RadioButton maleRadioButton;
 private RadioButton femaleRadioButton;
 private Label sexyLabel;
 private CheckBox yesCheckBox;
 private Label hobbiesLabel;
 private ListBox hobbiesListBox;
 private Button okButton;
 private Button cancelButton;
 private Label staticLabel;

 public static int Main(string[] Args)
 {
  Application.Run(new ContactsEntryForm());
  return 0;
 }

 public ContactsEntryForm()
 {
  Text         ="Contacts Entry";
  StartPosition=FormStartPosition.WindowsDefaultLocation;
  Width        =295+SystemInformation.FrameBorderSize.Width*2;
  Height       =376+SystemInformation.FrameBorderSize.Height*2;
  BorderStyle  =FormBorderStyle.FixedDialog;
  MaximizeBox  =false;
  MinimizeBox  =false;

  nameLabel         =new Label();
  nameLabel.Text    ="&Name:";
  nameLabel.Location=new Point(10,14);
  nameLabel.Width   =43;
  nameLabel.Height  =13;
  nameLabel.TabIndex=0;
  this.Controls.Add(nameLabel);

  nameTextBox         =new TextBox();
  nameTextBox.Location=new Point(90,11);
  nameTextBox.Width   =195;
  nameTextBox.Height  =23;
  nameTextBox.TabIndex=1;
  this.Controls.Add(nameTextBox);

  addressLabel         =new Label();
  addressLabel.Text    ="&Address:";
  addressLabel.Location=new Point(10,43);
  addressLabel.Width   =52;
  addressLabel.Height  =13;
  addressLabel.TabIndex=2;
  this.Controls.Add(addressLabel);

  addressTextBox         =new TextBox();
  addressTextBox.Location=new Point(90,40);
  addressTextBox.Width   =195;
  addressTextBox.Height  =23;
  addressTextBox.TabIndex=3;
  this.Controls.Add(addressTextBox);

  occupationLabel         =new Label();
  occupationLabel.Text    ="&Occupation:";
  occupationLabel.Location=new Point(10,72);
  occupationLabel.Width   =70;
  occupationLabel.Height  =13;
  occupationLabel.TabIndex=4;
  this.Controls.Add(occupationLabel);

  occupationComboBox         =new ComboBox();
  occupationComboBox.Location=new Point(90,68);
  occupationComboBox.Width   =195;
  occupationComboBox.Height  =50;
  occupationComboBox.TabIndex=5;
  this.Controls.Add(occupationComboBox);

  sexLabel         =new Label();
  sexLabel.Text    ="Sex:";
  sexLabel.Location=new Point(10,100);
  sexLabel.Width   =42;
  sexLabel.Height  =13;
  sexLabel.TabIndex=6;
  this.Controls.Add(sexLabel);

  maleRadioButton         =new RadioButton();
  maleRadioButton.Text    ="&Male";
  maleRadioButton.Location=new Point(90,100);
  maleRadioButton.Width   =60;
  maleRadioButton.Height  =16;
  maleRadioButton.TabIndex=7;
  this.Controls.Add(maleRadioButton);

  femaleRadioButton         =new RadioButton();
  femaleRadioButton.Text    ="Female";
  femaleRadioButton.Location=new Point(174,100);
  femaleRadioButton.Width   =72;
  femaleRadioButton.Height  =16;
  femaleRadioButton.TabIndex=8;
  this.Controls.Add(femaleRadioButton);

  sexyLabel         =new Label();
  sexyLabel.Text    ="Sexy?:";
  sexyLabel.Location=new Point(10,129);
  sexyLabel.Width   =52;
  sexyLabel.Height  =13;
  sexyLabel.TabIndex=9;
  this.Controls.Add(sexyLabel);

  yesCheckBox         =new CheckBox();
  yesCheckBox.Text    ="&Oh Yeah! Ooh La La!";
  yesCheckBox.Location=new Point(90,129);
  yesCheckBox.Width   =147;
  yesCheckBox.Height  =16;
  yesCheckBox.TabIndex=10;
  this.Controls.Add(yesCheckBox);

  hobbiesLabel         =new Label();
  hobbiesLabel.Text    ="&Hobbies:";
  hobbiesLabel.Location=new Point(10,158);
  hobbiesLabel.Width   =69;
  hobbiesLabel.Height  =13;
  hobbiesLabel.TabIndex=11;
  this.Controls.Add(hobbiesLabel);

  hobbiesListBox         =new ListBox();
  hobbiesListBox.Location=new Point(90,155);
  hobbiesListBox.Width   =195;
  hobbiesListBox.Height  =67;
  hobbiesListBox.TabIndex=12;
  hobbiesListBox.IntegralHeight=false;
  this.Controls.Add(hobbiesListBox);

  okButton         =new Button();
  okButton.Text    ="OK";
  okButton.Location=new Point(129,232);
  okButton.Width   =75;
  okButton.Height  =23;
  okButton.TabIndex=13;
  this.Controls.Add(okButton);
  AcceptButton=okButton;

  cancelButton         =new Button();
  cancelButton.Text    ="Cancel";
  cancelButton.Location=new Point(210,232);
  cancelButton.Width   =75;
  cancelButton.Height  =23;
  cancelButton.TabIndex=14;
  this.Controls.Add(cancelButton);

  staticLabel         =new Label();
  staticLabel.Text    ="These should be skipped without
                       "stopping the add-in...";
  staticLabel.Location=new Point(10,260);
  staticLabel.Width   =274;
  staticLabel.Height  =13;
  staticLabel.TabIndex=16;
  this.Controls.Add(staticLabel);
 }
}

The resulting .NET form…

The downloadable files contain the Add-In’s source, the dll (debug build), and an example .rc file, containing the dialog shown above, the generated C# code, and the compiled C# example.

Take it easy!

Version History

  • 10/24/00 – Bug fixes, and better functionality

Downloads

Download source – 65 KB
Download demo dialog resource and generated C# – 6 KB

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read