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


Comments

  • Download

    Posted by maninayy123 on 12/13/2005 04:25am

    I am not able to download the utility

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds