Solved

Problem using an array.

Posted on 2004-04-13
3
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 37

Expert Comment

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

Expert Comment

by:gregoryyoung
ID: 10819542
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
ID: 10820612
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 64
LINQ Select all records by MAX data syntax help 1 30
Please explain purpose of GZIP 4 60
Nested forach loop to linq 3 30
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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