user3956861
user3956861

Reputation:

Can you change the name of a C# Winforms button via the App.config file?

I have a Winforms app with buttons that when clicked go to various internal and external locations (Network locations/Web paths) I was asked if it was possible for the end users to have their own names for the buttons, instead of what I called them.

I've created an App.config file so if the links change they can at least edit that file reflecting the change in path/URL etc

The button properties text field doesn't appear to allow code behind it.

private void GoogleS_Click(object sender, EventArgs e)
        {
            try
            {
                string GoogleS = ConfigurationManager.AppSettings["Google"];
                Process.Start(GoogleS);
            }
            catch (Exception GoogleErr)
            {
                MessageBox.Show(GoogleErr.Message);
            }
        }

And the key inside the App.config file

<add key="Google"   value="http://www.google.com/" />

And the fix

App.config

<add key="GoogleButton"       value="Google Search"/>
<add key="GoogleLink"         value="http://www.google.com"/>

//Code for the Button names which are loaded on Form Loading from the App.config file
        private void CSS_Load(object sender, EventArgs e)
        {
            btnGoogleS.Text =   ConfigurationManager.AppSettings["GoogleButton"];
        }

private void btnGoogleS_Click(object sender, EventArgs e)
        {
            try
            {
                string GoogleS = ConfigurationManager.AppSettings["GoogleLink"];
                Process.Start(GoogleS);
            }
            catch (Exception GoogleErr)
            {
                MessageBox.Show(GoogleErr.Message);
            }
        }

        private void btnGoogleS_MouseHover(object sender, EventArgs e)
        {
            System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
            ToolTip1.SetToolTip(this.btnGoogleS, (ConfigurationManager.AppSettings["GoogleLink"]));
        }

Upvotes: 1

Views: 266

Answers (1)

Blorgbeard
Blorgbeard

Reputation: 103467

You can manually set the button captions:

btnGoogle.Text = ConfigurationManager.AppSettings["GoogleCaption"];

Probably you would do it in a Form Load handler.

Upvotes: 4

Related Questions