Resolved Listbox listing items like: ListViewItem: {myname}

Jens Boedt

Member
Joined
Dec 2, 2020
Messages
7
Programming Experience
Beginner
Hello,

Why is my ListBox listing my items like:
ListViewItem: {myname}
instead of:
myname
 
Solution
What type does a listbox item expect to be passed to it?

An object, right?

So why would you expect it to be displayed any other way when you are passing in a ListViewItem and not calling .Text on that items property to add the value as text?
I am referring to line 15 of your code. And in future, post your code on the forums wrapped in appropriate code tags. Otherwise, you force us to rewrite your code from scratch. So try harder to make it easier for us to help you by doing so.
In which gevalText = "D13 - P16", "D15 - P28"
So i want it to show: "D13 - P16", but it shows: "ListViewItem: {D13 - P16}"
 

Attachments

  • 2020_12_02_13_20_09_BC_Controleer_Galva_Microsoft_Visual_Studio.png
    2020_12_02_13_20_09_BC_Controleer_Galva_Microsoft_Visual_Studio.png
    24.8 KB · Views: 26
  • 2020_12_02_13_22_04_Controleer_galvagaten.png
    2020_12_02_13_22_04_Controleer_galvagaten.png
    2.3 KB · Views: 24
[CODE=csharp] YOUR CODE HERE [/CODE]

I don't do screenshots. POST CODE AS TEXT. And put it in code tags as I have shown.

I also suggest you use the debugger tutorial in my signature, and step through line by line to identify your problem. You might be only a beginner, but come on, are you really asking why your UI is displaying as demonstrated after posting that screenshot above...
 
What type does a listbox item expect to be passed to it?

An object, right?

So why would you expect it to be displayed any other way when you are passing in a ListViewItem and not calling .Text on that items property to add the value as text?
I am referring to line 15 of your code. And in future, post your code on the forums wrapped in appropriate code tags. Otherwise, you force us to rewrite your code from scratch. So try harder to make it easier for us to help you by doing so.
 
Solution
Pseudo example :
C#:
        private void button2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem li in listView1.Items)
                listBox1.Items.Add(li.Text);
        }
 
ListViewItemsCollection.Add() has multiple overloads. There is the version that takes a string, and there is a version that takes a ListViewItem. In this case, it seems like all the user wants to do is to display a string, so they should use the version that just takes a string.

 
I'm sorry, i'm used to posting screenshots from another forum (there it is a custom).

It fills actually 2 ListBox.
- gevallenLijst
- kokerLijst

kokerLijst is only used to get second tags to link to my first ListBox. This one is not visible.
The first ListBox: gevallenLijst is the one visible in the screenshot, showing the wrong text.
But here the method .Text was used...
 
Why are you trying to stuff a ListViewItem into a ListBox? ListViewItems go into ListViews, not ListBoxes.

Moving this to WinForms rather than general .NET Framework.
 
kokerLijst is only used to get second tags to link to my first ListBox. This one is not visible.
Explain further why you need a second hidden ListBox. My gut feel is that you don't really need this. What problem are you trying to solve?
 
Lastly, your second "hidden" control, could be replaced with a List<T> instead of jamming up your UI with unneeded controls.
 
Explain further why you need a second hidden ListBox. My gut feel is that you don't really need this. What problem are you trying to solve?

I'm working in a program to draw called Tekla.
I'm checking connections between a tube and a plate.
The modelobjects Plate and Tube need to be in my List to use later.
That's why i use a hidden listbox.
I've done something else like this too, and it worked like i wanted. So it's strange that it does this thing right now.
 
I just found this on google:
A ListView is basically like a ListBox (and inherits from it), but it also has a View property. This property allows you to specify a predefined way of displaying the items. The only predefined view in the BCL (Base Class Library) is GridView, but you can easily create your own.

This solved my problem. Had to change ListBox to ListView to get the names like i entered them.
 
Back
Top Bottom