What is the entering method of a multi form project? - c#

I'm working with a C# project using System.Windows.Form to create the GUI, I have two forms within the VS project( MainForm and InitialPrompt). I've never used Forms before and Google hasn't been of much help.
Intended action:
InitialPrompt Load
Click Button on InitialPrompt
Load MainForm
However, since MainForm was created first there is some property/method that allows it to load first and the InitialPrompt does not load at all. How to I make MainForm the secondary form and InitialPrompt the primary?
Thanks in advance.

namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
You can change the above code to read
Application.Run(new Form2()); // or whatever the name of the second form is.
This is found in your Program.cs file.

Look for the Program.cs file inside your project. Inside you will see something like this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
Just change new MainForm() to new InitialPrompt(). This will make InitialPrompt the main form.

Like any other .Net project it is the static void Main() method in a class defined in your project. Because of this, only one static void Main() method is allowed in a project.
NOTE: this static Main method must be defined as void return type, and it can either take no arguments, or it can be defined to take an array of strings to be passed as command line arguments.

Related

Visual studio does not show UI while running app

I have been working on c# for about 3 hours and Visual Studio does not show the UI while running the application. It shows the UI in designer but after compiling and running, it just goes blank.
Two things to test:
In the code file Program.cs, you will find something like this:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
Does the name of the form in the line Application.Run match the name of your form?
Your form's code behind (Form1.cs) should have a constructor looking like this
public Form1() // Where the name of the constructor must match the one of the form class.
{
InitializeComponent();
// Your code goes here (if any) ...
}
Does it have this constructor? If yes, does it call InitializeComponent?
InitializeComponent is very important, because it creates the controls and configures the form. You may have replaced it with your own code. Always call it before your initialization code.
I guess the entry point if your application is wrong. Check the solution properties (right click on your application/solution in the solution explorer -> properties ) for the correct entry point.
It looks like you have more than one form.
Go to the Program.cs file and verify if the class of the form is in Application.Run() method
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main ()
{
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault (false);
Application.Run (new urltetx());
}
}

Call a usercontrol’s method from a form c#?

I have the method listviewupdate() in usercontrol schuler.
Usercontrol schuler is in form1.
Then I have form2. When I click a button in form2 I want to call the method listviewupdate().
I tried creating a second method in form1 which calls the listviewupdate() method, and then calling this second method in form 2 but I get an error.
Can somebody please help me?
In your file Program.cs, you can define a globally accessible variable:
static class Program
{
// for external access to Form1 methods
public static Form1 MainForm;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new Form1(args);
Application.Run(MainForm);
}
}
The static variable MainForm can then be used to access any public method to Form1 via Program.MainForm.MyMethod().
Assuming, you have access to the Form2 object from within Form1, you can use the Form2 object variable to call Form2 methods out of Form1 methods.
Be aware that you might run into problems when (unknowingly ...) using more than one thread. Read about BeginInvoke.
#Axel Kemper thanks for the response. I did how you told me with Program.MainForm.MyMethod() but it didn't find MyMethod().
Then I went in the form1.Designer and there I saw that the usercontrol was set to private, also I switched it to public and now it works with Program.MainForm.schuler1.MyMethod() //schuler1 is the name of my user control.
I just don't understand why it doesn't work with
Form1 form1 = Application.OpenForms[1] as Form1;
form1.schuler1.ListviewUpdate(); //schuler1 is the name of my usercontrol
even if the user control is set to public I get the error "System.NullReferenceException" in main.schuler1.ListviewUpdate();

How to exit a Windows Forms/C# program

Class A has a Form1 (subclass of System.Windows.Forms.Form) member.
class A {
Form1 form;
public A()
{
form = new Form1();
form.Show();
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
A a = new A();
Application.Run();
}
The problem is I do not know how to exit the program. I have tried Application.Exit() when handling the Form.Closed event or call A.Dispose(), but the Windows Task Manager still lists the process of my program.
How do I finish this program?
Application.Run has 3 overloads. You are using this one with no arguments.
Windows runs your program in a message loop, but it doesn't care about your form.
So if you close your form it doesn't matter; the program will still run.
The second overload is what everyone uses, Application.Run(Form). This one runs a Windows message loop over your form, so when you click close on the window, the application closes.
Your code should be:
class A {
Form1 form;
public A()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
form = new Form1();
form.Show();
Application.Run(form);
}
}
[STAThread]
static void Main()
{
A a = new A();
}
Following Microsoft you should use this:
Application.Run(a.Form);
Because MSDN states that
Most Windows Forms developers will not need to use this version of the method. You should use the Run(Form) overload to start an application with a main form, so that the application terminates when the main form is closed.
I think you have a mixup there. Check the documentation for Application.Exit.
There you will see that Exit will raise the Closed event for you, and calling Exit there might cause an infinite loop (which might be causing your problem, that the application is still visible).
Try this:
Environment.Exit(1);

c# displaying form in class library winforms

I've been given some code that is in effect a class library with a Winform module. Obviously I cannot run the class library directly.
Is there anyway that I can 'run' it so that I can see what the form will look like when run? I need to check anchoring/docking, etc?
Thanks
Make a new WinForms project, reference the Module, create an instance of the form and show it:
using WfModule;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new WfModuleForm());
}
}
}
Assuming your DLL is called "WfModule.Dll" and the form is called "WfModuleForm".
You should be able to create a new winforms project (an EXE, not a DLL), add the DLL library as a reference, and then instantiate and show an instance of the form defined in the DLL library.

How do I get arguments in a form application?

I can find many examples on how to get arguments in a console application, but I can't seem to find an example of how to get arguments in a windows form application.
I would like to following things.
whenever I open a jpg file, windows launches my application.
I would like to know path and name of the jpg file from my application.
How do i do that?
Environment.GetCommandLineArgs
Open up program.cs, on a file > new > winform project, you'll get
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
change this to
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Now its just like the console apps, you'd access them via args.
Even if you don't go with this option, you should be aware of how the win form app is initialized :) This way, you could run different forms or not run a form at all.

Categories

Resources