The Dialogs

 

Combo Boxes

The ComboBox control is an improvement on the simple ListBox control because it transfers the element in the list that the user chooses to the top part of the control, and more importantly, it conducts a search when the user types characters into the top edit control. The combo box is a combination of an edit box at the top and a list box below.

image from book
Figure 19-6: Combo Boxes window

While you are in the IDE with the Form6 comboBox highlighted, note that under Property Appearance and DropDownStyle there are three choices: Simple, DropDown, and DropDown List. Unfortunately, none of these three styles allows the programmer to drop down the list as it is being presented to the user; in every case the user must click on the down arrow at the right side of the edit box to make the list visible. This is a mistake ” the list should be pre-opened, but Microsoft has never chosen to implement this configuration.

As soon as the user clicks on a song, that song moves up to the edit box. If the user types characters into the edit box, the list will scroll up or down to provide the best possible match to the user s request. This is a magnificent control; thank goodness the automated code handles all these details.

If the user has picked a song, as soon as the Quit button is clicked, that song name will be shown to the user in a message box. The code supporting this combo box is shown below:

Form6

 Form6.cs: SC1200:       #region Using directives               [7 lines, same as SC0002  SC0008] SC1208:       using System.IO; // 'StreamReader'. SC1209:       #endregion  SC1210:       namespace StandardControls SC1211:       { SC1212:         partial class Form6 : Form SC1213:         { SC1214:           public string[] strBeatleSongs = new string[206]; // 206 songs. SC1215:           public Form6() SC1216:           {InitializeComponent(); } //-----------------------------------------------------------------------------------------// SC1220:           private void Form6_Load(object sender, EventArgs e) SC1221:           { // Use a 'Simple' combo box.                     // comboEnumeration.SimpleStyle = ComboBoxStyle.Simple;                     // Load list items from file 'Beatles.dta'.                     // This file is located in 'StandardControls\bin\debug'. SC1222:             StreamReader ww = new StreamReader("Beatles.dta"); SC1223:             int intSongNumber = 0; SC1224:             int listptr = 0; SC1225:             char[] dummyChar = new char[1]; SC1226:             char[] charDummyBeatleSong = new char[44]; SC1227:             dummyChar[0]=''; SC1228:             while(dummyChar[0] != '@') // '@' shows end of file. SC1229:             { SC1230:               listptr = 0; SC1231:               ww.ReadBlock(dummyChar,0,1); SC1232:               if(dummyChar[0] == '@') goto Done; SC1233:               while(dummyChar[0] != ';') SC1234:               { SC1235:                 if(dummyChar[0] == '"') SC1236:                 {} SC1237:                 else SC1238:                 { SC1239:                   charDummyBeatleSong[listptr] = dummyChar[0]; SC1240:                   listptr++; SC1241:                 } SC1242:                 ww.ReadBlock(dummyChar,0,1); SC1243:               }                       // dummyChar[0] == ';' is the divider between songs. SC1244:               charDummyBeatleSong[listptr] = '
   Form6.cs: SC1200: #region Using directives [7 lines, same as SC0002  “ SC0008] SC1208: using System.IO; // 'StreamReader'. SC1209: #endregion SC1210: namespace StandardControls SC1211: { SC1212: partial class Form6 : Form SC1213: { SC1214: public string[] strBeatleSongs = new string[206]; // 206 songs. SC1215: public Form6() SC1216: {InitializeComponent(); } //-----------------------------------------------------------------------------------------// SC1220: private void Form6_Load(object sender, EventArgs e) SC1221: { // Use a 'Simple' combo box. // comboEnumeration.SimpleStyle = ComboBoxStyle.Simple; // Load list items from file 'Beatles.dta'. // This file is located in 'StandardControls\bin\debug'. SC1222: StreamReader ww = new StreamReader("Beatles.dta"); SC1223: int intSongNumber = 0; SC1224: int listptr = 0; SC1225: char[] dummyChar = new char[1]; SC1226: char[] charDummyBeatleSong = new char[44]; SC1227: dummyChar[0]=''; SC1228: while(dummyChar[0] != '@') // '@' shows end of file. SC1229: { SC1230: listptr = 0; SC1231: ww.ReadBlock(dummyChar,0,1); SC1232: if(dummyChar[0] == '@') goto Done; SC1233: while(dummyChar[0] != ';') SC1234: { SC1235: if(dummyChar[0] == '"') SC1236: {} SC1237: else SC1238: { SC1239: charDummyBeatleSong[listptr] = dummyChar[0]; SC1240: listptr++; SC1241: } SC1242: ww.ReadBlock(dummyChar,0,1); SC1243: } // dummyChar[0] == ';' is the divider between songs. SC1244: charDummyBeatleSong[listptr] = '\0'; SC1245: strBeatleSongs[intSongNumber] = new string(charDummyBeatleSong); // Temporary // if((intSongNumber < 5) || (intSongNumber > 200)) // MessageBox.Show("Song number " + intSongNumber.ToString() //+"is"+strBeatleSongs[intSongNumber] + " ."); SC1246: intSongNumber++; SC1247: } SC1248: Done: SC1249: ww.Close(); SC1250: for(int jj = 0; jj < 205; jj++) SC1251: comboBox1.Items.Add(strBeatleSongs[jj]); SC1252: } //-----------------------------------------------------------------------------------------// SC1253: private void button1_Click(object sender, EventArgs e) SC1254: { // Quit. // See which song the User picked. SC1255: MessageBox.Show("The song you picked is: \n " + strBeatleSongs[comboBox1.SelectedIndex]); SC1256: Close(); SC1257: } SC1258: } SC1259: } //=========================================================================================// Form6.Designer.cs: SC1300: namespace StandardControls SC1301: { [2 labels, 1 comboBox, 1 button] SC1359: }   
'; SC1245: strBeatleSongs[intSongNumber] = new string(charDummyBeatleSong); // Temporary // if((intSongNumber < 5) || (intSongNumber > 200)) // MessageBox.Show("Song number " + intSongNumber.ToString() //+"is"+strBeatleSongs[intSongNumber] + " ."); SC1246: intSongNumber++; SC1247: } SC1248: Done: SC1249: ww.Close(); SC1250: for(int jj = 0; jj < 205; jj++) SC1251: comboBox1.Items.Add(strBeatleSongs[jj]); SC1252: } //-----------------------------------------------------------------------------------------// SC1253: private void button1_Click(object sender, EventArgs e) SC1254: { // Quit. // See which song the User picked. SC1255: MessageBox.Show("The song you picked is: \n " + strBeatleSongs[comboBox1.SelectedIndex]); SC1256: Close(); SC1257: } SC1258: } SC1259: } //=========================================================================================// Form6.Designer.cs: SC1300: namespace StandardControls SC1301: { [2 labels, 1 comboBox, 1 button] SC1359: }

The temporary lines between SC1245 and SC1246 were left in the code to show how we checked the song loading procedure (from the file to string[] strBeatleSongs[]).

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net