Launching Windows Forms Applications from within an existing Windows Forms Application

My most recent project has been to develop a Windows Forms Application that connections to Microsoft Dynamics NAV system. I have been working closely with a NAV specialist who has been exposing some web services I can then use to develop the application.

The application has to be able to perform a variety of tasks and operations so I have separated these out into different tabs to make the interface more intuitive and less cluttered. My problem came when I needed to get a users to select an item from a drop down list when they clicked on a submit button. Normally I could add a panel and then hide this until the user pressed the button but given the amount of information on some forms I decided this would not be a good idea.

My next thought was it would be nice if I could re-use the MessageBox functionality and add a drop down list on this but after some research online I soon learned that this was not possible. I did find some posts about creating a class based on the MessageBox functionality and extending this but that seemed like a waste of time and effort.

I then spent some time researching other options and was surprised to learn how easy it is to call another Windows Forms Application in an already running Windows Forms Application and get some data from it, see example code below.

Launch the new Form
  1. string orderPadNo = String.Empty;
  2.             OrderPad orderPadForm = new OrderPad(customerNumber);
  3.             try
  4.             {
  5.                 // Show select order pads Form as a modal dialog and determine if DialogResult = OK.
  6.                 if (orderPadForm.ShowDialog(this) == DialogResult.OK)
  7.                 {
  8.                     // Read the contents of select order pads combobox
  9.                     ComboBox cbxOrderPads = (ComboBox)orderPadForm.Controls["CbxOrderPad_OrderPads"];
  10.                     if (cbxOrderPads != null)
  11.                     {
  12.                         orderPadNo = cbxOrderPads.SelectedValue as string;
  13.                     }
  14.                 }
  15.             }
  16.             finally
  17.             {
  18.                 if (orderPadForm != null)
  19.                 {
  20.                     orderPadForm.Dispose();
  21.                 }
  22.             }

In my new Windows Forms Application I then have the required functionality/display and two buttons. One button confirms the users selection and the other is for cancelling out of the interface. I have included the code below to return out of the new Windows Forms Application.

Code Snippet
  1. private void BtSelectOrderPad_Cancel_Click(object sender, EventArgs e)
  2.         {
  3.             this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  4.         }
  6.         private void BtSelectOrderPad_Ok_Click(object sender, EventArgs e)
  7.         {
  8.             if (CbxOrderPad_OrderPads.SelectedValue == null || String.IsNullOrEmpty(CbxOrderPad_OrderPads.SelectedValue as string))
  9.             {
  10.                 MessageBox.Show("Please select an order pad");
  11.                 return;
  12.             }
  14.             this.DialogResult = System.Windows.Forms.DialogResult.OK;
  15.         }


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: