Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i can display one item when i select it
but i can't display all items at once.

What I have tried:

i tried this
form1
C#
private void btn_voit_Click(object sender, EventArgs e)
        {
            string textboxchoix = listBox2.Text;           
            Form2 frm = new Form2(textboxchoix);
            frm.Show();
        }

form2
C#
public Form2(string textboxchoix)
        {
            InitializeComponent();
           label_choix.Text = textboxchoix;
            
        }
Posted
Updated 10-Jan-17 5:38am
v3

Add a method to Form2
C#
public void AddString(string s)
   {
   lable_choix.Text += s;
   }
And call that for each additional item.

Quote:
i know how to display textbox in label but if i select it will display and it only display one item for exmaple i have 10 element n my textbox but when i display it it shows only one i want display all element


That';s not a TextBox - it's a ListBox.
Try this:
StringBuilder sb = new StringBuilder();
foreach (object x in listBox1.Items)
    {
    sb.AppendLine(x.ToString());
    }
Form2 frm = new Form2(sb.ToString());
frm.Show();
 
Share this answer
 
v2
Comments
Bakhshi-faisal 10-Jan-17 5:18am    
it doesn't work
OriginalGriff 10-Jan-17 5:39am    
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Show us the exact code you tried to use.
Bakhshi-faisal 10-Jan-17 5:46am    
i know how to display textbox in label
but if i select it will display and it only display one item
for exmaple i have 10 element n my textbox but when i display it it shows only one
i want display all element this is the code that i worte



i tried this
form1
private void btn_voit_Click(object sender, EventArgs e)
{
string textboxchoix = listBox2.Text;
Form2 frm = new Form2(textboxchoix);
frm.Show();
}
form2
public Form2(string textboxchoix)
{
InitializeComponent();
label_choix.Text = textboxchoix;

}
OriginalGriff 10-Jan-17 6:07am    
That's not a TextBox - it's a ListBox, which is a very different thing.
Answer updated
Modified,

If I understand correctly, you're trying to move items from a listbox on a form to another listbox on a different form.

If that is correct then you need to define a public method on the receiving form and pass the items to be added to the method.

On the "sending" side you gather the items and call the method. However note that you need to know the instance for the form, it's not sufficient to only know the class

Consider the following example

On the "receiving" form define a method like the following
C#
public bool AddListboxItems(string[] items) {
   this.listBox1.Items.AddRange(items);

   return true;
}

Remember to use the correct list box name

And if the class of the receiving form is for example Form1 then the following should send the items to a new instance for the class
C#
ListBox lb1 = new ListBox();

lb1.Items.Add("first");
lb1.Items.Add("second");
lb1.Items.Add("third");

// select items 0 and 2
lb1.SelectionMode = SelectionMode.MultiSimple;
lb1.SelectedItems.Add(lb1.Items[0]);
lb1.SelectedItems.Add(lb1.Items[2]);

Form1 frm1 = new Form1(); // The receiving instance should be figured out in another way

string[] items = new string[lb1.SelectedItems.Count];

for (int counter = 0; counter < lb1.SelectedItems.Count; counter++) {
   items[counter] = lb1.SelectedItems[counter].ToString();
}

frm1.AddListboxItems(items);

Note that the receiving form is created in the code so in a real situation you probably need to have a reference to an already existing form.
 
Share this answer
 
v2
Comments
Bakhshi-faisal 10-Jan-17 5:35am    
i have two form in one form i move/pass element between 2 textbox that you have already told me how to do
but now i want to display second textbox in a label
i can display it
but if i select one element it will work but for more than one it don't display i don't know
Wendelius 10-Jan-17 5:47am    
Please post the code you have for handling multiple elements.
Bakhshi-faisal 10-Jan-17 5:50am    
private void pictureBox2_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox2.Items.Count; i++)
{
listBox1.Items.Add(listBox2.Items[i]);
}
listBox2.Items.Clear();
}
this code will move all element from one textbox to an other textbox
Wendelius 10-Jan-17 5:54am    
Listbox isn't the same as textbox, they are completely different classes.

So are you trying to move items from a listbox in form1 to a listbox in form2?
Wendelius 10-Jan-17 6:11am    
See the updated answer

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