Solved

Problem using an array.

Posted on 2004-04-13
3
225 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 24
Best resource to learn entity framework 2 47
MediaHelp 4 26
using sqldatareader to populate cells in Excel-syntax 6 17
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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