15

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

svick
  • 236,525
  • 50
  • 385
  • 514
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123
  • 9
    `if (true == condition)` should be `if (condition)`, nothing to do with your question, just a little point :) – Hans Z Aug 07 '12 at 22:25
  • 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 file – Rafik Bari Aug 07 '12 at 22:28
  • I need to know how can i display a form using Code, because i have only one file that i compile which is source.cs – Rafik Bari Aug 07 '12 at 22:29

4 Answers4

40

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.

svick
  • 236,525
  • 50
  • 385
  • 514
Dan
  • 9,717
  • 4
  • 47
  • 65
  • How to do the same thing in NET 2.0; sorry i forget to mention .net2.0 in tags – Rafik Bari Aug 07 '12 at 22:43
  • 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. – Dan Aug 07 '12 at 22:45
  • `error CS0234: The type or namespace name 'Forms' does not exist in the na mespace 'System.Windows' ` – john k Mar 16 '23 at 03:11
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);
Hans Z
  • 4,664
  • 2
  • 27
  • 50
  • 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? – Rafik Bari Aug 07 '12 at 22:53
  • 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. – Hans Z Aug 07 '12 at 23:00
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.
GarethJ
  • 6,496
  • 32
  • 42
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
1

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

Prasanth
  • 5,230
  • 2
  • 29
  • 61