-2

my listbox1 is populated with the names of the lists i have created which when a name is selected populate listbox2 with a list objects from that list that is already created. Then when an item from listbox2 is selected it populates textboxes with the different elements of that object.

I have a delete button and when clicked i want to delete the current object from the list named in listbox1. How do i do that? This is what i've tried.

this does not work. it gives me an error:

CS1061. 'char' does not contain a definition for 'requestDesc' and no extension method 'requestDesc' accepting a first argument of type 'char' could be found (are you missing a using directive or an assembly reference?)

I have everything else working but this.

string selecItem = listBox2.GetItemText(listBox2.SelectedItem);
var find2 = selecItem.FirstOrDefault(x => x.requestDesc == curItem);
if (find2 != null)
{
    selecItem.Remove(find2);
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • `selecItem` is a `string`. Since string implements `IEnumerable`, `x` in `FirstOrDefault` is `char`. Since it's not clear what `curItem` is, it's not really clear what you're trying to achieve. Perhaps you want `var selecItem = listBox2.SelectedItems`? – ProgrammingLlama Jul 22 '19 at 01:45
  • The error you get is pretty clear Your ```selectItem``` is type of ```string``` when you call ```FirstOfDefault``` on string you iterating through individual characters of the string, at lambda expression ```x => x.requestDesc == curItem``` compiler could not find ```requestDesc``` property on ```char``` – Tim Jul 22 '19 at 01:47
  • string curItem = listBox1.GetItemText(listBox1.SelectedItem); to use to search through the list of objects and populate the textboxes – Manuel Ledesma Jul 22 '19 at 01:51
  • `string curItem = listBox1.GetItemText(listBox1.SelectedItem);` returns the _text representation_ of the item. [Docs for GetItemText](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listcontrol.getitemtext?view=netframework-4.8). – ProgrammingLlama Jul 22 '19 at 02:04

2 Answers2

1

Try this with x.SelectedValue and it should compile:

string selecItem = listBox2.GetItemText(listBox2.SelectedItem);
var find2 = selecItem.FirstOrDefault(x => x.SelectedValue == curItem); 
if (find2 != null) { selecItem.Remove(find2); }

However it won't work because the find2 is a Ref type and it won't be equal to the item in listbox2.

You might choose to iterate over the items instead, or even better thanks to Jimi in the comments:

string selecItem = listBox2.GetItemText(listBox2.SelectedItem); 
int itemIdx = listBox1.FindStringExact(selecItem); 
if (itemIdx != ListBox.NoMatches) { selecItem.Items.RemoveAt(itemIdx); }

Ref: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.findstringexact?view=netframework-4.8

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

selecItem is a string. Like "12345abc".

A string does not contain a requestDesc. It just contain chars.

What you need to do is to remove it from the listbox by useing something like listBox1.Items.Remove

You can find more on it here C# removing items from listbox

Stefan
  • 17,448
  • 11
  • 60
  • 79