At least one item always remain selected in ListBox. I want that when user clicks empty area of ListBox, selection should be cleared. How to do this?
I am trying to replicate Opera Notes as a part of my application. First i was using a binded DataGridView now i am using a binded ListBox on left pane
Handle the ListBox.MouseDown event.
Call ListBox.IndexFromPoint, passing the Location property from the MouseDown event's MouseEventArgs parameter.
This should return the index of the item that was clicked, or ListBox.NoMatches if the click was on an empty area.
If the return value is ListBox.NoMatches, set the ListBox.SelectedIndex property to -1 to clear the selection.
Mr. Avalanchis has answered the question already. I am just adding the code necessary to follow the steps what he has suggested. Hope the explicit code will help.
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
Point pt = new Point(e.X, e.Y);
int index = listBox1.IndexFromPoint(pt);
if (index <= -1)
{
listBox1.SelectedItems.Clear();
}
}
Related
I am working on a windows form app with a zedgraph and a datagridview. The datagridview has a row for every point in the line graph, and when the user clicks on a point in the graph I want it to highlight the equivalent row in the datagridview.
So how can I find out which point the user has clicked? (I don't need any code for datagridview part).
I figured it out. You can use GraphPane.FindNearestObject to find the point that was clicked.
It seems that nearestObject is null if you do not click a point and is of type LineItem if you do, and then index will tell you which point was clicked.
private void zedGraphControl_MouseClick(object sender, MouseEventArgs e)
{
object nearestObject;
int index;
this.zedGraphControl.GraphPane.FindNearestObject(new PointF(e.X, e.Y), this.CreateGraphics(), out nearestObject, out index);
if (nearestObject != null && nearestObject.GetType() == typeof(LineItem))
{
// 'index' is the index of that data point
dataGridView.CurrentCell = dataGridView.Rows[index].Cells[0];
}
}
I want to use listview to populate it with data and then use mouseclick event to fill some textboxes with data. I looked up an example in msdn:
ListViewItem theClickedOne = listView1.GetItemAt(e.X, e.Y);
ListViewItem theClickedtwo = listView1.FocusedItem;
if (theClickedOne != null)
{
MessageBox.Show(theClickedtwo.ToString());
//do your thing here.
//there is a reference to the listview item we clicked on
//in our theClickedOne variable.
}
but I couldn't think about a way to use it in order to differentiate the listviewitems I use since the fist Column in my program is the same and it will only give me a string with it's name(first Column).I want to have something similar to next example but for treeview.
void treeView1_NodeMouseClick(Object sender, TreeNodeMouseClickEventArgs e)
{
MessageBox.Show(e.Node.Text);
}
When populating you ListView, set the Tag property of the items, e.g.
newItem.Tag = "Item 1";
The Tag property has type object, so you can use anything you want here to identify the item. When handling the mouse click event simply check the Tag value again:
if((string)(clickedItem.Tag) == "Item 1")
{
// do stuff for this specific item.
}
I've currently got two lists I'm working with. One filled with items and the other one is empty. When the user double-clicks an item in the filled list it's supposed to add that item to the empty/second list, but instead of adding it to the top of that list I want the newly added item at the bottom. So the items should be added from the bottom up.
I'm working with a datagridview, but am willing to use listview/listbox as long as it gets the job done.
I added two list boxes to a windows form. listBox1 and listBox2
I added Seven Items to the first list box {One,Two,Three...}
I added the double click event handler where I
listBox2.Items.Add(listBox1.SelectedItem);
The new Item added to the bottom of the list, which is what it sounds like you want. I know the same thing works with a DataGridView.
Do you want them to be added physically to the bottom of the box leaving whitespace at the top until it is filled? Is that what you are trying to do?
Sorry this isn't really an answer, I guess I don't have enough rep to reply as a comment.
EDIT:
ok I think I have your answer now
Add a list box with your items, it doesn't have to be a list box your Datagridview would work fine.
Try using a FlowControlPanel and change alignment to bottom up, sounds easy, well it is.
Add labels to it, like this
//add a label to the flow control panel when you double click on an item
private void listBox1_DoubleClick(object sender, EventArgs e)
{
Label label = new Label();
label.Text = listBox1.SelectedItem.ToString();
label.Click += new EventHandler(label_Click);
label.AutoSize = true;
flowLayoutPanel1.Controls.Add(label);
label.BringToFront();
}
//Will remove the label if you click on it.
void label_Click(object sender, EventArgs e)
{
((Label)sender).Click -= new EventHandler(label_Click);
((Label)sender).Dispose();
}
brining the label to the front puts the new one at the bottom.
I think I understand what you want to do and the only way I know of is to fake it by pre-filling your second list / datagridview with with empty items so the user doesn't see anything. Then as your user makes selections from your first box you will replace the bottom most empty item with your new real item.
Try my code. I am using C# 3.5
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
int index = listBox2.Items.Count>0?listBox2.Items.Count:0;
listBox2.Items.Insert(index, listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
}
}
I want to select item in a ListView upon clicking. I also want to know what I clicked.
I work on winforms with c#.I also want to know How I can clicking the all row?
Just handle the Click event on the list and use the ListView.SelectedItems property to get what items are selected:
private void listView1_Click(object sender, EventArgs e)
{
var firstSelectedItem = listView1.SelectedItems[0];
}
u can use MouseEventArgs and get the mouse location check if it exists inside the selected item bound , that means the click was made on the selected item .
EDIT :
example :
private void myList_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (myList.SelectedItems.Count >= 1)
{
ListViewItem item = myList.SelectedItems[0];
//here i check for the Mouse pointer location on click if its contained
// in the actual selected item's bounds or not .
// cuz i ran into a problem with the ui once because of that ..
if (item.Bounds.Contains(e.Location))
{
MessageBox.Show("Double Clicked on :"+item.Text);
}
}
}
also if you use xaml for window then you must add MouseUp="listView1_Click" attribute to ListView tag
alt text http://img413.imageshack.us/img413/9417/snapshotapp.jpg
The ContextMenuStrip tied to the ListView control. However, the right click option (edit)
appear where ever i click on the ListView area, this gives me exceptional error because the implementation of edit can only cope with a selected row. I only want it to appear when on a selected row (blue highlighted row). How can i do it?
Reset the ContextMenuStrip property back to (none). Implement the MouseUp event handler and use ListView.HitTest() to find out where it was clicked. For example:
private void listView1_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
var loc = listView1.HitTest(e.Location);
if (loc.Item != null) contextMenuStrip1.Show(listView1, e.Location);
}
}