简体   繁体   中英

How do i use the checkBox2 to check if its checked or not?

The code:

private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            crawlLocaly1 = new CrawlLocaly();
            crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
            OptionsDB.Set_localOnly(checkBox2.Checked);
            if (checkBox2.Checked)
            {
                DialogResult dr = crawlLocaly1.ShowDialog(this);
                if (dr == DialogResult.Cancel)
                {
                    crawlLocaly1.Close();
                }
                else if (dr == DialogResult.OK)
                {
                    LocalyKeyWords.Add(crawlLocaly1.getText());
                    crawlLocaly1.Close();
                }
                removeExt = true;
            }
            else
            {
                removeExt = false;
            }
        }

This line:

OptionsDB.Set_localOnly(checkBox2.Checked);

Save the state of the checkBox2 if its checked or not. If its checked next time i will run my program i will see the V in the checkBox2 checked box. If i will uncheck the checkBox next time i run my program the box of the checkBox2 will be unchecked.

The problem is when i check the checkBox2 once close my program and run it again since the checkBox is checked now then for some reason it will make this:

DialogResult dr = crawlLocaly1.ShowDialog(this);

Wich will open and show the user a new Form. But i dont want it to be like that.

I want that if the user checked the checkBox when the program is running the new Form will show up. But if the user is running the program from the beginning and the checkBox is checked dont show the new Form just show that the checkBox is checked !

How should i fix it ?

You need an other boolean flag checkedInThisSession which initially set to false , and just set it to true in a checkbox OnChecked handler, then you can check this state easily. Hope all is clear

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
       crawlLocaly1 = new CrawlLocaly();
       crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
       OptionsDB.Set_localOnly(checkBox2.Checked);

       // UPDATED
       if (checkedInThisSession && checkBox2.Checked)
       {
           DialogResult dr = crawlLocaly1.ShowDialog(this);
           // ...
       }
       else
       {
           removeExt = false;
       }


       // UPDATED
       checkedInThisSession = checkBox2.Checked;
}

// In constructor    
checkedInThisSession = false;
checkBox2.Checked = OptionsDB.Get_localOnly(); 

The CheckedChanged event is fired every time the checkbox is set, also programmatically. So to solve this issue you need to ignore the first time the event is fired. So a boolean could be your solution:

private bool ignore = true;
private void checkBox2_CheckedChanged(object sender, EventArgs e){
   if(ignore == false){
      //your code here
   }
   else 
      ignore = false;    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM