|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
A Windows Form as a Dialog BoxThis article was prompted by my desire to offer a Windows form in the manner of a File Dialog Box but populated with my own buttons and textboxes. I could display the "dialog" form and make it disappear, no problem. My problem was passing the results of the dialog operation back to the main program and passing configuration information to the dialog box prior to display. This article presents the solution to my dilemma. Two forms are presented. The first is created by How to effectively Pass information from one Form to anotherIn each Form's class template, I thoughtfully provided private storage for the object that's to be passed: private DateTime form1_Time; // internal container to store DateTime object
public DateTime From_Form1_object_form1_GetTime()
{ // get DateTime object and send it back
return form1_Time;
}
public void In_Form1_object_form1_Store_Time(DateTime timeType)
{ // take the incoming DateTime object
form1_Time = timeType;// and then store it internally
}
private Form2 form2;
// variable to hold object of Type Form2 created when Form1 is loaded
Notice above that I have a private field variable that is a private void Form1_Load(object sender, System.EventArgs e) { form2 = new Form2(); form2.BringToFront(); // if running this demo in debugger, form is form2.Show(); //minimized at first so force form2 to open } This is just about all we need. The newly created private void button1_Click_Form1_SendTime(object sender, System.EventArgs e)
{ // send time to form2 object
DateTime buttonClickTime = DateTime.Now;
// run method located in object form2
// named "In_Form2_object_form2_Store_Time"
form2.In_Form2_object_form2_Store_Time(buttonClickTime);
}
Now, consider that the user has completed filling out the dialog box and clicks the "accept" button (in this analogy, private void button2_Click_Form1_GetTime(object sender, System.EventArgs e)
{
// run method named "From_Form2_object_form2_GetTime()"
// located in object form2
DateTime timeStoredIn_form2 = form2.From_Form2_object_form2_GetTime();
textBox1.Text = String.Format( "{0:T}",timeStoredIn_form2);
// format and display
}
A click on the Get Time button on So we've successfully passed access back and forth to a private object by invoking public methods! Finally, notice that when you first start the program, the Get Time button returns 12:00:00 showing that the private object is indeed empty ( Getting a Dialog Result from Form2I didn't discover this until later but using a form as a dialog box is apparently a common technique. In fact, you can get a public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
I have no idea why something so incredibly easy now was so difficult then. I literally spend hours figuring out how to pass data between two .NET Forms. Good thing I'm retired and patient! Back in the old days of K + R C (pre C++ and pre C#), you could just put a fence around a heap of memory and then drive your bits and bytes in, out and around at will. The main program and dialog box both had access to the memory. One wrote and the other received. Incredibly convenient but then along came the virus writers. Anyway, I hope my explanation was informative and helpful. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||