How to Load an Array Element to a Listbox in Csharp
If you are familiar with the elements or index of an array this will be a very easy task for you, the program simply loads the value of the array to the listbox control, you need 1 button and a listbox to load in the form, the rest will load in the source code. The Text Property of the button will be “Load Value”, please refer to the image below.
And type the source code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace LoadArrayInListbox
{
public partial class Form1 :
Form
{
public Form1()
{
InitializeComponent();
}
//In this area we declared a
an array with a 5 elements and inside the method LoadArray() we also assigned a
value for each element
private const int
arrIndex = 5;
private String[] planet =
new String[arrIndex];
private void LoadArray()
{
planet[0] =
"Pluto";
planet[1] =
"Mars";
planet[2] =
"Mercury";
planet[3] =
"Saturn";
planet[4] =
"Jupiter";
Array.Sort(planet);
for (int i = 0; i
< planet.Length; i++)
{
listBox1.Items.Add(planet[i]);
}
}
private void
button1_Click(object sender, EventArgs e)
{
LoadArray();
}
}
}
Final Output:
How to Load an Array Element to a Listbox in Csharp
Reviewed by code-dev
on
9:51 PM
Rating:
No comments: