Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a visual studio C# GUI application, where I want to initalize some settings, before loading the gui.
At the moment I have a gui with a button, where I "load" the settings. But it does not work, since some of the settings should be done before the gui loads:
public static void initializeSettings(bool bShow) {
    //First, the log: 
    log__ = Logger.Current;

    //And settings, read from  config file (placed in bin folder):
    settings__ =  MySettings.Current; 
    
    //Set globalmember preprocessing parameters:
    runNumber_ = settings__.getInt("run.Number", 3); 
 
    iDelayMillisecs = settings__.getInt("DelayMilliSecs", 10);
    myString = settings__.getString("myString", "defaultString");
    myBool = settings__.getBool("myBool", true);
    
    //Set folder:
    strLogFolder_ = settings__.getS("FolderInput", "C:/logfiles");
 }

So I guess there must be a place to place code to load before the Main window opens?

What I have tried:

<pre>Settings are defined in a App.config file like this example:


<configuration>
  <startup>
    <supportedruntime version="v4.0" sku=".NETFramework,Version=v4.8">
  
  <appsettings>
	  <add key="Language" value="CSharp">
	  <add key="Copyright" value="Copyright © blabla">
	  <add key="SmtpClientName" value="blablabla">
	  <add key="PortNo" value="25">
	  <add key="userName" value="blabla">
	  <add key="password" value="blabla">
	  <add key="LastNumOfcharsFromLogfileToErrorMail" value="1500">  
	  etc....
  
At the current point the program is working fine, but with no gui.
Anyone for a quick help? I'm sure it's possible, but I cant find any "initialization" or something like in visual basic
Posted

1 solution

Yes, there is. Really settings are outside of presentation, so I'd create a settings class and keep them in there.

internal static class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();
        Application.Run(new Form1());
    }
}


I'd do it before the Application.Run(). If in the Form, how about the Form_Load event rather than a button? This gets called after the form is created but before it is displayed.

BTW - you've got some strange variable naming convention going on there..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900