Arokh Posted May 22, 2008 Posted May 22, 2008 Hi I redesigned my application a bit to be more flexable. Since sometimes I need a different UI to start up or no UI at all. So I thought I choose a normal class as startup and initialize the needed UI from there. To do this I had to change the application type to "Console Application". This worked but everything looks Windows 98 style. No I'm worring that even if I fix this that maybe there are some other issues changing to "Console Application" which I might only notice later. So I'm starting to wonder, is there a way to select "Windows Froms Application" and select a non-form class? (I'm using Visual Studio 08, VB.NET) Quote
Nate Bross Posted May 22, 2008 Posted May 22, 2008 All you do is chagne your Program.Main() method In here you can setup different forms create the logic to display the correct one, or display none at all. static class Program { public static Form1 f1; /// <summary> /// The main entry point for the application. /// </summary> [sTAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); f1 = new Form1(); f1.Hide(); Application.Run(); f1.Close(); } } Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Arokh Posted May 23, 2008 Author Posted May 23, 2008 OK, that worked. But there are still some issues: I don't know how to hide the command prompt and hide it from the taskbar. Quote
Nate Bross Posted May 23, 2008 Posted May 23, 2008 Sorry I wasn't clear, you use that code above, in a Windows Forms Application, this way f1 = new Form1(); f1.Hide(); Will hide the window and there will not be a console window on the screen. If you need the same functionallity, you could add a static method somewhere and call that in Program.Main(); HTH Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Arokh Posted May 23, 2008 Author Posted May 23, 2008 I though of trying to make a hidden dummy form. But I hoped for a more elegant solution, since the form is just hidden not closed. And closing all forms would close the program. I know it is not that big of a deal, having a hidden form, but there should be a better way. I can't imagine having to choose between: Bound to have at least one form open/hidden and the startup form is always initialized or Be able to choose between GUIs without being bound to have one open/hidden but having a command prompt in the background. Quote
Nate Bross Posted May 23, 2008 Posted May 23, 2008 The only difference between your two options, is that one uses a console window that you cannot hide, the other uses a Windows Form, which you can hide. Either way you need a main entry point in your program. If your program has no need for a UI, you can skip all initalizing in Program.Main() and just run the job you need from Program.Main() without ever loading a form. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
MrPaul Posted May 27, 2008 Posted May 27, 2008 Windows Application without a Form Note that even if the project is set to Windows Application, you can still use the Main method entry point and do not actually need to instantiate a Form if you do not need one. Quote Never trouble another for what you can do for yourself.
Arokh Posted May 28, 2008 Author Posted May 28, 2008 Thanks MrPaul, Nate Bross. That is exactly what I'm looking for. But I still have one problem: I have created a new class (StartUp), not a form. I declared it so that it would inherit the Form class, which enables me to select it as the startup form for my project. It works fine but I have to use the Load Event to do things. I tried using the main method but I can't seem to get it to work. I tried: Public Shared Sub main Public Shared Sub main(args() as String) Public Sub main And some other combinations but they aren't called. Quote
Nate Bross Posted May 28, 2008 Posted May 28, 2008 Check out this site for information on Sub Main() method Move Sub Main() to a Class Next the article will explain how to move the Sub Main() procedure to a class. Add a new class named AppMgr to the VB.NET project to your project. Change the accessibility keyword ‘Public’ to ‘Friend’ in the first line of the class statement. This means the AppMgr class can be accessed from anywhere in the application but never from outside the application. Cut the Sub Main() code from Form1 and paste it into the body of the new AppMgr class statement. The AppMgr class code should now read: Friend Class AppMgr <STAThread()> _ Shared Sub Main() ' Declare a variable named frm1 of type Form1. Dim frm1 As Form1 ' Instantiate (create) a new Form1 object and assign ' it to variable frm1. frm1 = New Form1() ' Call the Application class' Run method ' passing it the Form1 object created above. Application.Run(frm1) End Sub End Class The next step is to make the AppMgr class the startup object for the project. 1. Right click the Project in the Visual Studio.NET IDE’s Project Explorer pane and choose Properties from the menu that pops up. 2. In the TreeView on the left side of the dialog that opens click Common Properties then General. 3. In the dialog look for the ComboBox under the label Startup Object: . Use the ComboBox to select AppMgr as the startup object for the project. Possibly this portion below may be the information you are missing: "Change the accessibility keyword ‘Public’ to ‘Friend’ in the first line of the class statement. This means the AppMgr class can be accessed from anywhere in the application but never from outside the application." Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Arokh Posted May 28, 2008 Author Posted May 28, 2008 The site you linked is nice and logical but for some reason it doesn't work. In the first example given by the site the Main method is ignored. (I used breakpoints & commented Application.run(...) out but the form shows) For the second example, I just can't select the new created class (with the entry point) as the startup object. To select it I would have to set the program as an Console Application, where the command prompt would show up. Quote
Administrators PlausiblyDamp Posted May 31, 2008 Administrators Posted May 31, 2008 If the method is called Main and is also public and shared then it should work. If you are using VS 2005 or higher you will need to disable the application framework first though - it is on the same property page as the one for selecting the start up form / object. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.