0

First of all; I cannot use list. It has to be a array for school purposes. I have a string[] Brands = new string[10]; Four elements of this array are already filled when the form is initialized. The empty elements can be filled by adding textbox value to array with btnclick I'm displaying the filled elements in a listbox. The problem is that the listbox displays it like this:

Kawasaki
Yamaha
Harley
Suzuki

It needs to be displayed like this: Kawasaki Yamaha Harley Suzuki (when i click the buttun it needs to the already existing elementsd like this)
Kawasaki Yamaha Harley Suzuki (the added value1)

Another btnclick:
Kawasaki Yamaha Harley Suzuki (the added value1) (the added value2)

I know I need to use a for loop in my btnclik but I don't know how. This is what I currently have in my btnclick event:

if (brandNr >= 10)
   return;

Brands[brandNr++] = textBoxBrand.Text;
listBoxMotorcycles.DataSource = null;
listBoxMotorcycles.DataSource = Brands;
11
  • 1
    Why are you using a listbox if you don't want to display the elements as a list? Commented Jan 14, 2016 at 11:54
  • I know it's strange but I need to use it like this for school. I need to use a array with fixed length > add values to aray with textbox > display the values in a listbox Commented Jan 14, 2016 at 11:55
  • What doesn't work in your solution? Commented Jan 14, 2016 at 11:56
  • 1
    The listbox displays the values as a list but it needs to display it as increments. Commented Jan 14, 2016 at 12:00
  • I really doubt in your school make you use a listbox to display elements horizontally,just doesn't make sense to me..you should use a textbox or a label for the output array Commented Jan 14, 2016 at 12:00

1 Answer 1

1

first of all make sure that for form is wider enough, then on the load event you can write this code

 Brands[0] = "Kawasaki";
 Brands[1] = "Yamaha";
 Brands[2] = "Harley";
 Brands[3] = "Suzuki";
 listBoxMotorcycles.MultiColumn = true;
 listBoxMotorcycles.ColumnWidth = 100;
 listBoxMotorcycles.Width  = 400;
 listBoxMotorcycles.Height = 20;
 listBoxMotorcycles.DataSource = Brands;

and on the button click event your code must be he follwing

if(listBoxMotorcycles.Items.Count < Brands.Length)
{
    Brands[listBoxMotorcycles.Items.Count] = textBox1.Text;
    listBoxMotorcycles.DataSource = null;
    listBoxMotorcycles.DataSource = Brands;

    textBox1.Text = string.Empty;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.