Double press of a button c# - c#

I am struggling to make a button to be pressed multiple times with different outcomes. I have a few buttons to add some strings to a list. The problem is that I want to press the button to add an item and, if I press it again, to delete the same item.
private void labelPineapple_Click(object sender, EventArgs e)
{
if (!My_Pizza.Items.Contains(pineapple))
{
My_Pizza.Items.Add(pineapple);
labelPineapple.BackColor = Color.Green;
}
}

You want some kind of toggle behavior, we have this "toggle" behavior when the next click "denies" the last one. If I had some more details on if you have a specific button for each item or if you're a selection a item in a list and then pressing the button, I'd be more precise.
If you're selecting an item and then clicking "remove", you can do something like this:
private void DeleteItem_Click(object sender, EventArgs e)
{
listBox1.SelectedItems.Remove(listBox1.SelectedItems);
}
If somehow you're using the same button to remove the last value you added, you can use a local variable to store the old value like in:
private void labelPineapple_Click(object sender, EventArgs e)
{
if (!My_Pizza.Items.Contains(pineapple))
{
My_Pizza.Items.Add(pineapple);
labelPineapple.BackColor = Color.Green;
_oldValue = pineapple;
}
else
{
My_Pizza.Items.Remove(_oldValue);
}
}
If the same button will always and only add/remove the same item, use a toggle button instead.
Sometimes we developers try to solve simple things with harder approaches, when things get too complex, try to write down what you're trying to achieve and the possible solutions.
Update: if you have the pineapple object at the moment you're removing it, you don't need to store it as _oldValue. You can remove it directly inside your else statement.

Related

Grabbing the textbox label

Hello im making my first project with about 10 different textboxes where the user puts data in. when he/she clicks the the textbox the textbox text clears and a virtual numpad form pops up and when he leaves the textbox the numpad "hides".
right now (or i would) i have 2 events for every textbox, a click event and a leave event,
private void sheetWidthBox_Enter(object sender, EventArgs e)
{
vnumPadForm.Location = PointToScreen(new Point(sheetWidthBox.Right, sheetWidthBox.Top));
vnumPadForm.Show();
}
Im sure there is a way of dynamically coding that in one event and just grabbing the label name. i have played around with it a bit on my numpad like this and it works good;
private void button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
string num = b.Text;
SendKeys.SendWait(num);
}
Like that but instead i want to get the label name
right now (or i would) i have 2 events for every textbox, a click event and a leave event,
it works but very inefficient.
Change the name of the handler to something generic like "anyBox_Enter()", and update to the code below:
TextBox curTextBox = null;
private void anyBox_Enter(object sender, EventArgs e)
{
curTextBox = sender as TextBox;
vnumPadForm.Location = PointToScreen(new Point(curTextBox.Right, curTextBox.Top));
vnumPadForm.Show();
}
Note that I added a class level variable called "curTextBox" that gets set from the generic handler! This will track whatever TextBox was entered last.
Now, one by one, select each TextBox on your Form that you want to wire up to this common handler. After selecting each one, in the Properties Pane, click on the "Lightning Bolt" Icon to switch to the events for that control if they are not already showing. Find the "Enter" entry and change the dropdown to the right so that it says "anyBox_Enter".
Then in your button click handlers you can use the "curTextBox" variable to know where to send the key:
private void button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
string num = b.Text;
if (curTextBox != null) {
curTextBox.Text = num; // or possibly APPEND this to the TB?
}
}

How to react to a onclick in an ultragrid

I have code that creates an UltraGrid with a column that has the ColumnStyle.Button as it's style.
private void Grid_InitializeLayout(object sender, InitializeLayoutEventArgs e){
Grid.ResetEmptySelectedAppearance();
var column = Grid.SetColumn("Draw Lines", "Draw Lines", 30);
column.Style = ColumnStyle.Button;
Grid.HideOtherColumns();
}
Now I would like too make it react to being clicked on. I have found this but it does not show me how to bind the variable, usually I can either double click in the visual studio editor (but in this case this only directs me too the Grid_InitializeLayout) or I can go the the item in question and add the function to an OnClick variable, but this one doesn't exist.
private void Grid_InitializeRow(object sender, InitializeRowEventArgs e)
var buttoncell= e.Row.Cells["Draw Lines"];
//something here?
this is what I want to call
private void OnDrawLine(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e)
{
Debug.Print("test test");
}
It's probably something trivial but I'm stuck.
The UltraGrid responds to clicks on a cell button as explained in the link posted using an event called ClickCellButton. You need to subscribe to this grid event in the usual ways. (Designer or code doesn't matter)
private void Grid_ClickCellButton(object sender, ClickCellEventArgs e)
{
// Check if the click happens on the required column
if (e.Cell.Column.Key == "Draw Lines")
{
... your code ...
}
}

Simple ON/OFF toggle button with image

I'm working on a WinForms project where I'm trying to create an ON/OFF toggle button that uses two separate images (both located in project resources) for both the "ON" setting, and the "OFF" setting.
Based on what I've found online, I've used a CheckBox with its appearance set to "Button".
Here is the code I've got so far for my button:
private void ToggleButton_CheckedChanged(object sender, EventArgs e)
{
if (ToggleButton.Checked)
{
ToggleButton.BackgroundImage.Equals(Properties.Resources.ToggleButton_ON);
}
else
{
ToggleButton.BackgroundImage.Equals(Properties.Resources.ToggleButton_OFF);
}
}
For some reason nothing happens when I click on the button, and I'm not sure what I've done wrong here.
Basically, I'd like the background image to cycle back and fourth between ToggleButton_ON and ToggleButton_OFF when the user clicks on the button.
Change your code to:
private void ToggleButton_CheckedChanged(object sender, EventArgs e)
{
if (ToggleButton.Checked)
ToggleButton.BackgroundImage = Properties.Resources.ToggleButton_ON;
else
ToggleButton.BackgroundImage = Properties.Resources.ToggleButton_OFF;
}
The .Equals is for checking equality which you can override in your own classes.

C# WinForms showing a control as a dropdown

Gretings
I need to have a custom control for my application. Basically its an expression editing GUI. You have, say, expression:
If variable_x is greater than variable_y
And you can click on "greater than" and change it to other comparator (like, equal to or less than).
The control thus must look like a label, but when you click it, it must show a dropdown (like combobox does) that has a listview inside (or maybe some other control) so that user can choose something. In a sense, i need a combobox without the box itself, replaced by something else (in this case, a label).
I know how to make custom controls, i understand i must somehow DropDown on mouse click or enter keypress, and hook events so that when whatever i dropped has closed, the choice is made, and also somehow track if user clicked elsewhere so i can close this dropdowned control. But i dont know if this is easy to do (some built-in method exists) or i have to do it all myself? Dont want to redevelop the wheel....
Please tell me if there are easy ways to do this.
Thanks!
You can extend the ComboBox control to update the DropDownStyle on Enter and LostFocus events.
public partial class MyComboBox : ComboBox
{
public MyComboBox()
{
InitializeComponent();
this.Dock = DockStyle.Fill;
this.SelectionChangeCommitted += this.OnComboBoxSelectionChangeCommitted;
this.Enter += this.OnControlEnter;
this.LostFocus += this.OnComboBoxLostFocus;
}
private void OnControlEnter(object sender, EventArgs e)
{
this.DropDownStyle = ComboBoxStyle.DropDownList;
}
private void OnComboBoxLostFocus(object sender, EventArgs e)
{
this.DropDownStyle = ComboBoxStyle.Simple;
}
private void OnComboBoxSelectionChangeCommitted(object sender, EventArgs e)
{
// Notify to update other controls that depend on the combo box value
}
}

ListView ItemChanged puzzle with MessageBoxes - ItemSelectionChanged called twice

Here is the problem: I have a Windows Forms application that I'm developing, and in one segment I'm using a ListView control.
What I'm trying can be simply stated as: on event ListViewItemSelectionChange show a MessageBox for user to confirm the change, if not confirmed change to let's say the first item. This change to the first item would again fire ListViewItemSelecionChange, so I unregister and re-register the event handler method, so everything should be good, right?
What actually happens is that the handler method is called twice (actually ListView should fire two events on Selection change, one for deselect, other for newly selected item, but I have an e.IsSelected statement at the beginning to catch only selected items, so actually you could say that there are four events fired).
The problem is, if I generated the first event with mouse click on ListView item, and I've unsubscribed before programatically changing to the first item, what generates the second event firing? Is it some focus change because of the MessageBox call? Is there any way to prevent the second event to fire?
I have a simple example solution here, it can't be more simlified (25 SLOC), so if you can, please take a look. Note that commenting the line "if (ShowMessageBox())" stops the second event from firing, is this some focus change problem?
http://www.filedropper.com/listviewtestwithmsgbox
Edit: the relevant code:
private void listViewWithSelection1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
// listview actually generates two ItemSelectionChanged events,
// one for deselect of a item, and another event for a newly selected item (which we want here).
if (e.IsSelected)
{
if (ShowMessageBox())
Button1_Click(null, EventArgs.Empty);
label1.Text += "item selected ";
}
}
private bool ShowMessageBox()
{
return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
}
private void Button1_Click(object sender, EventArgs e)
{
// change ti first ListView item
listView1.ItemSelectionChanged -= listViewWithSelection1_ItemSelectionChanged;
listView1.Items[0].Selected = true;
listView1.ItemSelectionChanged += listViewWithSelection1_ItemSelectionChanged;
}
Hmm, can you describe how the selection is being changed to begin with? If it's by the user clicking to select an item, perhaps catch the Click or DoubleClick event rather than the ItemSelectionChanged event? I have this snippet I'm using on a program currently. If the user double clicks the list box (listView, in your case), do something with the selected item.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool ShowMessageBox()
{
return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
}
private void listView1_Click(object sender, EventArgs e)
{
if (ShowMessageBox())
listView1.TopItem.Selected = true;
label1.Text += "item selected ";
}
}
Edited to include relevant code.
One way to do this is to have a flag which says should the on change code run.
In your ListViewItemSelecionChange code you check the value of the flag and run code accordingly.

Categories