How do I programmatically create a windows form?
Asked Answered
B

4

16

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

Bushnell answered 7/8, 2012 at 22:22 Comment(3)
if (true == condition) should be if (condition), nothing to do with your question, just a little point :)Hid
I want to make an option on the Builder which i use to compile the file, whether to display the form or not. If it is set to display the form, the dialog will show up upon file execution. or Else, nothing will appear after clicking on fileBushnell
I need to know how can i display a form using Code, because i have only one file that i compile which is source.csBushnell
C
42

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.

Costmary answered 7/8, 2012 at 22:27 Comment(3)
How to do the same thing in NET 2.0; sorry i forget to mention .net2.0 in tagsBushnell
This should work in .Net 2 using the C# 3 compiler. If you don't have a newer compiler, you can't use the var keyword. I'll edit the answer to reflect this.Costmary
error CS0234: The type or namespace name 'Forms' does not exist in the na mespace 'System.Windows' Delora
H
5

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);
Hid answered 7/8, 2012 at 22:28 Comment(2)
That exactly what i need todo. But how can i create the class using designer, Is copying the content of Form1.Designer.cs to source.cs and using the code above in the main method is sufficient?Bushnell
you can Add New Item to your project, and you can add a Windows Form. It generates the files you need automatically for you. You'd probably want to do the form.Show code inside of your main form though, the application will exit if all it does is show a new form.Hid
D
4
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.
Depressed answered 7/8, 2012 at 22:27 Comment(0)
S
1

Form is a class which you can instantiate like any other, set it's properties, call it's methods.

Selfish answered 7/8, 2012 at 22:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.