Constant Check if ListView is Selected - c#

I have done this before and I have a way of doing it, but I want to make sure it is the best way. I have a ListView in Details view. I also have a button. I only want that button to be enabled if there is an item selected (multiselect is disabled). Items will be added and removed to this listview but the button should be enabled anytime there is a selected item and disabled otherwise.
My event handler:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
button1.Enabled = true;
else
button1.Enabled = false;
}
That is what I have, just wondering if that will always work or are there freak incidents where it fails? Like if I delete or add things or anything else?

It'd be better if you show what you have - but in short, you start with the button disabled, and in the list view selectedindexChanged event enable the button if the list view has a selectedItems.Count of 1. Disable it if no item is selected. Here's a link which may help:
ListView selectedindexchanged

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 9; i++)
{
listView1.Items.Add("kashif");
}
button1.Enabled = false;
}
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
button1.Enabled = listView1.SelectedItems.Count > 0;
}
private void button2_Click(object sender, EventArgs e)
{
foreach (ListViewItem v in listView1.SelectedItems)
{
v.Remove();
}
}
}
Before Button2 Click
After Button2 click

Related

Multiple ListBox on same Window doesn't select when focus moving ListBox

I have two ListBox in my window: LstStoreItems and LstPlayerItems. It looks like this:
The idea here is that when you select an item from the Store, the Sell button is disabled, and it UnselectAll on the Player Inventory, and vise Versa. Here's the code:
private void LstPlayerItems_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
LstStoreItems.UnselectAll();
BtnBuy.IsEnabled = false;
BtnSell.IsEnabled = true;
}
private void LstStoreItems_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
LstPlayerItems.UnselectAll();
BtnBuy.IsEnabled = true;
BtnSell.IsEnabled = false;
}
Now, though, if I select an item in the Player Inventory, and then go to select an item in the Store Inventory, it executes the code, but it doesn't actually select the item I'd clicked. How can I get it to select the item when I'm changing the focused ListBox?
I believe what is going on is the other ListBox's SelectionChanged event is being triggered when you do your UnSelectAll which is causing your new selected item to be unselected. Try checking to make sure that you have a selected item in ListBox before unselecting the items in the other ListBox.
Something like this:
private void LstPlayerItems_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (((ListBox)sender).SelectedIndex != -1)
LstStoreItems.UnselectAll();
BtnBuy.IsEnabled = false;
BtnSell.IsEnabled = true;
}
private void LstStoreItems_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (((ListBox)sender).SelectedIndex != -1)
LstPlayerItems.UnselectAll();
BtnBuy.IsEnabled = true;
BtnSell.IsEnabled = false;
}

Show button only when focus is on textbox

Is this possible to display button on Windows Form only when focus is on specific textbox?
Tried that with this approach:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
button3.Visible = false;
}
No luck, because button click does not work then, because button is hidden immediately after textbox lost focus, preventing it from firing button3_Click(/*...*/) { /*...*/ } event.
Now I'm doing it like that:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
//button3.Visible = false;
DoAfter(() => button3.Visible = false);
}
private async void DoAfter(Action action, int seconds = 1)
{
await Task.Delay(seconds*1000);
action();
}
Form now waits for a second and only then hides button3.
Is there any better approach?
I think you want to display the button only when focus is on specific textbox or the focus is on the button.
To do this you can check the Focused property of button3 in the Leave event of textBox2 and only hide the button if the button doesn't have focus. Note that the button will get focus before the Leave event of textBox2 fires.
You will then need to hide the button in the scenario where button3 loses focus and the focus moves to somewhere other than textBox2. You can use exactly the same technique here by handling the Leave event of button3 and only hiding button3 if textBox2 does not have focus.
The following code should fit your requirements:
private void textBox2_Leave(object sender, EventArgs e)
{
if (!button3.Focused)
{
button3.Visible = false;
}
}
private void button3_Leave(object sender, EventArgs e)
{
if (!textBox2.Focused)
{
button3.Visible = false;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
Why not work with the GotFocus and LostFocus event of the TextBox?
private void textBox2_GotFocus(object sender, EventArgs e)
{
button3.Visible = true;
}
Then hide the button on the click event.
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
button3.Visible = false;
}
How about you add a Panel and place the button and text boxes in that panel and when user MouseHovers that Panel then display the button...
This way user would be able to click on the button...
This is the event you are looking for, I think...
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
UPDATE:
var textboxFocussed = false;
private void textBox2_Enter(object sender, EventArgs e)
{
textboxFocussed = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textboxFocussed = false;
}
UPDATE 2
private void Panel_GotFocus(object sender, EventArgs e)
{
button3.Visible = textboxFocussed;
}
private void Panel_LostFocus(object sender, EventArgs e)
{
button3.Visible = false;
}
Here are the details of the Panel Events
you can add Enter event handler for all controls on form at Load. Just make sure to skip the controls on which you want to show the button.
List<string> strControlException = new List<string>();
public Form1()
{
InitializeComponent();
strControlException.Add("btnMain");
strControlException.Add("txtMain");
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count;i++ )
{
if (!strControlException.Contains(Controls[i].Name))
{
Controls[i].Enter += new EventHandler(hideButton);
}
}
}
private void txtMain_Enter(object sender, EventArgs e)
{
btnMain.Visible = true;
}
private void hideButton(object sender, EventArgs e)
{
btnMain.Visible = false;
}
btnMain (Button you want to Manipulate) and txtMain (Which controls the vibility of the button) are the controls in contention here
Add more controls on the form to test.
Explanation for the above code :
First initialize a list with the names of controls that should show the Button
On Form Load add an Event handler to all controls (except the one in our list)
In the handler function hide the button. (You might want to perform more logic here based on the control that called this function)
Button is hidden by default and only on textbox Enter event we show the button.

select blank row it deselect items from listview

int selectedRowIndex = 0;
private void listViewUserList_SelectedIndexChanged(object sender, EventArgs e)
{
if(listViewUserList.SelectedIndices.Count == 0)
return;
}
private void Form_Load(object sender, EventArgs e)
{
if(listViewUserList.Items.Count > 0)
{
listViewUserList.Items[selectedRowIndex].Selected = true;
listViewUserList.Select();
}
}
On form load I'm selecting first row in ListView. But when I select blank row it deselect items from ListView.
What should I do when click on blank row then select previously selected row? I have searched on Google but didn't find any good answers. Answers on stackoverflow only for WPF not for windows form.
This can be done by adding an event handler for the ListView's MouseUp event. If you still need the event handler for the SelectedIndexChanged and it's delegate method, listViewUserList_SelectedIndexChanged, remove from it any code that handles the item selection:
private void listViewUserList_SelectedIndexChanged(object sender, EventArgs e)
{
// remove the 2 lines below, as the selection handling is done in the
// MouseUp event handler
//if(listViewUserList.SelectedIndices.Count == 0)
// return;
// TODO: get the column data
}
Add the following event handler:
listViewUserList.MouseUp += new MouseEventHandler(this.listViewUserList_MouseUp);
and the following method:
private void listViewUserList_MouseUp(object sender, MouseEventArgs e)
{
if (listViewUserList.SelectedItems.Count > 0)
{
selectedRowIndex = listViewUserList.SelectedItems[0].Index;
}
else
{
listViewUserList.Items[selectedRowIndex].Selected = true;
}
}
Save the previous index in the class.
If current index points to an empty string, change the selection back to previous selection using (1)
Then call myListView.Select();
Code:
private void Form_Load(object sender, EventArgs e)
{
if(listViewUserList.Items.Count > 0 && (String.Empty != listViewUserList.Items[selectedRowIndex].ToString() ))
{
listViewUserList.Items[selectedRowIndex].Selected = true;
listViewUserList.Select();
}
}

CheckBox button and ContextMenuStrip for dropdown menu

I'm tring to implement a button which have a dropdown menu when checked and this menu is gone when unchecked. My problem is I cannot uncheck the checkbox when it or its menu lost focus.
The checkbox's appearance mode is button.
My code:
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
I've tried to uncheck the checkBox on contextMenuStrip's VisibleChanged / Closed event but this caused menu not to hide (or hide and show immediately).
The example below does not, of course, include the code you would need for swapping BackGroundImage of the CheckBox to indicate CheckState. The events to "wire-up" should be obvious. Hope this is helpful.
// tested in VS 2010 Pro, .NET 4.0 FrameWork Client Profile
// uses:
// CheckBox named 'checkBox1
// ContextMenuStrip named 'contextMenuStrip1
// TextBox named 'cMenuSelectionInfo for run-time checking of results
// used to position the ContextMenuStrip
private Point cPoint;
// context click ? dubious assumption that 'right' = context click
private bool cmOpenedRight;
// the clicked ToolStripMenuItem
private ToolStripMenuItem tsMIClicked;
private void checkBox1_MouseDown(object sender, MouseEventArgs e)
{
cmOpenedRight = e.Button == MouseButtons.Right;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// positioning the CheckBox like this
// is something in a 'real-world' example
// you'd want to do in the Form.Load event !
// unless, of course, you'd made the CheckBox movable
if(checkBox1.Checked)
{
contextMenuStrip1.Show();
cPoint = PointToScreen(new Point(checkBox1.Left, checkBox1.Top + checkBox1.Height));
contextMenuStrip1.Location = cPoint;
}
else
{
contextMenuStrip1.Hide();
}
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// assume you do not have to check for null here ?
tsMIClicked = e.ClickedItem as ToolStripMenuItem;
tbCbMenuSelectionInfo.Text = tsMIClicked + " : " + ! (tsMIClicked.Checked);
}
private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = checkBox1.Checked;
}
private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
if (cmOpenedRight)
{
tbCbMenuSelectionInfo.Text += " : closed because : " + e.CloseReason.ToString();
}
}
I think your approach of unchecking the check box on the context menu's closed event is a good one, what you need is a bit of "event cancelling logic"(c), like this:
private void OnContextClosing(object sender, EventArgs e)
{
_cancel = true;
cbSettings.Checked = false;
_cancel = false;
}
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if(_cancel)
return;
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
This will keep your CheckChanged event from re-checking your checkbox.

Right click to select a row in a Datagridview and show a menu to delete it

I have few columns in my DataGridView, and there is data in my rows. I saw few solutions in here, but I can not combine them!
Simply a way to right-click on a row, it will select the whole row and show a menu with an option to delete the row and when the option selected it will delete the row.
I made few attempts but none is working and it looks messy. What should I do?
I finally solved it:
In Visual Studio, create a ContextMenuStrip with an item called "DeleteRow"
Then at the DataGridView link the ContextMenuStrip
Using the code below helped me getting it work.
this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown);
this.DeleteRow.Click += new System.EventHandler(this.DeleteRow_Click);
Here is the cool part
private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
var hti = MyDataGridView.HitTest(e.X, e.Y);
MyDataGridView.ClearSelection();
MyDataGridView.Rows[hti.RowIndex].Selected = true;
}
}
private void DeleteRow_Click(object sender, EventArgs e)
{
Int32 rowToDelete = MyDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
MyDataGridView.Rows.RemoveAt(rowToDelete);
MyDataGridView.ClearSelection();
}
For completness of this question, better to use a Grid event rather than mouse.
First Set your datagrid properties:
SelectionMode to FullRowSelect
and
RowTemplate / ContextMenuStrip to a context menu.
Create the CellMouseDown event:-
private void myDatagridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
int rowSelected = e.RowIndex;
if (e.RowIndex != -1)
{
this.myDatagridView.ClearSelection();
this.myDatagridView.Rows[rowSelected].Selected = true;
}
// you now have the selected row with the context menu showing for the user to delete etc.
}
}
private void dgvOferty_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
{
dgvOferty.ClearSelection();
int rowSelected = e.RowIndex;
if (e.RowIndex != -1)
{
this.dgvOferty.Rows[rowSelected].Selected = true;
}
e.ContextMenuStrip = cmstrip;
}
TADA :D. The easiest way period. For custom cells just modify a little.
It's much more easier to add only the event for mousedown:
private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hti = MyDataGridView.HitTest(e.X, e.Y);
MyDataGridView.Rows[hti.RowIndex].Selected = true;
MyDataGridView.Rows.RemoveAt(rowToDelete);
MyDataGridView.ClearSelection();
}
}
This is easier. Of cource you have to init your mousedown-event as already mentioned with:
this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown);
in your constructor.
All the answers posed in to this question are based on a mouse click event. You can also assign a ContenxtMenuStrip to your DataGridview and check if there is a row selected when the user RightMouseButtons on the DataGridView and decide whether you want to view the ContenxtMenuStrip or not. You can do so by setting the CancelEventArgs.Cancel value in the the Opening event of the ContextMenuStrip
private void MyContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
//Only show ContextMenuStrip when there is 1 row selected.
if (MyDataGridView.SelectedRows.Count != 1) e.Cancel = true;
}
But if you have several context menu strips, with each containing different options, depending on the selection, I would go for a mouse-click-approach myself as well.
base on #Data-Base answer it will not work until make selection mode FullRow
MyDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
but if you need to make it work in CellSelect Mode
MyDataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
// for cell selection
private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
var hit = MyDataGridView.HitTest(e.X, e.Y);
MyDataGridView.ClearSelection();
// cell selection
MyDataGridView[hit.ColumnIndex,hit.RowIndex].Selected = true;
}
}
private void DeleteRow_Click(object sender, EventArgs e)
{
int rowToDelete = MyDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
MyDataGridView.Rows.RemoveAt(rowToDelete);
MyDataGridView.ClearSelection();
}
private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
MyDataGridView.ClearSelection();
MyDataGridView.Rows[e.RowIndex].Selected = true;
}
}
private void DeleteRow_Click(object sender, EventArgs e)
{
Int32 rowToDelete = MyrDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
MyDataGridView.Rows.RemoveAt(rowToDelete);
MyDataGridView.ClearSelection();
}
private void dataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
if (e.RowIndex != -1)
{
dataGridView1.ClearSelection();
this.dataGridView1.Rows[e.RowIndex].Selected = true;
e.ContextMenuStrip = contextMenuStrip1;
}
}
It is work for me without any errors:
this.dataGridView2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown);
this.dataGridView2.Click += new System.EventHandler(this.DeleteRow_Click);
And this
private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hti = dataGridView2.HitTest(e.X, e.Y);
dataGridView2.ClearSelection();
dataGridView2.Rows[hti.RowIndex].Selected = true;
}
}
private void DeleteRow_Click(object sender, EventArgs e)
{
Int32 rowToDelete = dataGridView2.Rows.GetFirstRow(DataGridViewElementStates.Selected);
if (rowToDelete == -1) { }
else
{
dataGridView2.Rows.RemoveAt(rowToDelete);
dataGridView2.ClearSelection();
}
}
You can also make this a little simpler by using the following inside the event code:
private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
rowToDelete = e.RowIndex;
MyDataGridView.Rows.RemoveAt(rowToDelete);
MyDataGridView.ClearSelection();
}
}
See here it can be done using the DataGridView RowTemplate property.
Note: This code isn't tested but I've used this method before.
// Create DataGridView
DataGridView gridView = new DataGridView();
gridView.AutoGenerateColumns = false;
gridView.Columns.Add("Col", "Col");
// Create ContextMenu and set event
ContextMenuStrip cMenu = new ContextMenuStrip();
ToolStripItem mItem = cMenu.Items.Add("Delete");
mItem.Click += (o, e) => { /* Do Something */ };
// This makes all rows added to the datagridview use the same context menu
DataGridViewRow defaultRow = new DataGridViewRow();
defaultRow.ContextMenuStrip = cMenu;
And there you go, as easy as that!
I have a new workaround to come in same result, but, with less code.
for Winforms... That's example is in portuguese
Follow up step by step
Create a contextMenuStrip in your form and create one item
Sign one event click (OnCancelarItem_Click) for this contextMenuStrip
Create a event 'UserDeletingRow' on gridview
and now... you've simulating on key press del from user
you don't forget to enable delete on the gridview, right?!
and finally...

Categories