I have two ListView objects in my code, MovieList and UserFriendList. I have a method that looks like this
public void passFriends(ListViewItem[] friends)
{
foreach (ListViewItem tempItem in friends)
{
ListViewItem temp = new ListViewItem();
temp = (ListViewItem)tempItem.Clone();
UserFriendList.Items.Add(temp);
}
}
The method is designed to pass an array of ListViewItems from one form to another, and then add each listview item to the UserFriendList. However, when I actually perform the action, the ListViewItem (a User) is added to my MovieList instead of my UserFriendsList
Does anybody know why this could be happening?
This question can be closed. I found the issue with my problem. A section of legacy code that was used to demonstrate list switching wasn't removed and it was changing the listview of UserFriendList
Related
I have searched around but could not find any references.
How do I delete an item in a generic list that relates to items in a listbox?
I currently have a public static List<Employees> and a listbox named lstRecords, I can remove the item in the listbox just fine, but either everything is removed from the list or nothing at all.
This was my first set of code I was working with:
private void DeleteRecord()
{
if (lstRecords.Items.Count > 0)
{
for (int i = 0; i < lstRecords.Items.Count; i++)
{
if (lstRecords.GetSelected(i) == true)
{
Employees employeeRecord = lstRecords.SelectedItem as Employees;
employee.Remove(employeeRecord);
}
}
lstRecords.Items.Remove(lstRecords.SelectedItem);
}
}
}
This is my 2nd set of code I was working with, I have my List right under partial class, but this is all contained in a method.
private void DeleteRecord()
{
ListBox lstRecords = new ListBox();
List<object> employee = new List<object>();
employee.RemoveAt(lstRecords.SelectedIndex);
lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}
So far I haven't gotten either set of code to work the way I would like it to, I'm obviously doing something wrong.
I have a few other blocks of code I played around with but these seemed to be headed in the right direction.
Eventually I'll need to be able to double click an item in the list to pull up the properties menu.
Your code runs fine you just have to make some small changes.
The first code block is Ok however I dont know where your lstRecords are.
But have a look at this just copy the code and run it after you have some records in your employee object.
It's createing a listbox in code then adds it to the form(Winforms) and having the lstRecords globaly.
ListBox lstRecords;
private void IntializeDemoListbox()
{
lstRecords = new ListBox();
this.Controls.Add(lstRecords);
foreach (var item in employee)
{
lstRecords.Items.Add(item);
}
}
And then you will be able to use your first set of code the other set will be like this.
private void DeleteRecord()
{
employee.RemoveAt(lstRecords.SelectedIndex);
lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}
What you want to do is bind your ListBox to you List of employees. This post shows the binding and the comments shows the removing code as well. The idea is that when you remove an item from the DataSource, then you won't see it in the ListBox.
Binding Listbox to List<object>
The problem with the DeleteRecord() method is that the lstRecords object you just created isn't the ListBox that is on the form.
I tried to manage on how to prevent double entries in ListView Item in C#. All of them didn't work for me.
I try to based the source code of Ahmad Mageed and I was confused of his trappings.
I based his source code to my project
ListViewItem item = ListView1.FindItemWithText(txtPLU.Text);
if (item != null)
{
MessageBox.Show("Item is already been exist!"); //Result if the item has exist in the listview item.
}
else
{
addToList(); //Its a method to add the product items in the ListViewItem.
txtBoxPLU.Focus();
}
The Behaviour of the runtime is that it only add an item.
Sorry if this is kinda confusing for all of you. I just to trap if the item is already exist in the listview item.
The ListView.ListViewItemCollection has 2 methods that can be used to find if an item is contained in the collection.
The Contains and ContainsKey method.
Example:
ListViewItem _item = new ListViewItem();
if (!listView1.Items.Contains(_item))
{
// TODO: Add to list.
}
else
{
// Already exists.
}
I need to access a listview's items in a DoWork event handler. For this the delegate and the method to invoke the listview what I wrote is:
delegate ListView itemDelegate(ListView bufferedListView1);
private ListView getItems(ListView bufferedListView1)
{
if (bufferedListView1.InvokeRequired)
{
// BeginInvoke(new itemDelegate(getItems));
bufferedListView1.Invoke(new itemDelegate(getItems));
}
else
{
return bufferedListView1;
}
}
This is the first time I am working with invoking a control. So please let me know where I am wrong. One error that I get is gsm_modem.Form1.getItems(System.Windows.Forms.ListView): not all code paths return a value. I even guess that what I wrote might be wrong. Correction please..
You can do something like this
First create shared variable in global scope for your form.
List<string> listItems;
Now before calling RunWorkerAsync do following
listItems = new List<string>();
foreach (ListViewItem item in bufferedListView1.Items)
{
//If you want to add tag to list then you can use dictionary like Dictionary<string, object) listItems; and then add items as listItems.Add(item.Text, item.Tag); It only works if text is unique.
listItems.Add(item.Text);
}
bgw1.RunWorkerAsync();
Now read the list inside background worker using foreach.
Thanks to #Ravi patel for the idea. This is what I did to solve the problem:
ListView listItems = new ListView();\\In global scope
foreach (ListViewItem item in bufferedListView1.Items)
{
listItems.Items.Add((ListViewItem)item.Clone()); // Copied the bufferedListview's items that are to be accessed in other thread to another listview- listItems
}
Then used listItems in my other thread easily.
I've a common ContextMenuStrip for every workspace control of my application.
This ContextMenuStrip contains 4 Items ("Move front", "Move back", and "Delete control").
Now I want to extend it for one control.
There's a DataGridView on this control and I want an additional item to delete the selected DataGridViewRow.
This is the code I tried:
private void extendContextMenuOfDataGridViewRow (DataGridViewRow row) {
ContextMenuStrip ctx = new ContextMenuStrip();
foreach (ToolStripMenuItem item in this.ContextMenuStrip.Items) {
ctx.Items.Add(item);
}
ctx.Items.Add(new ToolStripSeparator());
ToolStripMenuItem ctxDeleteRow = new ToolStripMenuItem("Delete row");
ctxDeleteRow.Name = "ctxDeleteRow";
ctxDeleteRow.Click += new EventHandler(ctxDeleteRow_Click);
ctx.Items.Add(ctxDeleteRow);
row.ContextMenuStrip = ctx;
}
After the first item of the foreach loop was added to ctx.Items the debugger leaves the whole method and the first item is missing at the common ContextMenuStrip.
How do I do that right?
If you want to extend functionally of some control, you can either
a) create an extension method
public void DoSomething(this MyExtendedControl mec, DataGridViewRow row)
{
}
b) create a new class inheriting from your unsatisfactory control (or even create a completely new control), when you can override/add things as needed
Depends on your specific needs, couldn't understand from your description...
I haven't worked with WinForms for ages, but are you sure that you can keep the same Row object assigned to two different Strips at once?
foreach (ToolStripMenuItem item in this.ContextMenuStrip.Items) {
ctx.Items.Add(item);
}
I seriously doubt this should work by design because a row has to “talk” to its parent, and by adding it to another strip I'm afraid you're re-assigning the parent.
Instead, I would have added an item to the common menu but with its Visible property to false.
Then I would catch the menu opening event and make item visible if target is a DataGridViewRow.
I am dynamically creating ListViewItem descendants:
class Application : ListViewItem {
. . .
class LegacyApplication : Application {
I store these first in a List of obects:
private List<object> legacyApps = new List<object>();
..this way:
if (ldbc.appIsLegacy(sPathOfExe)) {
legacyApp = new LegacyApplication(sApp, sTitle, sDesc, sIconFileName, sPathOfExe, appCategoriesForCurrentApp);
}
legacyApps.Add(legacyApp);
...and then I add them to the ListView on the main form this way:
foreach (LegacyApplication LegApp in legacyApps) {
this.listApplications.Items.Add(LegApp);
}
...but the ListView does not display them. It displays the ListView Groups I've created (and each ListViewItem is assigned to one of those groups), but not the ListViewItems themselves...
Updated with requested info:
The constructor for the ListViewItem descendant looks like so:
public LegacyApplication(String AAppName, String ATitle, String ADesc, String AIconFileName, String APathOfExe, List<String> ACategories) {
base.Name = String.Format("legapplvi{0}", AAppName);
base.Text = ATitle; // "Title" is a short description - between exe name and Description
base.ToolTipText = ADesc;
base.EnsureVisible();
// "base" above means ListViewItem; "base" below refers to our Application class*
base.Categories = ACategories;
base.ExePath = APathOfExe;
base.IconFileName = AIconFileName;
}
which adds the following properties to ListViewItem:
public string ExePath
public string IconFileName
public string Category
public List Categories
LegacyApplication adds no further properties to (our) Application class.
I'm not sure what the respondent below means by "subitems" - the ListViewItems are subitems of the ListView Groups, perhaps...?
Updated with unrequested info (TMI?):
OK, I'm thinking I can add columns this way, once all the Groups are assigned to the ListView:
for(var item in listApplications.Groups) {
listApplications.Columns.Add(item)
}
...but now, how do I add specific ListViewItems to particular columns?
Updated after getting it to (sort of) work:
Commenting out this:
listApplications.View = View.Details;
...gets the Items to show. HOWEVER, the Text is truncated, which caused me to pose another question here:
I need to display the entire Text of my ListViewItems (not truncated)
Did you try to create a ListViewItem first and then add into the List instead of using
this.listApplications.Items.Add(LegApp);
Can you provide more details with a code snippet