How to copy individual subitem from ListView? - c#

I have no idea how to do this.
What I am trying to do is to allow user to copy certain field by using ether right click mouse or keyboard shortcut.
I need this because I am storing some fields as a code which cannot be retyped easily.
ListView allow to select only individual row at the time, and I want to select only one field of the whole table layout.
How do I do this?
PS. Or, how do I allow user to modify the content of each field. At least when they can modify the field they can copy/paste the content (changes will not be saved to my database file).

To allow the user to edit the item, you can set LabelEdit on the ListView to true. Here is the description from MSDN:
"When the LabelEdit property is set to true, the user is able to modify the text of an item by clicking the item text to select it and then clicking the item text again to put the label text into edit mode. The user can then modify or replace the item's text label."
Note that this does not apply to the subitems.
Copying the data out could be slightly more involved depending on the user interface that you desire (i.e. button or context menu). The easy solution would be to add a button to the form that, when pressed, would copy the content of the selected item (or any of its subitems) to the clipboard.
private void button1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count != 0)
{
Clipboard.SetText(listView1.SelectedItems[0].Text);
}
}

There is some "Clipboard" class
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx
You could simply make a button or something, and set the selected item text in the clipboard.

Related

Strange Bahavior when using RowEnter Event in DataGridView

I have a datagridview, a list with the same content as the datagridview and a textbox, when I change the row, the content of the textbox changes according to the row that I have selected (they are not linked through databind, but I change the content manually). Up to here everything works fine.
Now, I need that, if I modify the content of the textbox, and I change the row selected in the datagridview, before changing the row selected, check if the content of the textbox has been modified, and if it has, ask if I want to save the changes in the list.
For this task, I thought of using the RowEnter event of the datagridview, comparing the content of the textbox with the content of the list, but I ran into a difficulty, when I put a MessageBox inside the rowenter event, to ask if I want to save or not, no matter if I answered yes or no, the row of the datagridview does not change, and I want it to change the row right after asking, whether or not I saved the textbox change, or if there is another way to check if I change the content of the textbox with respect to the list, after changing the row in the datagridview
I leave part of the code that I am using.
private void dgvVisitas_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (numeroFilaVisitaActual == e.RowIndex)
{
if (txtNotasVisita.Text != listaVisitasPaciente[e.RowIndex].Notas)
{
MessageBox.Show("No es igual"); // If this Message is show, the row don't change
}
}
numeroFilaVisitaActual = e.RowIndex;
}

How to change an image in a pictureBox when a List item is chosen in a dataGrid [C#] (Visual Studio)

I am working to create something akin to a "Pokedex" in Visual Studio 2012/13 (It basically is a Pokedex at the moment). It is a WinForm program, and so far I have a data grid that is listing off the "Pokemon's" list number, its name, and type. What I want to do next is have a pictureBox element show the image of the Pokemon (extracted from a list of image links) when that specific dataGrid list item is chosen (So if #4, Charmander, is chosen in the dataGrid, then I want it to show a picture of Charmander in the pictureBox).
This here is an example of the visual design of the project so far:
The empty space under the richTextBox is where the pictureBox is. Later on the Pokemon's description will also be included in that box when the list item is chosen, but I believe that will be easy to implement once I understand how to call the image at selection.
All and any help is appreciated! I am not including code for the project right now as I do not know what is helpful or not. Do let me know what is needed for a better understanding of the project.
You could add ImageList control to your form and add your Pokemon pictures to your ImageList. You can easily do that using the designer Images Collection property of the ImageList.
In addition, make sure to set your image indices as your Pokemons in your DataGridView (so the first image in the collection is Bulbassur, second is Ivysaur etc.) - this will later help you easy access a picture by its index.
Next step is to attach CellEnter event to your DataGridView. It will fire when a cell is focused.
Add this code to the event handler:
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
pictureBox1.Image = imageList1.Images[e.RowIndex];
}
}
Note that this will only change the image if you select any of the first column (DexNum) cells. You could change it to whatever you like, change the condition.
I have not made any validation, you should do so, but I'm assuming that the number of rows is equal to the number of images.

How To Hide ListView ColumnHeader?

I am struggling to figure out the correct control to use for a list of predefined jobs in the included form. I currently have a ListBoxControl in the Predefined Job Name group that lists all of the predefined jobs for a marine service shop (i.e. oil change, tune up, etc...). Then, based on the item (i.e. job name) that is selected in my ListBox, I need to display the items that correspond to that job. For example, if oil change is the selected job I need to show 4 quarts oil, 1 oil filter, labor, etc...and so on.
Currently, when I load the form data I have a DAO that retrieves all of my jobs from the database using LINQ to SQL. Then I iterate over the results and put the job names into the ListBox. The problem that I am having is that there is no tag for ListBox items like there is for ListView items. So each time the user selects another item in the ListBox, I have to perform another LINQ query to get the job from the database again so that I can display its' corresponding items. If I could use a ListView and hide the column header I could set the entire job on the tag so that each time the user selects a new item I would have access to the details without having to make another call to the database. Is there a way that I can hide the column header of a ListView without hiding the entire column?
You can set the HeaderStyle member of the ListView to None.
listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
Checkout the ListView HeaderStyle property. It has the following options:
None
Nonclickable
Clickable
From MSDN:
The HeaderStyle property allows you to specify whether the column headers are visible or, if they are visible, whether they will function as clickable buttons. If the HeaderStyle property is set to ColumnHeaderStyle.None, the column headers are not displayed, although the items and subitems of the ListView control are still arranged in columns
You can also create simple object like ListItem which has two poperties: Text (string) and Tag (object). Then implement ListItem.ToString() and you can use these in the ListBox as well.
You can also check out Better ListView Express component, which is free and allows displaying items in Details view without columns. The advantage over ListBox and ListView is a native look and many extra features.
Easy way is using the ColumnWidthChanging event
private void listViewExtended1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Cancel = true;
e.NewWidth = listViewExtended1.Columns[e.ColumnIndex].Width;
}
}
I found that if you know for a fact you are not displaying the headers it may be best to set the HeaderStyle property to None, as Rajesh mentions above.
When setting in the .CS when screen initially loads the headers are displayed until screen is fully rendered.

Combobox population problem

So I have a combobox - the designer code:
this.cmbStatusBox.Items.AddRange(new object[] {
"Ordered",
"Cooking",
"In-transit",
"Delivered"});
The formload code:
if (mainForm.boolEdit == true)
{
this.cmbStatusBox.Items.AddRange(new object[] {
"Cooking",
"In-transit",
"Delivered"});
}
else
{
this.cmbStatusBox.Items.AddRange(new object[] {
"Ordered"});
}
As you can see, I am trying to make the combobox have different values.
As things stand, i get both whats in the designer and in formload in the comboboxes.
How can i stop this?
I also have an edit function, so when i edit a record, i want the combo box to be populated by what is already saved.
Just a random question, can you stop the user entering a value that isn't in the combo box?
Thankyou
If you want to replace the current contents then you'll need to call
this.cmbStatusBox.Items.Clear();
before adding your new values.
ComboBox MSDN page
ComboBox.Items MSDN page
The DropDownStyle property also specifies whether the text portion can be edited.
Source
The values are:
Simple Specifies that the list is always visible and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.
DropDown Specifies that the list is displayed by clicking the down arrow and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list. When using this setting, the Append value of AutoCompleteMode works the same as the SuggestAppend value. This is the default style.
DropDownList Specifies that the list is displayed by clicking the down arrow and that the text portion is not editable. This means that the user cannot enter a new value. Only values already in the list can be selected. The list displays only if AutoCompleteMode is Suggest or SuggestAppend.
Source
Just remove the items from the designer code.

.Net C# windows forms, listbox control question

I have a pretty simple form with a listbox, a text box, and two buttons.
The list box items are populated from a sql database table. The user may chose to select one or multiple items from the listbox.
The text box is used to write more details about the items in the listbox. One button can then be clicked to update another database table with these details.
I want to make it where if any items are selected from the listbox, those contents are automatically copied into the text box field on the fly as they are selected.
Is this possible?
I've been able to make this happen on the button click event - just not on the fly as they are selected. I want it to occur before the additional details are being sent to the database
I've also tried using several different listbox events, but have been unable to obtain the results I am looking for.
Any suggestions?
try this out
you will have to handle the SelectedIndexChanged event on the listbox.
here is an example with example controls.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = "";
foreach (string nextitem in listBox1.SelectedItems)
{
textBox1.Text += nextitem + " ";
}
}
im not too sure HOW you want the text to appear in the textbox so that would be up to you in the foreach loop.
yes, the SelectedIndexChanged event fires on every selection change, and you can concatenate together the items in the listbox. But if you are talking the description that's not visible too, you need to store the description in each listboxitem tag property, and in your code retrieve the description from there.

Categories

Resources