Selecting text from ListView in c# windowsForms - c#

I'm coding in C#, in Windows Forms. I'm using VS 2013.
I have a ListView in detailed mode and it shows my data correctly. I want to select it's items and copy the text of them or show some information about an item when I hover the mouse over it. But I can't do anything with Data which are shown in table. Only the first column can be selected and of course I can't copy those items too. The other columns cannot be selected.
What should I do?
I've searched table in toolbox. But there is only TableLayoutPanel and working with it is harder than ListView. Does VS 2013 have anything better?

One way to control mouse hover is by making a event for mouse hover
this.panel1.MouseHover += new System.EventHandler(this.panel1_MouseHover);
private void panel1_MouseHover(object sender, System.EventArgs e)
{
// Update the mouse event label to indicate the MouseHover event occurred.
label1.Text = sender.GetType().ToString() + ": MouseHover";
// Instead of printing you can do whatever you want here, like you want to select some text or whatever
}
For more details, you can see this link https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
For complete row selection, you can use
listView1.FullRowSelect = true;

Related

How to make DataGridView editing control entry behaviour the same as TextBox?

Is there any way to make entering a DataGridView textbox cell work exactly like entering a TextBox? By default, text can be selected in a TextBox just by clicking and dragging the mouse, even when the control doesn't start with the focus, but in a DGV you seem to have to select the cell before you can vary the selected text.
EDIT: I am trying to make the DataGridView behave the same as tables in MS Access where you can dynamically select text in a cell which doesn't already have the focus with a single click and drag. (I notice that in Access the text shows as selected before the cell receives focus, so is not quite the same as I am asking above, but would also be acceptable.)
Once the cell editing control has focus, text selection works fine, but I can't seem to make it happen when entering the cell in the first place.
I am currently using programmatical editing using DataGridView1.BeginEdit(False) to show the editing control, but the only options here are False (nothing selected) or True (all text selected). Edit: Using EditOnEnter causes the whole cell text to be selected and another mouse click is needed to dynamically select just a portion of the cell contents.
Surely behaviour like this ought to be possible since DataGridViewTextBoxEditingControl inherits TextBox anyway?
I don't think what you're asking is possible, here is something very close.
private void OnCellMouseClick(object sender, DataGridViewCellMouseEventArgs cellMouseEventArgs)
{
if (cellMouseEventArgs.RowIndex < 0 || cellMouseEventArgs.ColumnIndex < 0)
{
return;
}
var cellRef = _dataGridView[cellMouseEventArgs.ColumnIndex, cellMouseEventArgs.RowIndex];
if (cellRef == null || cellRef.IsInEditMode || cellRef.ReadOnly)
{
return;
}
_dataGridView.BeginEdit(false);
if (!(cellRef is DataGridViewComboBoxCell))
{
return;
}
var editingControl = DataGridView.EditingControl as DataGridViewComboBoxEditingControl;
if (editingControl != null)
{
editingControl.DroppedDown = true;
}
}
And subscribe like normal of course _dataGridView.CellMouseClick += OnCellMouseClick;
The behaviour this produces is such that when you click editing starts, this also has an improvement for combo boxes which usually take 2 or 3 clicks to open, this does it in one. You cannot select text immediately, you must click once still. I have experimented with MouseEnter and similar and cannot produce exactly what you are asking for unfortunately. if anyone else has a complete solution please do submit it because I would also like it!

ComboBox Selected Value to be Blank

Uses: VS 2012;
I've a combobox attached to a datasource in my form. And things work fine. When I run the form, again everything works fine; I can select an item in the dropdown list and it updates to the datasource as well. My problem comes when I need to deselect/revert what I have selected after I saved or Remove what I have select (basically should go as null for that field value).
Our legacy system was built in Delphi 3 & 5, and users got a feature of right-clicking on the dropdown list and get a small popup like button named
Blank
which blanks what have been selected. I could not find anything that will do the same what ever user have selected in .NET's combo box.
You can add a new item in dropdown named -Select-( or something similar name) by using following code:
drp.DataSource = dataSet;
drp.DataBind();
// do it after binding
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
If you are binding in xaml then on page_load event you can write only this line
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
Now if user want to deselect choice, he/she will simply select -Select- item.
Whilst thanking all of your Answers and suggestions, I used #V4Vendetta's idea and composed my solution.
Similarly to deleting a record in a datagridview where you click Delete key, I took the same concept and associated my solution with Delete Key.
What I did was created a handler for ComboBox's Keypress Event like:
private void comboBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
(sender as ComboBox).SelectedIndex = -1;
}
}
And linked to every ComboBox's available
ComboBox1.KeyDown += new KeyEventHandler(comboBox_KeyPress);
ComboBox2.KeyDown += new KeyEventHandler(comboBox_KeyPress);
Now when user clicks Delete key while the ComboBox selected/active, it gets blank.

Select items from Listbox, one by one

I am working on Microsoft Visual C# 2010 Express. Actually, when I execute my code, I get a listbox containing N items. There is a list of items with their size in my database. I am using Microsoft Access 2007 database. I want to display the total size of items present in listbox into a label below the listbox. I have my code for displaying the size. I just want to know how to select already present data in a listbox without any button click. I think loop will be used.
Do what you want to do but i don't understand why you need to select an item form listbox. but you can get the items by
listBox1.Items[i] and then can calculate the size.
Use below code to do that what you wanted.
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.SelectedItem = listBox1.Items[i];
//Calculate the total size of items present in listbox here
}
I just want to know how to select already present data in a listbox without any button click.
I think the MouseDoubleClick event is what you want to handle. To add the handler for that, in the designer, in the properties window, click on the little yellow lightning bolt icon. This will show a list of events. Double-Click on the one you want to handle and the system will create the stub for it and add the handler to the control. A loop in side here will allow you get what you need:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
foreach (string item in listBox1.Items)
{
//Pass item to your size routine here
}
}

Using a ListBox locks out the other controls on the form

I have a form in a Windows application that contains several controls: radio buttons, command buttons, text boxes, a datagridview, and a ListBox.
The dataGridView is bound to a datatable and the ListBox is bound to another datatable. I use the radio buttons and the ListBox to filter the contents of the dataGridView.
If I click on any of the radio buttons, the data in the Grid changes accordingly. I can also click on the other controls and they respond as well.
I can also scroll through the ListBox without any problems. But once I click on an item in the ListBox, the data grid is updated and continues to update with each item selected from the ListBox. But all other controls are inactive, as if they are locked up.
What causes this and how do I fix it?
Here is the code:
private void lstColor_SelectedIndexChanged(object sender, EventArgs e)
{
viewLocalMatsALLBindingSource.Filter = "GRM_COLOR_DESCR = '" + lstColor.Text + "'";
}
private void rdbLogomat_CheckedChanged(object sender, EventArgs e)
{
if (rdbLogomat.Checked)
{
viewLocalMatsALLBindingSource.Filter = "Description LIKE '%LOGO%' OR Description LIKE '%LO-GO%'";
}
}
The radio event works just fine and I can use the other controls on the form. But the ListBox event causes all the other controls to freeze, even the red 'X' close button on the window. However, after the selecting an item in the ListBox, I can select cells and rows in the datagrid. But if I try typing anything in a cell, the ListBox selected item changes based on the characters I type. (The datagrid is read only so editing cells is meaningless.) So apparently the Listbox is maintaining focus but I can't figure out how to lose the focus. Another thing is that I can sort on the columns in the datagrid until I select an item from the ListBox and then that function goes away.

How to show tool tip when cursor is on ListView cell?

I have listview object, and several columns in it. One of the columns does not fit all text info, and the text has been cutted. I need to implement something like this: when user move mouse cursor to this column's cell, all the text is shown. Is it possible to do that ?
Now i have:
protected ListViewItem GetItem(ListView listView, Point mousePosition)
{
Point localPoint = listView.PointToClient(mousePosition);
return listView.GetItemAt(localPoint.X, localPoint.Y);
}
private void myListView_MouseMove(object sender, MouseEventArgs e)
{
ListViewItem item = GetItem(myListView, Cursor.Position);
// or should I use e.Location instead of Cursor.Position?
item.ToolTipText = "my info"//Now I need to show "my info" on the cell
//that user move cursor on
}
One of the old ways to show decent tooltip with lists is to hide a label and make it visible at the desired location, with the desired text for the desired duration. Benefit of this could be multi-line, multi-color tip.
So it works like this:
the label sits on the listview or form and it is invisible.
on mouse move you get item data using mouse X and Y. Position,
size and show your label filled with data.
label itself can have a timer, which will start when label
is visible and make it invisible in 5 seconds, for example.
I remember doing just that this year. Although my "tooltip" was done over the listbox.
Replace label with a textbox and suddenly you can make your data in the cell editable :o)
Try this code
ListViewItem iListView = new ListViewItem("add");
iListView.ToolTipText = "Add Expanded";
myListView.Items.Add(iListView);
Do this by setting the ShowItemToolTips property of ListView to true

Categories