0

New to windows forms so please bear with me.

I have a simple application that displays a few buttons, and I'd like the attributes of these buttons to be set by the user using some kind of external config file e.g. config.ini.

In the config file, the attributes would be set out something like this:

[Button 1]          //button identifier
isEnabled = True    //show/hide the button
colour = "#FFFF"    //button background colour
caption = "Button"  //text to display on button
action = "Action"   //action when button is clicked

The config file should be stored with the program executable, and when the program is launched, it will display the buttons and text defined in this file.

The action determines what happens when the button is clicked. This could either be opening a new page, or writing a string to a variable. The aim of the program is to allow users to navigate through a couple of menus, and then select a button which will print out the string.

Edit: if the action is to go to a new page, it will just say 'Next Page' so I guess I can just use an if/else for this. I.E.

if(string.Equals("Next Page"){
    //go to next page
} else{
    //save string to variable
}

I know how to set button attributes in the code, but I don't understand how I can read the config file. Its important that these attributes can be set from the external file so that the program can be easily configured without needing to rebuild it.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
SamONeill
  • 11
  • 4
  • 1
    what you are lookig for is the app.config, the standard way for .net program to store config settings. Have a look at this question https://stackoverflow.com/questions/13043530/what-is-app-config-in-c-net-how-to-use-it – Gian Paolo Jan 12 '23 at 21:12
  • If you really need to use an INI format then look at this question where the answers list a lot of NuGet packages to reaad/write INI files: https://stackoverflow.com/questions/217902/reading-writing-an-ini-file – Steve Jan 12 '23 at 21:14
  • @GianPaolo this looks perfect, thanks. Now I'm just wondering how I get my program to load the settings on launch? I've been playing around with changing the settings within a button handler, but I need them to be active when the program loads. I thought I could do this in `InitializeComponent()` but visual studio is warning me not to edit that. – SamONeill Jan 12 '23 at 22:25
  • Just add it after `InitializeComponent() ` within the constructor. It would be better if you do it in a function and call the function though, like `LoadConfig()` –  Jan 15 '23 at 21:10
  • Use application settings: [Manage application settings (.NET)](https://learn.microsoft.com/en-us/visualstudio/ide/managing-application-settings-dotnet?view=vs-2022&WT.mc_id=DT-MVP-5003235) It also supports[data-binding to control properties](https://stackoverflow.com/a/32487895/3110834). – Reza Aghaei Jan 28 '23 at 13:53

0 Answers0