ListView version of ListBox.SelectedItem - c#

hi all i'm trying to perform an action once the user double clicks on an item in the listview.
but there doesnt appear to be any methods available for this. can anybody please help me out here?
thanks lots :D
ANSWER (Thanks to Kyle's link):
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (listView1.Items.Count >= 1)
Process.Start(listView1.SelectedItems[0].Text);
}

You could handle the doubleclick method of the ListView and then loop through each selected item. Something like:
private void thelistview_MouseDoubleClick(object sender, MouseEventArgs e)
{
foreach(ListViewItem item in thelistview.SelectedItems)
{
//do something with item
}
}
You also need to hook up the event unless you do it in the designer...
thelistview.MouseDoubleClick +=
new System.Windows.Forms.MouseEventHandler(this.thelistview_MouseDoubleClick);

Have a look at this blog. Should help you do what you want to.

Related

How to move move selected items from one C1List to another in C#?

C1List component is something similar to listBox. I have button and event for that button:
private void btnNavigateLeft_Click(object sender, System.EventArgs e)
{
if (lbRight.SelectedIndex != -1)
{
lbLeft.InsertItem();
lbRight.RemoveItem(lbRight.SelectedIndex);
}
}
Any idea how to do that?
Try this,
lst_2.Items.Add(lst_1.SelectedItem);
lst_1.Items.Remove(lst_1.SelectedItem);

C# ovalshape on the front on listview

I have listview and it doesn't support image in second column. I try create ovalshape, but it olways is on the background of listview. So nobody can see it. I try ovalShape1.BringToFront();, but it doesn't work. Anybody can help me?
Here is a simple example with an external variable.
I assume you have added your images to an ImageList imageList1.
int imgIndex = 0;
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex == 1)
e.Graphics.DrawImage(imageList1.Images[imgIndex], e.Bounds);
else e.DrawDefault = true;
}
private void button1_Click(object sender, EventArgs e)
{
imgIndex++;
listView1.Invalidate();
}
You would more likely want to get the imageindex from data in the ListViewItem separately for each Row, maybe like this:
//..
e.Graphics.DrawImage(imageList1.Images[Convert.ToInt16( e.SubItem.Text) ], e.Bounds);
//..
As usual error checking is up to you.
Also note the Invalidate, which triggers the Paint and the subsequent DrawSubItem event, necessary after you have changed the imageindex. When the underlying values change, the system will take care of that.

Different objects calling same method, how can I set the "caller's" property?

I have 15 images on my WPF application. I want it so that whenever MouseUp on any of the images is called.. it'll call the same method.
I would like to do something similar to the psuedo code written here.. This would save so much time instead of writing 15 individual methods for each button. How can I do something like this?
private void BluePick1_Image_MouseUp_1(object sender, MouseButtonEventArgs e)
{
sender.ImageSource = something;
}
thank you for any help
if your event is always on a button :
private void ButtonMouseUp(object sender, MouseButtonEventArgs e) {
((Button)sender).ImageSource = something;
}
and
button1.MouseUp += ButtonMouseUp;
button2.MouseUp += ButtonMouseUp;

Changing ListBox value asynchronously

I'm in a situation and I want to know that it is possible or not.
I want to change the Items of ListBox control depend on TextBox value.
Means when I change textbox value, the listitems of ListBox changes asynchronously (without pressing button or something).
Is it possible? If it is possible please guide me some references, tutorials or something.
Not completely getting what you need,but i hope the cases below would help you;
Case 1 : If you have a listbox with several items in it and you want the item to be selected that matches the text in textbox. If this is the case the code below should do the job;
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.TextLength >= 1)
{
int index = listBox1.FindString(textBox1.Text);//Search any items that match Textbox's text.
listBox1.SelectedIndex = index;//Highlight the match
}
else
{
listBox1.SelectedIndex = 0;
}
}
Add the code above to the TextChangedEvent of your textbox and make sure you rename the controls in the code with the ones you have.If this is not the case,see the below one;
Case 2 : You have a textbox and you want to add the text of textbox to listbox.On more thing i would like to tell you,the code below assumes that when you press the Enter Key while the textbox is focused,it's text(if any) should be added to listbox.Add the code below in KeyDownEvent of your textbox,and make sure to rename the controls.If this is the case the code below would help you;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
string item = textBox1.Text;
if (textBox1.Text.Length >= 1)
{
if (e.KeyCode == Keys.Enter)//If Enter key is pressed while textbox is focused.
{
listBox1.Items.Add(item);
}
}
}
Hope this helps you.
I hope the below code will help you:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if(TextBox1.Text == "One")
listBox1.Items.Add("One");
if(TextBox1.Text == "two")
listBox1.Items.Add("two");
}
You don't have to do that async. Just use the TextBox.TextChanged-Event.

C# Listbox Item Double Click Event

I have a list box with some items. Is there anyway I can attach a double click event to each item?
Item 1
Item 2
Item 3
If i was to double click Item 2, a Messagebox saying "Item 2" would pop up
How would i do this?
void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = this.listBox1.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
MessageBox.Show(index.ToString());
}
}
This should work...check
WinForms
Add an event handler for the Control.DoubleClick event for your ListBox, and in that event handler open up a MessageBox displaying the selected item.
E.g.:
private void ListBox1_DoubleClick(object sender, EventArgs e)
{
if (ListBox1.SelectedItem != null)
{
MessageBox.Show(ListBox1.SelectedItem.ToString());
}
}
Where ListBox1 is the name of your ListBox.
Note that you would assign the event handler like this:
ListBox1.DoubleClick += new EventHandler(ListBox1_DoubleClick);
WPF
Pretty much the same as above, but you'd use the MouseDoubleClick event instead:
ListBox1.MouseDoubleClick += new RoutedEventHandler(ListBox1_MouseDoubleClick);
And the event handler:
private void ListBox1_MouseDoubleClick(object sender, RoutedEventArgs e)
{
if (ListBox1.SelectedItem != null)
{
MessageBox.Show(ListBox1.SelectedItem.ToString());
}
}
Edit: Sisya's answer checks to see if the double-click occurred over an item, which would need to be incorporated into this code to fix the issue mentioned in the comments (MessageBox shown if ListBox is double-clicked while an item is selected, but not clicked over an item).
Hope this helps!
I know this question is quite old, but I was looking for a solution to this problem too. The accepted solution is for WinForms not WPF which I think many who come here are looking for.
For anyone looking for a WPF solution, here is a great approach (via Oskar's answer here):
private void myListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DependencyObject obj = (DependencyObject)e.OriginalSource;
while (obj != null && obj != myListBox)
{
if (obj.GetType() == typeof(ListBoxItem))
{
// Do something
break;
}
obj = VisualTreeHelper.GetParent(obj);
}
}
Basically, you walk up the VisualTree until you've either found a parent item that is a ListBoxItem, or you ascend up to the actual ListBox (and therefore did not click a ListBoxItem).
For Winforms
private void listBox1_DoubleClick(object sender, MouseEventArgs e)
{
int index = this.listBox1.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
MessageBox.Show(listBox1.SelectedItem.ToString());
}
}
and
public Form()
{
InitializeComponent();
listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_DoubleClick);
}
that should also, prevent for the event firing if you select an item then click on a blank area.
It depends whether you ListBox object of the System.Windows.Forms.ListBox class, which does have the ListBox.IndexFromPoint() method. But if the ListBox object is from the System.Windows.Control.Listbox class, the answer from #dark-knight (marked as correct answer) does not work.
Im running Win 10 (1903) and current versions of the .NET framework (4.8). This issue should not be version dependant though, only whether your Application is using WPF or Windows Form for the UI.
See also: WPF vs Windows Form
This is very old post but if anyone ran into similar problem and need quick answer:
To capture if a ListBox item is clicked use MouseDown event.
To capture if an item is clicked rather than empty space in list box check if listBox1.IndexFromPoint(new Point(e.X,e.Y))>=0
To capture doubleclick event check if e.Clicks == 2
The post is old but there is a simple solution for those who need it
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > -1)
{
MessageBox.Show(listBox1.Items[listBox1.SelectedIndex].ToString());
}
}

Categories