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;}
}
Related
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();
}
}
Suppose we have a ListBox with the Custom elements and the blank panel . When you drag these items to the panel , they must build on a certain logic. For example, if there's nothing panel , the element is located in the middle. But if there is , then the new element is to stay near the element that is closest to it . As such it is possible to implement?
For example:
I modified this answer of working drag and drop implementation by adding some sorting logic. Placing an item in the middle visually can be done with CSS styling.
Assumption: "closest to it" means closest alphabetically.
public object lb_item = null;
private void listBox1_DragLeave(object sender, EventArgs e)
{
ListBox lb = sender as ListBox;
lb_item = lb.SelectedItem;
lb.Items.Remove(lb.SelectedItem);
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (lb_item != null)
{
listBox1.Items.Add(lb_item);
lb_item = null;
// Here I added the logic:
// Sort all items added previously
// (thereby placing item in the middle).
listBox1.Sorted = true;
}
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
lb_item = null;
if (listBox1.Items.Count == 0)
{
return;
}
int index = listBox1.IndexFromPoint(e.X, e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
lb_item = null;
}
Basically in the code below where i highlight, i would like to know how to use drawBricks method in the timer tick when that button(btnDisplayBricks) is pressed. Because i am using a timer tick and picturebox for paper drawings etc i cannot just simply call the method from within the button click event because the paper then clears in the timer only allowing me to display the bricks before timer1 starts any ideas.
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (btnDisplayBricks_Click[0] = true) ///code here problem
//then call method
DrawBricks(paper);
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
}
}
}
problem is in your equation, you should use == instead of =
if (btnDisplayBricks_Click[0] == true)
also move methodbtnDisplayBricks_Click outside of timer1_Tick
private bool buttonClicked = false;
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (buttonClicked)
{
DrawBricks(paper);
// maybe you want to set buttonclicked to false again, but specs are not clear to me
// buttonClicked = false;
}
}
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
buttonClicked = true;
}
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;
}
}
I createbtn1_MouseDown Event.
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
btn1.BackgroundImage = Properties.Resources.click;
}
I want to disable that btn1_MouseDownt Event behind this RadioButton when it checked. I mean to say when Radiobutton checked then this btn1_MouseDown event should not work.
private void r1_CheckedChanged(object sender, EventArgs e)
{
if (r1.Checked == true)
clock = 5;
radioButton2.Enabled = false;
radioButton3.Enabled = false;
radioButton4.Enabled = false;
}
Is that possible ?
inside r1_checkeChanged add:
btn1.MouseDown -=btn1_MouseDown;
You can remove the event:
btn1.MouseDown -= btn1_MouseDown
Of course don't forget to add it back again when you need it.
However, instead of doing that I would change your event handler's code to check the state of the radio button:
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
if (!r1.Checked)
btn1.BackgroundImage = Properties.Resources.click;
}
1st option can be to remove the event at radio button check_change event like this:
btn1.MouseDown -=btn1_MouseDow;
but i dont prefer this way.
Second is to add a flag at start of this event and check whether to execute the event or not
like this:
bool flag;
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
if (flag) return;
btn1.BackgroundImage = Properties.Resources.click;
}