?
Solved

Problem using an array.

Posted on 2004-04-13
3
Medium Priority
?
228 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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