Trapping of Double Entry in ListViewItem in C# - c#

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.
}

Related

C# - Menustrip- Check if Parent exists and get the reference to them

First of all Thanks for your Time! I hope you can help me =/
I have a MenuStrip where i want to add items dynamically.
What i want to do :
If a Partent with exactly the same name already exists the Childs should be add to this parent instead creating a new Parent(MenuStripItem) with the same name.
My Code does currently checks if the parent already exists (which works fine) but the problem is i cant get the reference to this parent -> firstItem=var doesnt work -> cant convert ToolStripItem to ToolStripMenuItem... and changing the "firstItem" to ToolStripItem gives me an Error because i cant use "firstItem.DropDownItems.Add(withChild);" anymore to add a Child later on...
private void AddNewMenuStrips(string [,] NewMenuStripInfo)
{
ToolStripMenuItem firstItem;
bool alreadyexists = false;
string someItem = "Settings"; // the parent im looking for
var items = menuStrip2.Items.Find(someItem+"ToolStripMenuItem",false); //here it checks if parent already exists. Which Works but i cant get the reference of the parent to "firstItem"
foreach (var item in items)
{
MessageBox.Show("FOUND"+item.Name);
firstItem = var; // ERROR cant convert ToolStripItem to ToolStripMenuItem
alreadyexists=true;
}
if (alreadyexists == false) { firstItem = new ToolStripMenuItem(someItem); }
}
THANKS in advance!
ToolStripMenuItem is a class which represents top level menu items and derives (not directly) from ToolStripItem.
Therefore to retrieve the parent menu item you can use a cast:
foreach (var item in parents)
{
MessageBox.Show("FOUND" + item.Name);
firstItem = item as ToolStripMenuItem;
alreadyexists = true;
// break;
}

ListView.Items.Add adding a ListViewItem to wrong ListView

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

C# ListView from Another Function

Please forgive me for such a stupid question. I am sure many of you will find this easy, where I have sent almost half the day reading trying to figure this out.
Here is the problem:
I have a FORM (Form1.cs) made. In that form I created a listview, and named it "ListView1".
Within the Form1.cs, I call a function called FileManager(this), where I pass in the THIS object.
In FileManager.cs I was able to listviewArray= originalForm.Controls.Find("listView1", true) and find that 'listview'.
When I do a listviewArray[0]<-- I can't seem to add a list to it.
FileManager.cs
FileManager(object sender)
{
if (sender != null)
{
originalForm = (Form)sender;
}
}
public void getFiles()
{
filePaths = Directory.GetFiles(hsocDir);
if(filePaths != null)
{
listviewArray= originalForm.Controls.Find("listView1", true);
if(listviewArray != null)
{
ListViewItem lvi = new ListViewItem("text");
// My Array is listViewArray
// How to add things to Lvi to it.
}
}
== Form1.cs
public Form1()
{
InitializeComponent(`enter code here`);
mysql = new MySQLCheck(this);
fileManager = new FileManager(this);
fileManager.getFiles();
}
You can't access element 0 of the collection because the collection is empty. To add an item, use:
listViewArray.Items.Add(lvi);
You need to modify the Items collection instead of the ListView itself for this to work, as ListView is not a collection (its a control).
listViewArray.Items.Add(lvi);
Also in your listview,setting this properties will help :
// Set the view to show details.
listViewArray.View = View.Details;
// Select the item and subitems when selection is made.
listViewArray.FullRowSelect = true;
// Display grid lines.
listViewArray.GridLines = true;

Simple ListBoxItem Issue, not sure how to write this into the code correctly

lblSelected.Text = string.Empty; // empty the label that says you already have an item
foreach (GridViewRow row in gvSnacktastic.Rows) // check all the items in the grid view
{
CheckBox checkItOut = row.Cells[0].Controls[0] as CheckBox; // get the checkbox
if (checkItOut != null && checkItOut.Checked) // if the checkbox exists and is checked
{
bool storeVariable = true; // store a variable that tells us if we need to add it to the list
**foreach(ListBoxItem listItem in lbSelected.Items)** // Loop through list box items to see if we already have it. i haven't used listbox in a long time, this might be slightly wrong
{
// I'm not sure if it's .Text - compare the text of the listbox item to the checkbox item description
if(listItem.Text== checkItOut.Text)
{
lblSelected.Text = "The Item " + listItem.Text + " has already been added."; // make our already have it label
storeVariable = false; // remember that we don't need to add this item
}
}
if(storeVariable) // if we do need to add this item
{
lbSelected.Items.Add(checkItOut.Text); // create a new list box item with the check box item's description - this code is not complete
}
}
}
}
The highlighted (**) area is reading with an error as I am debugging. Any idea what I'm doing wrong?
I'm sure this is a simple question to answer, but I am not finding the right information online.
Please Change
foreach(ListBoxItem listItem in lbSelected.Items)
{
}
TO
foreach(ListItem listItem in lbSelected.Items)
{
}

How do you disable a WPF ListViewItem in C# code?

I'm used to the old Winforms way of doing things. Apparently WPF ListViews are full of... XmlElements? How would I do something like disabling a ListViewItem?
foreach (XmlElement item in this.lvwSourceFiles.Items)
{
//disable?
}
ListView is an ItemsControl. ItemsControl.Items does not return the child controls - it returns the items - that is, objects that you have added to the ListView, either directly, or via data binding. I guess in this case you have bound your ListView to some XML, right?
ListViewItem (and other classes like it - e.g. ListBoxItem for ListBox) is called an "item container". To retrieve an item container for a given item, you should do this:
ListView lv;
...
foreach (object item in lv.Items)
{
ListViewItem lvi = (ListViewItem)lv.ItemContainerGenerator.ContainerFromItem(item);
}
You need to access the ListViewItem that represents the data item. You can achieve that through the ItemContainerGenerator
foreach (object item in this.lvwSourceFiles.Items)
{
UIElement ui = lvwSourceFiles.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
if (ui != null)
ui.IsEnabled = false;
}
You can Perform that In XAML Easily

Categories

Resources