This is my first full sized c# project, so I apologize if the answer if the answer here was obvious.

I've got a program that runs a method called firstrun, which is called at the point that the form loads (if a value from a file is true).

This method reveals a settings panel, which gives access to various buttons ect.

Image:
Click Here

I want the user to enter values into both of the text boxes, before clicking OK. I was planning to do this by using a boolean array (initialised at the start of the firstrun method) with two values, each of which is set to true when the corresponding box is filled.

The thing is, when the 'browse' button is clicked, the button_click method is called, which then means that I can't set the boolean array value to true without using a global variable (I'm trying to minimise use of these) .

        public void FirstRunProc()
        {
            //This runs if first execution is true. Below is user message.
            MessageBox.Show("this is the first time you've run this program.\nand where you want your maps to be stored (optional).");
            MessageBox.Show("Please set the path of your server's .jar file,\nand where you want your maps to be stored (optional)");

            //making sure both boxes are checked
            bool[] fillallcheck = new bool[2];
            pnlSettingsMenu.Visible = true;

            btnCancelSettings.Enabled = false;
            btnRestoreDefaultsProc.Enabled = false;


        }



        private void btnBrowseForJar_Click(object sender, EventArgs e){
            ofdBrowseForGeneric.Filter = "jar files (*.jar)|*.jar|All files (*.*)|*.*";
            DialogResult browseForJarResult = ofdBrowseForGeneric.ShowDialog();

            if (browseForJarResult == DialogResult.OK){
                tbJarPathDisplay.Text = ofdBrowseForGeneric.FileName;

                GlobalVariables[2] = ofdBrowseForGeneric.FileName;
            }
        }

Any help is appreciated.

  • Alex.

One option is to keep the button's enabled property to false until both textboxes have data. One way to keep track of that, without extra global variables, is to use the Tag property for each textbox. You could use a common validating event handler for the 2 textboxes to check if the button should be enabled.

It could look something like this:

    public Form1()
    {
        InitializeComponent();
        textBox1.Validating += new System.ComponentModel.CancelEventHandler(textBox_Validating);
        textBox2.Validating += new System.ComponentModel.CancelEventHandler(textBox_Validating);
        textBox1.Tag = Boolean.FalseString;
        textBox2.Tag = Boolean.FalseString;
    }

    private void textBox_Validating(object sender, CancelEventArgs e)
    {
        TextBox CurrentTextbox = (TextBox)sender;
        if(CurrentTextbox.Text.Length > 0)
            CurrentTextbox.Tag = Boolean.TrueString;
        else
            CurrentTextbox.Tag = Boolean.FalseString;
        if(textBox1.Tag.ToString() == Boolean.TrueString && textBox2.Tag.ToString() == Boolean.TrueString)
            button1.Enabled = true;
        else
            button1.Enabled = false;
    }
commented: Nice plan. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.