Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically create a windows form?

I have a unique c# source file named source.cs that i compile using CSharpCodeProvider from a builder to get an executable.

I would put an option on the builder whether to display the About form on application startup or not.

How can i create a form with title as About Us then add controls within (Labels, RichTextEdit etc..)

Something like

if (display_about_dialog) {
// code to display the form }

Any help would be highly appreciated

like image 225
Rafik Bari Avatar asked Aug 07 '12 22:08

Rafik Bari


3 Answers

Try something like this:

using (Form form = new Form())
{
    form.Text = "About Us";

    // form.Controls.Add(...);

    form.ShowDialog();
}

Here's the documentation page for the System.Windows.Forms.Form class.

like image 97
Dan Avatar answered Nov 20 '22 11:11

Dan


if you have a class MyForm : System.Windows.Forms.Form (that you create using windows form builder)

You can do

MyForm form = new MyForm();
form.Show();

To launch an instance of MyForm.


Though if you want to create a simple confirmation or message dialog, check out the many uses of MessageBox

MessageBox.Show("text");
MessageBox.Show("text", "title", MessageBoxButtons.OKCancel);
like image 5
Hans Z Avatar answered Nov 20 '22 12:11

Hans Z


Form aForm = new Form();

aForm.Text = @"About Us";
aForm.Controls.Add(new Label() {Text = "Version 5.0"});
aForm.ShowDialog();  // Or just use Show(); if you don't want it to be modal.
like image 4
Sam Axe Avatar answered Nov 20 '22 11:11

Sam Axe