469,612 Members | 1,673 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

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.
Expand|Select|Wrap|Line Numbers
  1. namespace Assignment1
  2. {
  3.     public partial class frmTaskB : Form
  4.     {
  5.         public frmTaskB()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.  
  11.         bool btnForeIsChecked;
  12.         bool btnBackIsChecked;
  13.  
  14.         public static void variableColours()
  15.         {
  16.             ColorDialog btnColour = new ColorDialog();
  17.             DialogResult chosenColour;
  18.  
  19.             btnColour.FullOpen = true;
  20.             chosenColour = btnColour.ShowDialog();
  21.  
  22.             if (chosenColour == DialogResult.Cancel)
  23.             {
  24.                 return;
  25.             }
  26.  
  27.         }
  28.         public static void callColour(bool isChosen, string txtbColour, string useColour)
  29.         {
  30.             //variableColours();
  31.             if (isChosen)
  32.             {
  33.                 txtbColour = useColour;
  34.             }
  35.         }
  36.  
  37.         private void btnExit_Click(object sender, EventArgs e)
  38.         {
  39.             // Application closing button
  40.             this.Close();
  41.         }
  42.  
  43.  
  44.  
  45.         public void btnFont_Click(object sender, EventArgs e)
  46.         {
  47.             //validating if checkbox 'chkTextBox1' is checked by user
  48.             if (chkTextBox1.Checked)
  49.             {
  50.                 if (btnBackIsChecked)
  51.                 {
  52.                     //calling colour changing methods
  53.                     callColour(true, "txtText1.BackColor", "BackColor.Color");
  54.                 }
  55.                 else if (btnForeIsChecked)
  56.                 {
  57.                     callColour(true, "txtText1.ForeColor", "ForeColor.Color");
  58.                 }
  59.                 if (btnBackIsChecked && btnForeIsChecked)
  60.                 {
  61.                     callColour(true, "txtText1.BackColor", "BackColor.Color");
  62.                     callColour(true, "txtText1.ForeColor", "ForeColor.Color");
  63.                 }
  64.             }
  65.  
  66.             //validating if checkbox 'chkTextBox2' is checked by user
  67.             else if (chkTextBox2.Checked)
  68.             {
  69.                 //calling colour changing methods
  70.                 //ForeColourDialogtxt2();
  71.                 //BackColourDialogtxt2();
  72.             }
  73.  
  74.             //validating if checkbox 'chkTextBox1' and 'chkTextBox2' are checked by user
  75.             if (chkTextBox1.Checked && chkTextBox2.Checked)
  76.             {
  77.                 //calling colour changing methods
  78.                 /*ForeColourDialogtxt1();
  79.                 BackColourDialogtxt1();
  80.                 ForeColourDialogtxt2();
  81.                 BackColourDialogtxt2();*/
  82.             }
  83.         }
  84.  
  85.         private void btnFore_Click(object sender, EventArgs e)
  86.         {
  87.             variableColours();
  88.             btnForeIsChecked = true;
  89.         }
  90.  
  91.         private void btnBack_Click(object sender, EventArgs e)
  92.         {
  93.  
  94.             variableColours();
  95.             btnBackIsChecked = true;
  96.         }
  97.     }
  98. }
  99.  
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 'variableColours()' 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.
Oct 16 '21 #1
2 8573
SioSio
264 256MB
Looking at your code, I didn't know what you wanted to do.
Perhaps you want this behavior?
Expand|Select|Wrap|Line Numbers
  1. namespace Assignment1
  2. {
  3.     public partial class frmTaskB : Form
  4.     {
  5.         public frmTaskB()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         System.Drawing.Color FColor;
  11.         System.Drawing.Color BColor;
  12.  
  13.         private void btnExit_Click(object sender, EventArgs e)
  14.         {
  15.             // Application closing button
  16.             this.Close();
  17.         }
  18.  
  19.         public void btnFont_Click(object sender, EventArgs e)
  20.         {
  21.             //validating if checkbox 'chkTextBox1' is checked by user
  22.             if (chkTextBox1.Checked)
  23.             {
  24.                 txtText1.ForeColor = FColor;
  25.                 txtText1.BackColor = BColor;
  26.             }
  27.  
  28.             if (chkTextBox2.Checked)
  29.             {
  30.                 txtText2.ForeColor = FColor;
  31.                 txtText2.BackColor = BColor;
  32.             }
  33.         }
  34.  
  35.         private void btnFore_Click(object sender, EventArgs e)
  36.         {
  37.             ColorDialog colorDialog1 = new ColorDialog();
  38.             if (colorDialog1.ShowDialog() == DialogResult.OK)
  39.             {
  40.                 FColor = colorDialog1.Color; 
  41.             }        
  42.         }
  43.  
  44.         private void btnBack_Click(object sender, EventArgs e)
  45.         {
  46.             ColorDialog colorDialog1 = new ColorDialog();
  47.             if (colorDialog1.ShowDialog() == DialogResult.OK)
  48.             {
  49.                 BColor = colorDialog1.Color; 
  50.             }        
  51.         }
  52.     }
  53. }
3 Weeks Ago #2
SioSio
264 256MB
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.
3 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Michael Johnson Jr. | last post: by
4 posts views Thread by Mark Lingen | last post: by
3 posts views Thread by Robert W. | last post: by
7 posts views Thread by =?Utf-8?B?bWFydGluMQ==?= | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.