How to re-use variable from a button click event? (ColorDialog)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lnfidel
    New Member
    • Oct 2021
    • 2

    How to re-use variable from a button click event? (ColorDialog)

    I am trying to create a program in windows forms that consist of 2 checkboxes for 2 textboxes to change the colors of their background and font color. Plus 3 buttons, forecolor, back color to store chosen color in, and an executable to check if any checkboxes are checked to use adequate colors to the selection. I have no problem with the logic for the executable button.
    Code:
    namespace Assignment1
    {
        public partial class frmTaskB : Form
        {
            public frmTaskB()
            {
                InitializeComponent();
            }
    
    
            bool btnForeIsChecked;
            bool btnBackIsChecked;
    
            public static void variableColours()
            {
                ColorDialog btnColour = new ColorDialog();
                DialogResult chosenColour;
    
                btnColour.FullOpen = true;
                chosenColour = btnColour.ShowDialog();
    
                if (chosenColour == DialogResult.Cancel)
                {
                    return;
                }
    
            }
            public static void callColour(bool isChosen, string txtbColour, string useColour)
            {
                //variableColours();
                if (isChosen)
                {
                    txtbColour = useColour;
                }
            }
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                // Application closing button
                this.Close();
            }
    
    
    
            public void btnFont_Click(object sender, EventArgs e)
            {
                //validating if checkbox 'chkTextBox1' is checked by user
                if (chkTextBox1.Checked)
                {
                    if (btnBackIsChecked)
                    {
                        //calling colour changing methods
                        callColour(true, "txtText1.BackColor", "BackColor.Color");
                    }
                    else if (btnForeIsChecked)
                    {
                        callColour(true, "txtText1.ForeColor", "ForeColor.Color");
                    }
                    if (btnBackIsChecked && btnForeIsChecked)
                    {
                        callColour(true, "txtText1.BackColor", "BackColor.Color");
                        callColour(true, "txtText1.ForeColor", "ForeColor.Color");
                    }
                }
    
                //validating if checkbox 'chkTextBox2' is checked by user
                else if (chkTextBox2.Checked)
                {
                    //calling colour changing methods
                    //ForeColourDialogtxt2();
                    //BackColourDialogtxt2();
                }
    
                //validating if checkbox 'chkTextBox1' and 'chkTextBox2' are checked by user
                if (chkTextBox1.Checked && chkTextBox2.Checked)
                {
                    //calling colour changing methods
                    /*ForeColourDialogtxt1();
                    BackColourDialogtxt1();
                    ForeColourDialogtxt2();
                    BackColourDialogtxt2();*/
                }
            }
    
            private void btnFore_Click(object sender, EventArgs e)
            {
                variableColours();
                btnForeIsChecked = true;
            }
    
            private void btnBack_Click(object sender, EventArgs e)
            {
    
                variableColours();
                btnBackIsChecked = true;
            }
        }
    }
    I need to store colors chosen by the user from the ColorDialog when he clicks on either fore/back so then I can take the values from the button and execute a change of background and/or font color, but can't figure out the way to do so.. cuz anytime I'm trying to call 'variableColour s()' it keeps asking the user to choose the color.
    My 'callColour' function will ask if chosen, then in both strings txtbColour and useColour I would put the adequate property.
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    Looking at your code, I didn't know what you wanted to do.
    Perhaps you want this behavior?
    Code:
    namespace Assignment1
    {
    	public partial class frmTaskB : Form
        {
            public frmTaskB()
            {
                InitializeComponent();
            }
            
            System.Drawing.Color FColor;
            System.Drawing.Color BColor;
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                // Application closing button
                this.Close();
            }
     
            public void btnFont_Click(object sender, EventArgs e)
            {
                //validating if checkbox 'chkTextBox1' is checked by user
                if (chkTextBox1.Checked)
                {
                	txtText1.ForeColor = FColor;
                	txtText1.BackColor = BColor;
                }
     
                if (chkTextBox2.Checked)
                {
                	txtText2.ForeColor = FColor;
                	txtText2.BackColor = BColor;
                }
            }
    
            private void btnFore_Click(object sender, EventArgs e)
            {
                ColorDialog colorDialog1 = new ColorDialog();
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    FColor = colorDialog1.Color; 
                }        
            }
     
            private void btnBack_Click(object sender, EventArgs e)
            {
                ColorDialog colorDialog1 = new ColorDialog();
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    BColor = colorDialog1.Color; 
                }        
            }
        }
    }

    Comment

    • SioSio
      Contributor
      • Dec 2019
      • 272

      #3
      I found the same post on the StackOverflow site.
      I posted the answer this time, but usually no one will post the answer.
      Multiposting is a rule violation.

      Comment

      Working...