d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C# List Box Question
Add Reply New Topic New Poll
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 25 2015 05:51pm
I making a program with visual c# just playing around

I have 3 text box (item/description/ price) where you input those 3 params then after the add button is hit I want them to be logged to the console (list box in this case, but im not sure how to add them all in sequence)
I have the basic code to add one of them
Code
consoleListBox.Items.Add(itemBox.Text);
but when i try multiple params it overloads and gives me an error
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 25 2015 05:57pm
Code
consoleListBox.Items.Add(itemBox1.Text);
consoleListBox.Items.Add(itemBox2.Text);
consoleListBox.Items.Add(itemBox3.Text);


any reason you can't do this?

also can do

Code
consoleListBox.Items.AddRange(itemBox1.Text, itemBox2.Text, itemBox3.Text);


if it complains about the format in that one you can just New up a generic list and toss them in there

This post was edited by Eep on Mar 25 2015 05:59pm
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 25 2015 06:28pm
Quote (Eep @ Mar 25 2015 07:57pm)
Code
consoleListBox.Items.Add(itemBox1.Text);
consoleListBox.Items.Add(itemBox2.Text);
consoleListBox.Items.Add(itemBox3.Text);


any reason you can't do this?

also can do

Code
consoleListBox.Items.AddRange(itemBox1.Text, itemBox2.Text, itemBox3.Text);


if it complains about the format in that one you can just New up a generic list and toss them in there


it puts em in order like
ib1
ib2
ib3
and I would prefer them in a list like
ib1 ib2 ib3
but when i try to format it it gives me a compiler error
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 25 2015 06:29pm
and to your last solution i tried the addrange

returns

error: no overload for method addrange takes 3 paramaters
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 25 2015 06:42pm
Quote (Trig @ Mar 25 2015 07:29pm)
and to your last solution i tried the addrange

returns

error: no overload for method addrange takes 3 paramaters


We personally write wrappers for addrange at work. You might consider this.

Try creating a List(of String) and adding your items to that and then passing that in?



Also, if you want a single string that is just

b1 ib2 ib3

try doing maybe:

Code
consoleListBox.Items.Add(itemBox.Text & " " & itemBox2.Text & " " & itemBox3.Text);


This post was edited by Eep on Mar 25 2015 06:42pm
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 25 2015 07:24pm
Quote (Eep @ Mar 25 2015 08:42pm)
We personally write wrappers for addrange at work. You might consider this.

Try creating a List(of String) and adding your items to that and then passing that in?



Also, if you want a single string that is just

b1 ib2 ib3

try doing maybe:

Code
consoleListBox.Items.Add(itemBox.Text & " " & itemBox2.Text & " " & itemBox3.Text);


It returns error:

operator '&' cannot be used on operands type 'string' and 'string'

also, let me try creating with list of string and get back here
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 25 2015 07:25pm
use +

he's thinking VB
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 25 2015 07:56pm
Quote (carteblanche @ Mar 25 2015 08:25pm)
use +

he's thinking VB


ohh, my bad. Yeah, I was thinking vb (go figure)

in VB you can use & or +, the documentation says & is the best way to go for small concats like that.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll