none
Adding conditions to a checkbox RRS feed

  • Question

  • Just started with C# and Visual Studio and im now trying to build up a Analyzer tool but im not quite sure about the syntax still.

    I have a code here that i think should work and Visual Studio gives no errors, but no console Message gets written. I also tried to do a Message Box but nothing happened when i clicked checkbox 3.

    // Include libraries
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    // Define and start the form
    namespace Vis_Maskininformasjon
    {
        public partial class Analyser : Form
        {
            public Analyser()
            {
                InitializeComponent();
            }
    
    // On Form Load
    
    private void Analyser_Load(object sender, EventArgs e)
            {
            if (checkBox3.Checked)
            {
                Console.WriteLine("Checkbox 3 was marked");
    
            }
            }
               
    // Checkboxes            
    private void checkBox3_CheckedChanged(object sender, EventArgs e)
            {
                
            }
    
    private void checkBox2_CheckedChanged(object sender, EventArgs e)
            {
    
            }
    
    
    
    
    
    
    
    
    
    
    
    
    // Closes the Form
        }
    }
    

    • Moved by CoolDadTx Wednesday, June 4, 2014 1:48 PM Winforms related
    Tuesday, June 3, 2014 8:35 AM

All replies

  • Hi Avean,

    I think you just need to pop up notification when you check a check box. So try this code,

    private void checkBox3_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox3.Checked)
    	    MessageBox.Show("checkbox3 is checked");
            }
    Shan_k.

    Tuesday, June 3, 2014 9:20 AM
  • Hi,

    Write console.WriteLine in CheckBox3 event

    // Checkboxes           
    private void checkBox3_CheckedChanged(object sender, EventArgs e)
           
    {           
    MessageBox.Show("checkbox3 is checked");
           
    }


    PS.Shakeer Hussain

    Tuesday, June 3, 2014 9:32 AM
  • Hi,

    Write console.WriteLine in CheckBox3 event

    // Checkboxes           
    private void checkBox3_CheckedChanged(object sender, EventArgs e)
           
    {           
    MessageBox.Show("checkbox3 is checked");
           
    }


    PS.Shakeer Hussain

    That works, but i need to show the messagebox only when a condition is met. Not when the checkbox is changed. Basicly im building a troubleshooting program. It will check for certain registry settings and items. If condition is met, check the appropriate checkbox.

    • Edited by Avean Tuesday, June 3, 2014 10:01 AM
    Tuesday, June 3, 2014 9:59 AM
  • That works, but i need to show the messagebox only when a condition is met. Not when the checkbox is changed. Basicly im building a troubleshooting program. It will check for certain registry settings and items. If condition is met, check the appropriate checkbox.

    Hi Avean,

    Please refer to this code example:

    private void button1_Click(object sender, EventArgs e)
            {
                var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\......");
                if (key == null)
                {
                    // Key does not exist
                    checkBox1.Checked = false;
                }
                else
                {
                    // Key exists
                    checkBox1.Checked = true;
                }
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox1.Checked)
                {
                    MessageBox.Show("Key exists");
                }
                else
                {
                    MessageBox.Show("Key does not exist");
                }
            }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 4, 2014 6:05 AM
  • My code Works when i enter it under the load of my main form. But i have a button that Calls a New form, thats where the code suddenly dont work.

    Here is the code: Its exactly like the one in my main form, and that Works. But not in the form that gets called from the button.

    private void Analyser_Load(object sender, EventArgs e)
            {
                StartPosition = FormStartPosition.CenterParent;
                FormBorderStyle = FormBorderStyle.FixedSingle;
                MaximizeBox = false;
                MinimizeBox = false;
                checkBox2.Checked = true;
               
            }

    No errors, just the checkbox dont get checked and the name of the form is right as well.
    Wednesday, June 4, 2014 12:00 PM