I've got the following problem:
When my program checks for a determined condition and if the checkbox was checked, it throws the MessageBox twice; according to the debugger, it goes inside the if, throws the messagebox and then issue the Checkbox1.Checked == false . After that it executes the entire if condition again.
I've tried MSDN documentation about Checkbox1.Threestate but I couldn't get to implement it. What can I do to solve this checkbox issue ?
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
string currPath = textBox1.Text;
if (!textBox1.Text.Contains("\\"))
{
MessageBox.Show("Please define the input folder before starting");
checkBox1.Checked = false;
}
else if (!textBox2.Text.Contains("\\"))
{
MessageBox.Show("Please define the XML Output folder before starting");
checkBox1.Checked = false;
}
else if (!textBox3.Text.Contains("\\"))
{
MessageBox.Show("Please define the Converted PPF Output Folder before starting");
checkBox1.Checked = false;
}
else if (!textBox4.Text.Contains("\\"))
{
MessageBox.Show("Please define the Invalid PPF Output Folder before starting");
checkBox1.Checked = false;
}
else
{
// calls the watcher
prg.ProgramProcessing(textBox1.Text);
}
}
Since you change the check inside the checkchange event, it trigger it a second time, you should unsubscribe the event, then resubscribe
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
checkBox1.CheckedChanged -= checkBox1_CheckedChanged;
try
{
// Your conditions
}
finally
{
checkBox1.CheckedChanged += checkBox1_CheckedChanged;
}
}
You could skip it if checkbox is not checked.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox1.Checked) return; //<- this.
string currPath = textBox1.Text;
if (!textBox1.Text.Contains("\\"))
{
MessageBox.Show("Please define the input folder before starting");
checkBox1.Checked = false;
}
else if (!textBox2.Text.Contains("\\"))
{
MessageBox.Show("Please define the XML Output folder before starting");
checkBox1.Checked = false;
}
else if (!textBox3.Text.Contains("\\"))
{
MessageBox.Show("Please define the Converted PPF Output Folder before starting");
checkBox1.Checked = false;
}
else if (!textBox4.Text.Contains("\\"))
{
MessageBox.Show("Please define the Invalid PPF Output Folder before starting");
checkBox1.Checked = false;
}
else
{
// calls the watcher
prg.ProgramProcessing(textBox1.Text);
}
}
You have multiple solution:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
checkBox1.CheckedChanged -= checkBox1_CheckedChanged;
// Yours conditions
checkBox1.CheckedChanged += checkBox1_CheckedChanged;
}
OR
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked == false)
{
return;
}
// your conditions..
}
As soon as you set Checked = false, you change its value, and the Changed event is fired again.
One way to solve this, is using a guard variable that makes sure you don't go through the code again while you are handling the event:
private bool handlingCheckboxChanged;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(handlingCheckboxChanged) { return; }
handlingCheckboxChanged = true;
try {
// Your original code
} finally {
handlingCheckboxChanged = false;
}
}
Even better would be to make sure that the user cannot check the checkbox in the first place, for example by handling the change events of the text boxes and only enabling your checkbox once all the conditions have been met.
Related
The student is back! I am trying to self-teach C#, so please pardon my simple but many questions. I appreciate you all.
I am working on a quiz app.What I want but cant seem to achieve is that when "Testing mode" (radio button) is selected, "Number of questions" need to be grayed out.Otherwise, student can select number of questions to attempt.
Here is my code
private void rdotesting_CheckedChanged(object sender, EventArgs e)
{
if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
MessageBox.Show("Click 'Start' to continue..");
btnclose.Hide();
}
else
{
MessageBox.Show("You Must select an option to continue.");
}
}
//if testing mode, dissable number of questions ,and also the 'Close' button
Like this - see comments
private void rdotesting_CheckedChanged(object sender, EventArgs e)
{
//this event fires when rdotesting is checked or when it is unchecked (change)
//set the enabled state of the nud/button to th opposite of the checked state
//ie when checked = true then enabled = false
numberQsNUD.Enabled = !rdotesting.Checked;
closeButton.Enabled = !rdotesting.Checked;
//if not in test mode, exit to stop the message showing every time
if(!rdotesting.Checked)
return;
if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
rdotesting.Checked = false; //user said no; turn off test mode
simply you can try this code...
private void rdotesting_CheckedChanged(object sender, EventArgs e) {
if(rdotesting.Checked) {
numberQsNUD.Enabled = false;
closeButton.Enabled = false;
} else {
numberQsNUD.Enabled = true;
closeButton.Enabled = true;
}
}
or you can customize this via using this code...
private void EnableComponent(bool check) {
numberQsNUD.Enabled = check;
closeButton.Enabled = check;
}
private void rdotesting_CheckedChanged(object sender, EventArgs e) {
if(rdotesting.Checked) {
EnableComponent(false);
} else {
EnableComponent(true);
}
}
I have a notify icon in my Winforms form and it seems that when any kind of event happens the tray icon is duplicated.
I have debugged one of the issues, being that it is duplicated when the dialog box is closed after using it.
It happens in debug and when released.
The other issue it with a timer that runs method.
I cannot see why this happens. My timer ran 60 times last night and each time it has four methods to run and there were hundreds of icons in the tray.
My code is as follows:
public Form1()
{
InitializeComponent();
notifyIcon1.BalloonTipText = "Mappi CSV Manager is running.";
notifyIcon1.BalloonTipTitle = "Mappi CSV Manager";
notifyIcon1.Text = "Mappi CSV Manager";
}
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
ShowIcon = false;
ShowInTaskbar = false;
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(1000);
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowInTaskbar = true;
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// Call Dispose to remove the icon out of notification area of Taskbar.
notifyIcon1.Dispose();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (CloseCancel() == false)
{
e.Cancel = true;
};
}
//When closing the form
public static bool CloseCancel()
{
const string message = "If you close the program, no files will be generated!";
const string caption = "Stop!";
var result = MessageBox.Show(message, caption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
return true;
else
return false;
}
//Set new value for timer
private void UdTimerValue_ValueChanged(object sender, EventArgs e)
{
timer1.Interval = Convert.ToInt32(udTimerValue.Value) * 60000;
}
//Start generating CSV's
private void Timer1_Tick(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
if (AutoGenerateEnabled)
{
richLogWindow.AppendText("CSV Created at: " + DateTime.Now + "\r\n");
var startdate = "";
if(DateTime.Now.Hour == 1)
{
richLogWindow.Clear();
startdate = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
CSVGenerator.GenerateCSV(startdate, this);
}
else
{
startdate = DateTime.Today.ToString("yyyy-MM-dd");
CSVGenerator.GenerateCSV(startdate, this);
}
}
else
{
return;
}
}
}
Why is this code producing another tray icon every time a button is clicked or an event happens.
TIA
I found the error. I have put RichTextBoxAppend.AddNewText("test me", new Form1()); the new form was created each time a process was run. I am an idiot!
I am using "●" as the password character for my login form. I have a check box there titled "Show Password". If a person clicks on "show password" the "●" should be removed and the password should be shown as actual text. I am not able to do that; here is the code:
private void LoginForm_Load(object sender, EventArgs e)
{
Passtxt.PasswordChar = '●';
if (checkBox_Showpass.Checked == true)
{
Passtxt.PasswordChar = '\0';
}
}
Ok, finally got that to work: Copied the if statement and pasted that into checkBox_Showpass_CheckedChanged method:
private void checkBox_Showpass_CheckedChanged(object sender, EventArgs e)
{
if (checkBox_Showpass.Checked == true)
{
this.Passtxt.PasswordChar = '\0';
}
}
Use UseSystemPasswordChar property to enable or disable password mode:
private void checkBox_Showpass_CheckedChanged(object sender, EventArgs e)
{
if (checkBox_Showpass.Checked == true)
Passtxt.UseSystemPasswordChar = false;
else
Passtxt.UseSystemPasswordChar = true;
}
I have a textBox that the user should fill it. Default text of the textBox is blank. I want that if the user enter some text in it, buttons will be enable.
private void txtLicense_TextChanged(object sender, EventArgs e)
{
if (txtEconomic.Text != "")
btnInsert.Enabled = true;
}
but in this code, if the user enter some text and then erase it, it dosent work. I mean the buttons will be enable ...
how can I do that?
thanks
just do btnInsert.Enabled = false;
private void txtLicense_TextChanged(object sender, EventArgs e)
{
if (txtEconomic.Text != "")
btnInsert.Enabled = true;
else
btnInsert.Enabled = false;
}
Problem : You don't have any logic to disable the Button.
Solution : You need to add else block to disable the Button.
Suggestion: i would suggest you to use String Method String.IsNullOrEmpty() to check wether your textbox input string is Null or Empty.
if (!String.IsNullOrEmpty(txtEconomic.Text))
btnInsert.Enabled = true;
else
btnInsert.Enabled = false;
private void txtLicense_TextChanged(object sender, EventArgs e)
{
if (txtEconomic.Text.Length > 0)
btnInsert.Enabled = true;
else
btnInsert.Enabled = false;
}
I have a TextBox in my winforms. When a user starts typing in it I will like to set the AcceptButton property to call another function. However it is calling another function which is called by a Button in my ToolStrip. To elaborate, here is my code below:
private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = searchBtn;
}
private void searchBtn_Click_1(object sender, EventArgs e)
{
if (locNameTxtBx.Text != "")
{
List<SearchLocation> locationsArray = new List<SearchLocation>();
var location = locNameTxtBx.Text;
SearchLocation loc = new SearchLocation();
loc.Where = location;
locationsArray.Add(loc);
mapArea.VE_FindLocations(locationsArray, true, true, null);
mapArea.VE_SetZoomLevel(14);
}
else
{
MessageBox.Show("Please Enter Location");
}
}
searchBtn is a Button in the ToolStrip. So, when I try to run this code, I get this error
Cannot implicitly convert type 'System.Windows.Forms.ToolStripButton' to 'System.Windows.Forms.IButtonControl'. An explicit conversion exists (are you missing a cast?)
I have tried casting it as a ToolstripButton like this:
private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = (ToolStripButton)searchBtn;
}
You could use two delegates and set the delegate to use in the locNameTxtBx_TextChanged method.
private delegate void ToUseDelegate();
ToUseDelegate delegateIfNoText = delegate{
MessageBox.Show("Please Enter Location");
}
ToUseDelegate delegateIfText = delegate{
List<SearchLocation> locationsArray = new List<SearchLocation>();
var location = locNameTxtBx.Text;
SearchLocation loc = new SearchLocation();
loc.Where = location;
locationsArray.Add(loc);
mapArea.VE_FindLocations(locationsArray, true, true, null);
mapArea.VE_SetZoomLevel(14);
}
ToUseDelegate delToUse = delegateIfNoText;
private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = searchBtn;
if (locNameTxtBx.Text != ""){
delegateToUse = delegateIfNoText;
} else {
delegateToUse = delegateIfText;
}
}
private void searchBtn_Click_1(object sender, EventArgs e)
{
delegateToUse();
}