Solved

Problem using an array.

Posted on 2004-04-13
3
222 Views
Last Modified: 2010-04-15
I created an array (using ArrayList()) called load when my form loads. I use the array as values for my combobox.
I am trying to use this array to assign a value to a text box when the combobox value is selected using the following code.

public void comboBox1_SelectedValueChanged(object sender, System.EventArgs e)
             {
                
      if(comboBox1.SelectedIndex != -1)
        Console.WriteLine(comboBox1.SelectedValue.ToString());
      else
        label2.Text=load[comboBox1.SelectedIndex].ToString();
                  
             }

My problem is I am from the old school and can't make my array Public. What would be a work around for this? Can someone shed some light on the theory why its like this?
0
Comment
Question by:Jimbo456
  • 2
3 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
if(comboBox1.SelectedIndex != -1)
    access comboBox1.SelectedItem (which is the array element if you used databinding)
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
o yeah also your code will crash most likely since it will only ever try to access the array at element[-1]
0
 
LVL 4

Accepted Solution

by:
caner_elci earned 500 total points
Comment Utility
Try some code like this:

ArrayList load = new ArrayList();

private void Form1_Load(object sender, System.EventArgs e)
{
      // Initialize load array
      load.Add( "Sample value 1" );
      load.Add( "Sample value 2" );
      load.Add( "Sample value 3" );
      load.Add( "Sample value 4" );
      load.Add( "Sample value 5" );

      // Add them to comboBox1, but first clear it
      comboBox1.Items.Clear();
      foreach( string val in load )
            comboBox1.Items.Add( val );
}

// Use this event, not SelectedValueChanged
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
      if( comboBox1.SelectedIndex >= 0 )
            textBox1.Text = (string)load[comboBox1.SelectedIndex];
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now