Select - Yes or No - c#

I am making a yes or no comboBox labeled "comboBox". In my Items property of my yesnocomboBox, I put first item as Yes, and second item as No.
When I let my user select Yes, it has to show visibility to other certain labels and TextBoxs. How do I code to do that?
So far I have this and it isn't working:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(yesnocomboBox.SelectedItem = "0"){
}
}
Okay, I chose to use a checkbox instead. This is my code so far for when a user checks the checkbox:
private void yestochappedlipsCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (yestochappedlipsCheckBox.Checked = chapstickbrandsListBox.Visible = true)
(choosewhatyouwanttobuyLabel.Visible = true);
How do I make both of their visibilities to appear true?

private void yestochappedlipsCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (yestochappedlipsCheckBox.Checked)
{
chapstickbrandsListBox.Visible = true;
choosewhatyouwanttobuyLabel.Visible = true;
}
}

I see you said you were switching to a checkbox but to do it with a combo would be quite simple. I think you are using an assignment op in your code instead of an equal compare. Try the following by checking the index instead of the item.
Also, as good coding practice, keep your brackets consistent.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(yesnocomboBox.SelectedIndex == 0)
{
label1.Visible = true;
otherItem.Visible = true;
anotherItem.Visible = false;
}
}

If you're too serious to use that comboBox with Yes or No choices. make sure to set the DropDownStyle = DropDownList
bool? IsYes;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != -1)
{
//IsYes = comboBox1.SelectedIndex == 0;
choosewhatyouwanttobuyLabel.Visible = comboBox1.SelectedIndex == 0;
}
else
{
IsYes = null;
}
}

Related

How to enable button after all textboxes are not empty in c# winforms?

How can I make button property set to enabled=true after all my textboxes are not empty?
I'm learning programming and my apps are simple.
I know how to enable this property when one of my textboxes have text but this is not the case.
Use case is that user need to put data in both textboxes and after that will be able to click btn.
How in most simple way can I validate all form and then enable button?
There are just 2 tb:
https://i.imgur.com/JUslNWE.png
You need to create a TextBox_TextChanged event and subscribe to all text boxes.
private void TextBox_TextChanged(object sender, EventArgs e)
{
int notEmptyTextBoxCount = 0;
int textBoxCount = 0;
foreach (var item in Controls)
{
if (item is TextBox txtb)
{
textBoxCount++;
if (txtb.Text != String.Empty)
notEmptyTextBoxCount++;
}
}
if (textBoxCount == notEmptyTextBoxCount)
button.Enabled = true;
else
button.Enabled = false;
}
Thanks guys for all feedback.
I have managed to do this this way:
private void ValidateTextBoxes()
{
if (loginTextBox.Text.Length != 0 && passTextBox.Text.Length != 0)
{
generateHashBtn.Enabled = true;
}
else
{
generateHashBtn.Enabled = false;
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}

How to allocate two different functions to the same button?

I am working on Winforms with C#.
I have a problem with the logic, there are two different methods that I need to call, so that if I click the button, the first action should get applied and if I click the same button again, the second action should get applied.
This is not the exact code but I have an idea something like this:
private void button1_Click(object sender, EventArgs e)
{
if(button1.click==true)
{
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}
Here I have two problems regarding whether the button is already clicked:
If it's not clicked the Imgsave() should get activated.
If button is clicked the Imgsave() should get closed.
Can anyone please help me with this? Thanks.
You need to keep state somewhere. You can do this:
private bool buttonClicked = false;
private void button1_Click(object sender, EventArgs e)
{
if(!buttonClicked)
{
buttonClicked = true;
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}
This assumes you never going to click it a third time. If you are, you would need to handle that in some way.
I'd have either a class level variable track the number of times a button is clicked:
private bool _unclicked = false;
private void button1_Click(object sender, EventArgs e)
{
if(!_unclicked)
{
_unclicked = true; //toggle so next time the ELSE will be performed
fileNumber = 1;
ImgSave();
}
else
{
_unclicked = false; //toggle it off again
ImgSave.exit();
}
}
, or I'd store it in the .Tag of the button:
private void button1_Click(object sender, EventArgs e)
{
if(!button1.Tag.ToString() == "unclicked")
{
button1.Tag = "clicked"; //toggle so next time the ELSE will be performed
fileNumber = 1;
ImgSave();
}
else
{
button1.Tag = "unclicked"; //toggle it off again
ImgSave.exit();
}
}
You could also remove one event handler and add another:
private void button1_FirstClick(object sender, EventArgs e)
{
button1.Clicked -= button1_FirstClick;
button1.Clicked += button1_SecondClick;
fileNumber = 1;
ImgSave();
}
private void button1_SecondClick(object sender, EventArgs e)
{
button1.Clicked -= button1_SecondClick;
button1.Clicked += button1_FirstClick;
ImgSave.exit();
}
I've always been less of a fan of adding and removing event handlers to achieve things like this but it's quite a clean solution
You should save your state in a variable. Your state will change after first click and you can change the state of Clicking button with calling ConditionChanger() method anytime.
For example you may need change the state of variable when you clicked a second button.
private void ConditionChanger(){
myState = !myState;
}
Your variable :
private bool myState = false;
And your click event :
private void button1_Click(object sender, EventArgs e)
{
if(!myState)
{
myState = true;
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}

How to display the ContextMenuStrip when item is selected in a list box c# .net

I'm trying select an item from list box when is right clicked and show the ContextMenuStrip to display my options available, but when I click everywhere in the control (list box) is showing the ContextMenuStrip.
This is what I have in code:
private void lbSMTPEmails_MouseDown(object sender, MouseEventArgs e)
{
int SelectedIndex = lbSMTPEmails.IndexFromPoint(e.X, e.Y);
if (SelectedIndex == -1)
lbSMTPEmails.ContextMenuStrip.Hide();
else
{
lbSMTPEmails.SelectedIndex = SelectedIndex;
lbSMTPEmails.ContextMenuStrip.Show();
}
}
do you have any idea how to solve this?
Use opening event of ContextMenuStrip
void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
int SelectedIndex = lbSMTPEmails.IndexFromPoint( lbSMTPEmails.PointToClient(Cursor.Position) );
if (SelectedIndex == -1)
e.Cancel = true;
else
{
lbSMTPEmails.SelectedIndex = SelectedIndex;
}
}
I did by this way and it worked!
private void listbox_MouseDown(object sender, MouseEventArgs e)
{
ShowMenuStrip = listbox.IndexFromPoint(e.Location) >= 0; //This is a global bool variable
if (ShowMenuStrip)
listbox.SelectedIndex = listbox.IndexFromPoint(e.Location);
else
listbox.SelectedIndex = -1;
}
private void ContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
e.Cancel = !ShowMenuStrip;
}

C# winforms TreeView sometimes selects wrong item

I have winforms project with TreeView in it. Sometimes when I select some item other Item gets selected. I am very sure that there is not any code which could override the selection.
https://www.dropbox.com/s/qutmu0tmrkjspc6/disablederrortreeview.rar?dl=0
Here is example project. I need to update the treeview so there is timer which regurarly updates its content (so once per second items gets deselected) but when clicking fast enough sometimes it select different item, that I didn't click.
Anyone knows what the problem could be? Is there any workaround/fix, that could prevent this?
Thank you
Jakub
Two courses of action:
You can either: While the selection is happening disable the Tick event:
bool selecting = false;
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
selecting = true;
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
selecting = false;
// maybe you want to call the Tick now:
// timer1_Tick(null, null);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (selecting) return;
// do your stuff
..
}
Alternatively you could disable selecting while the Tick is running:
bool ticking = false;
TreeNode clickedNode = null;
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = ticking;
clickedNode = e.Node;
}
private void timer1_Tick(object sender, EventArgs e)
{
ticking = true;
// do your stuff
ticking = false;
// try to select the clicked node
if (clickedNode != null)
{ treeView1.SelectedNode = clickedNode; clickedNode = null;}
}

Making more than an event on a button

Please, I want to know how to make two events on one button like: when I first click on the button display an image and while still in debugging mode the second time I click
display another image. What are some ways to do this?
You can make something like:
protected void Button1Click(object sender, EventArgs e)
{
if (Img1.Visible == false)
{
Img1.Visible = true;
}
else
{
Img2.Visible = true;
}
}
I don't think you need two (or more) events to do what you want, you only need to trace how many times you clicked the button, for example using an instance variable.
private int clicks = 0;
protected void myButton_Click(object sender, EventArgs e)
{
if(clicks == 1)
{
// do something
}
if(clicks == 2)
{
// do other things
}
if(clicks > 2)
{
// something else
}
clicks++;
}
What about something like this: just make sure to declare the counter outside the button
int clickedCount = 0;
private void button1_Click(object sender, EventArgs e)
{
clickedCount++;
if (clickedCount % 2 == 0) { pictureBox1.ImageLocation = #"path"; } else { pictureBox1.ImageLocation = #"path"; }
}

Categories

Resources