On listbox double click open new form - c#

Im using windowsForms c# have a listbox which is bound with data items ...So depending on double click of the listbox item i want the corressponding form open...thnx in adv

ListBox has an DoubleClick event. You can access it if you select object, open Events tab in a Properties window.
Double click it and Visual Studio will create an event handler for you like this:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
// here is your code
}
Now, you just need to enter your code.
If you want to open a form with a corresponding item then it will be something like:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
string text = listBox1.Text; // Don't forget to manipulate with it
Form1 form = new Form1();
form.Show();
}

Subscribe to DoubleClick event on ListBox
listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
private void listBox1_DoubleClick(object sender, EventArgs e)
{
// logic here
}
or
listBox1.DoubleClick += (s,e) => { /*logic here */};

I would use the MouseDoubleClick Event, it provides the cursor position in MouseEventArgs so you can easily detect which item got doubleclicked.
void Listbox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = Listbox1.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
object clickedItem = Listbox1.Items[index];
// open your form here
}
}

Related

Edit Items in a ListBox

I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.

How to sync selection index between two ListBoxes

I have two listboxes: listBox1, listBox2.
If i select the item in first listBox1, item of the same index must be automatically selected in listBox2.
So, If i select item 1 in listbox1 then, item 1 selected automatically in listbox2 and so on.
Not: I found some examples but not work.
private void listBoxControl2_SelectedIndexChanged(object sender, EventArgs e)
{ listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex; }
Edit:
I solved it using the selected index code in This answer in SelectedValueChanged Event.
private void listBoxControl2_SelectedValueChanged(object sender, EventArgs e)
{
listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex;
}
Here's a sample that you may want to explore more, try to add ListBoxto your form (in this sample 3 listboxes) it should look like the following:
And here's the source that would select the same index whenever you click on an item on it:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeListBoxes();
}
private void InitializeListBoxes()
{
//Populate listboxes
listBox1.Items.Add("Apple");
listBox1.Items.Add("Orange");
listBox1.Items.Add("Mango");
listBox2.Items.Add("Milk");
listBox2.Items.Add("Cheese");
listBox2.Items.Add("Butter");
listBox3.Items.Add("Coffee");
listBox3.Items.Add("Cream");
listBox3.Items.Add("Sugar");
//Subscribe to same events
listBox1.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox2.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox3.SelectedIndexChanged += listBox_SelectedIndexChanged;
}
void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox listBox = (ListBox)sender;
listBox1.SelectedIndex = listBox.SelectedIndex;
listBox2.SelectedIndex = listBox.SelectedIndex;
listBox3.SelectedIndex = listBox.SelectedIndex;
}
}
What happens is on the InitializeListBoxes you subscribe to the same event which would trigger the SelectedIndexChanged event, and select appropriate item from each of the ListBox.
to solve your problem, you can use a pattern called Observer: https://msdn.microsoft.com/en-us/library/ee850490(v=vs.110).aspx
Basically, you will have to create a notifier method in the listboxes that you want to notify. When you select an item in listBox1, you will call the notifier method of listBox2.
I solved it using the selected index code in This answer in SelectedValueChanged Event.
private void listBoxControl2_SelectedValueChanged(object sender, EventArgs e)
{
listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex;
}
Fastest and easiest way can be via MouseDown event:
private void lstBoxes_MouseDown(object sender, MouseEventArgs e)
{
ListBox lstBox = (ListBox)sender;
lstBx1.SelectedIndex = lstBox.SelectedIndex;
lstBx2.SelectedIndex = lstBox.SelectedIndex;
lstBx3.SelectedIndex = lstBox.SelectedIndex;
}

Open the last used RibbonTab when the wpf RibbonWindow is loaded

How can I return to the last used RibbonTab that was in focus when the window was last closed?
You could create a variable to hold the reference to the RibbonTab and listen to the SelectionChanged event on your Ribbon object.
MyRibbonObj.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
{
RibbonTab rt = ((sender as Ribbon).SelectedItem as RibbonTab);
MyReferenceToRibbonTab = rt;
}
This way you keep track of the latest selected RibbonTab within your Ribbon.
PS: code might needs tweaks. I didn't test it.
Create a setting LastRibbonTab
Save the last tab used in MainWindow_Closed
MainWindow_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.LastRibbonTab = (MyRibbon.SelectedItem as RibbonTab).Header.ToString();
Properties.Settings.Default.Save();
}
Select the last tab in MainWindow_Loaded
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Init();
foreach (RibbonTab r in MyRibbon.Items)
if (r.Header.ToString() == Properties.Settings.Default.LastRibbonTab)
{
MyRibbon.SelectedItem = r;
break;
}
}

How to create a right click event handler in c#

I would please like to know how to display a context menu when you right click on the window.
Here is my code so far :
private void ShowContextMenu_RightClick(object sender, EventArgs e)
{
toolStripMenuItem5.Visible = true;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programmed by D & K");
}
AFAIK there is no direct RightClick event in winforms. You can use the mousedown event to achieve this
private void toolStripButton1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programed by D & K");
}
}
in form cs file you can attach your context menu like this..
public Form1()
{
InitializeComponent();
//Create right click menu..
ContextMenuStrip s = new ContextMenuStrip();
// add one right click menu item named as hello
ToolStripMenuItem hello = new ToolStripMenuItem();
hello.Text = "Hello";
// add the clickevent of hello item
hello.Click += hello_Click;
// add the item in right click menu
s.Items.Add(hello);
// attach the right click menu with form
this.ContextMenuStrip = s;
}
void hello_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Clicked");
}
You should use MouseDown. Then you can get the clicked button with e.Button and the coordinates with e.X and e.Y.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show(e.Button.ToString() + " " + e.X + " " + e.Y);
}
Add a ContextMenuStrip control to the form (note, it does not display on the form but is instead shown at the bottom of the designer).In the form's ContextMenuStrip property, choose the name of the ContextMenuStrip control you just added to the form.That's it. HansPassant stated this in a comment to the question but I think it is being overlooked.
The ContextMenuStrip property is a property of many UI controls that you would use the same technique for.

DoubleClick on a row in ListView

Is there any possibility to get a value of doubleclicked row in ListView?
I registered an event:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems.ToString());
}
But on message, when i doubleclick some row in listview i get:
System.Windows.Forms.ListView+SelectedListViewItemCollection
What is more, I have got 2 columns in listview:
lvLista.Columns.Add("ID");
lvLista.Columns.Add("Tilte");
And i want to show in messagebox the "ID" of doubleclicked row.
How to do it? How to get a values from this event?
If you handle the MouseDown and/or MouseDoubleClick events of the ListView control, and use the HitTest method to determine the target of the mouse action, you will know which item has been double clicked. This is also a good means to determine if NO item was clicked (for example, clicking on the empty area in a partially filled list.
The following code will display the clicked item in a textbox if a single click occurs, and will pop up a message box with the name of the double-clicked item if a double click occurs.
If the click or double click occur in an area of the list view not populated by an item, the text box or message box inform yopu of that fact.
This is a trivial example, and depending on your needs, you will have to mess with it a little.
UPDATE: I added some code which clears the SelectedItems property of the Listview control when an empty area of the list is clicked or double-clicked.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);
listView1.MouseDoubleClick += new MouseEventHandler(listView1_MouseDoubleClick);
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
this.SetupListview();
}
private void SetupListview()
{
ListView lv = this.listView1;
lv.View = View.List;
lv.Items.Add("John Lennon");
lv.Items.Add("Paul McCartney");
lv.Items.Add("George Harrison");
lv.Items.Add("Richard Starkey");
}
void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
MessageBox.Show("The selected Item Name is: " + item.Text);
}
else
{
this.listView1.SelectedItems.Clear();
MessageBox.Show("No Item is selected");
}
}
void listView1_MouseDown(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
this.textBox1.Text = item.Text;
}
else
{
this.listView1.SelectedItems.Clear();
this.textBox1.Text = "No Item is Selected";
}
}
}
Try this:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems[0].SubItems[0].Text);
}
I know this thread is old but nobody here answered the question properly in my opinion. For those in the future, try this, from MSDN:
// User must double-click to activate item
myListView.Activation = System.Windows.Forms.ItemActivation.Standard;
// Add event handler
myListView.ItemActivate += new
System.EventHandler(this.myListView_ItemClick);
Since the accepted answer didn't help me i thought that I would share my solution to the same problem: getting data from a specific column in a listview in the double click event.
The following line returns the data of the second column in the row that I've double clicked on as a string:
private void listViewOutput_DoubleClick(object sender, EventArgs e)
{
string content = listViewOutput.Items[listViewOutput.SelectedIndices[0]].SubItems[1].Text
}
Thanks; this is what I needed. I thought I'd also mention one could set up the local info variable more generally as:
ListViewHitTestInfo info = ((ListView)sender).HitTest(e.X, e.Y);
Try this
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);
Rectangle rowBounds = hit.SubItem.Bounds;
Rectangle labelBounds = hit.Item.GetBounds(ItemBoundsPortion.Label);
int leftMargin = labelBounds.Left - 1;
string x = hit.Item.Text;
}

Categories

Resources