Click here to Skip to main content
15,867,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a c# program that allows me to select a string (text) from listBox1, a string (text) off listBox2, and then put into listBox3.
I've tried defining listBox 1 as a string, but it says "string does not contain a definition for 'selected item' and no accessible extension method 'selected item' accepting a first argument of type 'string'

How can you use a string in an equation? or can you?

What I have tried:

I submitted what I have tried in the problem part. I hope that it helps!
private void button 1_click
{
string training = listBox1.SelectedItem.ToString();
string city = listBox2.SelectedItem.ToString();
int daysoftraining = 0, registrationfee = 0, total = 0;
{
if ((workshop = listBox1.SelectedItem) && (city = listBox2.SelectedItem));
//then
idk what to put here to put those two list box answers into listBox 3
}
}
Posted
Updated 15-Mar-21 9:46am
v4
Comments
BillWoodruff 13-Mar-21 5:45am    
"I submitted what I have tried in the problem part." Where's the code ?
Member 15098877 13-Mar-21 9:57am    
private void button 1_click
{
string training = listBox1.SelectedItem.ToString();
string city = listBox2.SelectedItem.ToString();
int daysoftraining = 0, registrationfee = 0, total = 0;
{
if ((workshop = listBox1.SelectedItem) && (city = listBox2.SelectedItem));
//then
idk what to put here to put those two list box answers into listBox 3
}
}
Richard MacCutchan 13-Mar-21 11:24am    
You have already extracted the two fields so what is that if statement for? And you may not have noticed that an equals test requires a double ==. And you have not explained how you want the information in the third listbox.
BillWoodruff 14-Mar-21 10:23am    
more confused code. you define 'training and never use it.

The ListBox Class (System.Windows.Forms) | Microsoft Docs[^] is a control, not a string.
 
Share this answer
 
Comments
BillWoodruff 14-Mar-21 10:04am    
Voted #5 to counter unwarranted down-vote.
Richard MacCutchan 14-Mar-21 10:08am    
Thank you.
C#
private void button1_Click(object sender, EventArgs e)
       {

           //I would check if items are selected at all, to avoid errors..

           if (listBox1.SelectedItems.Count >0 && listBox2.SelectedItems.Count > 0 )
           {
               //then
               //idk what to put here to put those two list box answers into listBox 3
               ListViewItem NewItem = new ListViewItem();
               NewItem.Text = listBox1.SelectedItem.ToString() + " - " + listBox2.SelectedItem.ToString();
               listBox3.Items.Add(NewItem);

           }

       }



enjoy your day!



you can send a message to the user, that not enough items have been selected. like so:


if (listBox1.SelectedItems.Count > 0 && listBox2.SelectedItems.Count > 0)
            {
                //DoSomething
            }
            else
            {
                MessageBox.Show("No items selected!");
            }
 
Share this answer
 
v2
Comments
BillWoodruff 14-Mar-21 10:16am    
A ListBox Items collection is just a collection of Type Object ... not ListViewItem ... you can add anything to it ... anything with a built-in, or user-defined ToString method will cause the expected result in the displayed control.

NewItem.Text = listBox1.SelectedItem.ToString() + " - " + listBox2.SelectedItem.ToString();

This is wrong: it would result in a single item being added, not two items

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900