List boxes are provided to the user so she can pick one element in the list and act upon it. The most common use of list boxes is file listings. This is the first control element shown where populating the box may be difficult. Rather than creating a list of files for the list box, we have created a list of songs written and sung by the Beatles, and the user gets to pick one (theoretically to play the song on a PC). The list of songs is in a text file named Beatles.dta. This file must be located in directory StandardControls\bin\debug to be read by the StandardControls project, since the StandardControls executable (*.exe) is placed in this subdirectory by the C# compiler. This file, Beatles.dta, could have been placed elsewhere on the PC hard drive, but the filename in the project would have to include the absolute path to the file, starting with C:.
The code that supports this list box is shown below:
Form5
Form5.cs: SC1000: #region Using directives [7 lines, same as SC0002 SC0008] SC1008: #endregion SC1009: namespace StandardControls SC1010: { SC1011: partial class Form5 : Form SC1012: { SC1013: public string[] strBeatleSongs = new string[206]; // 206 songs. SC1014: public Form5() SC1015: {InitializeComponent(); } //-----------------------------------------------------------------------------------------// SC1020: private void Form5_Load(object sender, EventArgs e) SC1021: { // Load list items from file 'Beatles.dta'. // This file is located in 'StandardControls\bin\debug'. SC1022: StreamReader ww = new StreamReader("Beatles.dta"); SC1023: int intSongNumber = 0; SC1024: int listptr = 0; SC1025: char[] dummyChar = new char[1]; SC1026: char[] charDummyBeatleSong = new char[44]; SC1027: dummyChar[0]=' '; SC1028: while(dummyChar[0] != '@') // '@' shows end of file. SC1029: { SC1030: listptr = 0; SC1031: ww.ReadBlock(dummyChar,0,1); SC1032: if(dummyChar[0] == '@') goto Done; SC1033: while(dummyChar[0] != ';') SC1034: { SC1035: if(dummyChar[0] == '"') SC1036: {} SC1037: else SC1038: { SC1039: charDummyBeatleSong[listptr] = dummyChar[0]; SC1040: listptr++; SC1041: } SC1042: ww.ReadBlock(dummyChar,0,1); SC1043: } // dummyChar[0] == ';' is the divider between songs. SC1044: charDummyBeatleSong[listptr] = ' Form5.cs: SC1000: #region Using directives [7 lines, same as SC0002 “ SC0008] SC1008: #endregion SC1009: namespace StandardControls SC1010: { SC1011: partial class Form5 : Form SC1012: { SC1013: public string[] strBeatleSongs = new string[206]; // 206 songs. SC1014: public Form5() SC1015: {InitializeComponent(); } //-----------------------------------------------------------------------------------------// SC1020: private void Form5_Load(object sender, EventArgs e) SC1021: { // Load list items from file 'Beatles.dta'. // This file is located in 'StandardControls\bin\debug'. SC1022: StreamReader ww = new StreamReader("Beatles.dta"); SC1023: int intSongNumber = 0; SC1024: int listptr = 0; SC1025: char[] dummyChar = new char[1]; SC1026: char[] charDummyBeatleSong = new char[44]; SC1027: dummyChar[0]=' '; SC1028: while(dummyChar[0] != '@') // '@' shows end of file. SC1029: { SC1030: listptr = 0; SC1031: ww.ReadBlock(dummyChar,0,1); SC1032: if(dummyChar[0] == '@') goto Done; SC1033: while(dummyChar[0] != ';') SC1034: { SC1035: if(dummyChar[0] == '"') SC1036: {} SC1037: else SC1038: { SC1039: charDummyBeatleSong[listptr] = dummyChar[0]; SC1040: listptr++; SC1041: } SC1042: ww.ReadBlock(dummyChar,0,1); SC1043: } // dummyChar[0] == ';' is the divider between songs. SC1044: charDummyBeatleSong[listptr] = '\0'; SC1045: strBeatleSongs[intSongNumber] = new string(charDummyBeatleSong); // Temporary // if((intSongNumber < 5) || (intSongNumber > 200)) // MessageBox.Show("Song number " + intSongNumber.ToString() //+"is"+strBeatleSongs[intSongNumber] + " ."); SC1046: intSongNumber++; SC1047: } SC1048: Done: SC1049: ww.Close(); SC1050: for(int jj = 0; jj < 205; jj++) SC1051: listBox1.Items.Add(strBeatleSongs[jj]); SC1052: } //-----------------------------------------------------------------------------------------// SC1060: private void button1_Click(object sender, EventArgs e) SC1061: { // Quit. // See which song the User picked. SC1062: MessageBox.Show("The song you picked is: \n '" + strBeatleSongs[listBox1.SelectedIndex] + "'."); SC1063: Close(); SC1064: } SC1065: } SC1066: } //=========================================================================================// Form5.Designer.cs: SC1100: namespace StandardControls SC1101: { [1 label, 1 listBox, 1 button] SC1150: }
'; SC1045: strBeatleSongs[intSongNumber] = new string(charDummyBeatleSong); // Temporary // if((intSongNumber < 5) || (intSongNumber > 200)) // MessageBox.Show("Song number " + intSongNumber.ToString() //+"is"+strBeatleSongs[intSongNumber] + " ."); SC1046: intSongNumber++; SC1047: } SC1048: Done: SC1049: ww.Close(); SC1050: for(int jj = 0; jj < 205; jj++) SC1051: listBox1.Items.Add(strBeatleSongs[jj]); SC1052: } //-----------------------------------------------------------------------------------------// SC1060: private void button1_Click(object sender, EventArgs e) SC1061: { // Quit. // See which song the User picked. SC1062: MessageBox.Show("The song you picked is: \n '" + strBeatleSongs[listBox1.SelectedIndex] + "'."); SC1063: Close(); SC1064: } SC1065: } SC1066: } //=========================================================================================// Form5.Designer.cs: SC1100: namespace StandardControls SC1101: { [1 label, 1 listBox, 1 button] SC1150: }
The temporary lines between SC1045 and SC1046 were left in the code to show how we checked the song loading procedure (from the file to string[] strBeatleSongs[]).