A combo box can be useful when creating a form for users such as an on-line order form.

The new GcComboBox cell in Spread for Windows Forms allows you to set different backcolors for each list item. The GcComboBox also has a resize icon that allows you to resize the drop-down list. Both the GcComboBox cell and the original Spread combo box cell allow you to add images to the drop-down list. The GcComboBox cell can display the image in the edit area with the TextBoxStyle property.

The following example creates a simple form that displays a price when you select from a list of dessert flavors.

GcComboBox Cell


Selected Item


Use the following steps to create this example:

  1. Add images to an image list.
     ImageList img = new ImageList();
    img.Images.Add(Image.FromFile("C:\\Program Files (x86)\\GrapeCity\\rasp.png"));
    img.Images.Add(Image.FromFile("C:\\Program Files (x86)\\GrapeCity\\vanilla.png"));
    img.Images.Add(Image.FromFile("C:\\Program Files (x86)\\GrapeCity\\choco.png"));
    img.Images.Add(Image.FromFile("C:\\Program Files (x86)\\GrapeCity\\banana.png"));

  2. Create the GcComboBox cell.
    GrapeCity.Win.Spread.InputMan.CellType.GcComboBoxCellType gccombo = new GrapeCity.Win.Spread.InputMan.CellType.GcComboBoxCellType(); 

  3. Set cell properties and add the list items.
    gccombo.DropDownStyle = ComboBoxStyle.DropDownList;
    gccombo.Items.AddRange(new String[] { "Raspberry", "Vanilla", "Chocolate", "Banana" });

  4. Set colors and images for the list items. Assign the image list to the cell.
    gccombo.Items[0].BackColor = Color.Fuchsia;
    gccombo.Items[1].BackColor = Color.Ivory;
    gccombo.Items[2].BackColor = Color.Chocolate;
    gccombo.Items[3].BackColor = Color.LightYellow;
    gccombo.ImageList = img;
    gccombo.Items[0].Image = 0;
    gccombo.Items[1].Image = 1;
    gccombo.Items[2].Image = 2;
    gccombo.Items[3].Image = 3;
    gccombo.ShowListBoxImage = true;

  5. Set any cell formatting properties.
    gccombo.ImageAlign = HorizontalAlignment.Right;
    gccombo.ListSelectedItemStyle.BackColor = Color.Bisque;
    gccombo.ListSelectedItemStyle.ForeColor = Color.Black;

  6. Assign the cell type to the cell location.
    fpSpread1.Sheets[0].Cells[1, 1].CellType = gccombo; 

  7. Add the currency cell.
    FarPoint.Win.Spread.CellType.CurrencyCellType currencycell = new FarPoint.Win.Spread.CellType.CurrencyCellType();
    fpSpread1.Sheets[0].Cells[1, 2].CellType = currencycell;

  8. Add text information and set the font and alignment.
    fpSpread1.Sheets[0].Cells[0, 1].Text = "Select a flavor";
    fpSpread1.Sheets[0].Cells[0, 2].Text = "Total";
    fpSpread1.Sheets[0].Columns[1, 2].Font = new Font("Calibri", 10, FontStyle.Bold);
    fpSpread1.Sheets[0].Columns[1, 2].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
    fpSpread1.Sheets[0].Rows[1].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;

  9. Use the ComboSelChange event to get the selection and set the price.
    private void fpSpread1_ComboSelChange(object sender, FarPoint.Win.Spread.EditorNotifyEventArgs e)
    {
    string caseSwitch;
    caseSwitch = fpSpread1.Sheets[0].Cells[1, 1].Text;
    switch (caseSwitch)
    {
    case "Raspberry":
    fpSpread1.Sheets[0].Cells[1, 2].Value = 5.00;
    break;
    case "Vanilla":
    fpSpread1.Sheets[0].Cells[1, 2].Value = 4.00;
    break;
    case "Chocolate":
    fpSpread1.Sheets[0].Cells[1, 2].Value = 4.25;
    break;
    case "Banana":
    fpSpread1.Sheets[0].Cells[1, 2].Value = 4.75;
    break;
    }
    }


Here is the complete example in C#:

 // Select dessert flavor.
ImageList img = new ImageList();
img.Images.Add(Image.FromFile("C:\\Program Files (x86)\\GrapeCity\\rasp.png"));
img.Images.Add(Image.FromFile("C:\\Program Files (x86)\\GrapeCity\\vanilla.png"));
img.Images.Add(Image.FromFile("C:\\Program Files (x86)\\GrapeCity\\choco.png"));
img.Images.Add(Image.FromFile("C:\\Program Files (x86)\\GrapeCity\\banana.png"));
GrapeCity.Win.Spread.InputMan.CellType.GcComboBoxCellType gccombo = new GrapeCity.Win.Spread.InputMan.CellType.GcComboBoxCellType();
gccombo.DropDownStyle = ComboBoxStyle.DropDownList;
gccombo.Items.AddRange(new String[] { "Raspberry", "Vanilla", "Chocolate", "Banana" });
gccombo.Items[0].BackColor = Color.Fuchsia;
gccombo.Items[1].BackColor = Color.Ivory;
gccombo.Items[2].BackColor = Color.Chocolate;
gccombo.Items[3].BackColor = Color.LightYellow;

gccombo.ImageList = img;
gccombo.Items[0].Image = 0;
gccombo.Items[1].Image = 1;
gccombo.Items[2].Image = 2;
gccombo.Items[3].Image = 3;
gccombo.ShowListBoxImage = true;
gccombo.ImageAlign = HorizontalAlignment.Right;
gccombo.ListSelectedItemStyle.BackColor = Color.Bisque;
gccombo.ListSelectedItemStyle.ForeColor = Color.Black;
fpSpread1.Sheets[0].Cells[1, 1].CellType = gccombo;
fpSpread1.Sheets[0].Columns[1].Width = 200;
fpSpread1.Sheets[0].Rows[1].Height = 40;
FarPoint.Win.Spread.CellType.CurrencyCellType currencycell = new FarPoint.Win.Spread.CellType.CurrencyCellType();
fpSpread1.Sheets[0].Cells[1, 2].CellType = currencycell;
fpSpread1.Sheets[0].Cells[0, 1].Text = "Select a flavor";
fpSpread1.Sheets[0].Cells[0, 2].Text = "Total";
fpSpread1.Sheets[0].Columns[1, 2].Font = new Font("Calibri", 10, FontStyle.Bold);
fpSpread1.Sheets[0].Columns[1, 2].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
fpSpread1.Sheets[0].Rows[1].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;

private void fpSpread1_ComboSelChange(object sender, FarPoint.Win.Spread.EditorNotifyEventArgs e)
{
string caseSwitch;
caseSwitch = fpSpread1.Sheets[0].Cells[1, 1].Text;
switch (caseSwitch)
{
case "Raspberry":
fpSpread1.Sheets[0].Cells[1, 2].Value = 5.00;
break;
case "Vanilla":
fpSpread1.Sheets[0].Cells[1, 2].Value = 4.00;
break;
case "Chocolate":
fpSpread1.Sheets[0].Cells[1, 2].Value = 4.25;
break;
case "Banana":
fpSpread1.Sheets[0].Cells[1, 2].Value = 4.75;
break;
}
}


Here is the complete example in VB:

Dim img As New ImageList()
img.Images.Add(Image.FromFile("C:\Program Files (x86)\GrapeCity\rasp.png"))
img.Images.Add(Image.FromFile("C:\Program Files (x86)\GrapeCity\vanilla.png"))
img.Images.Add(Image.FromFile("C:\Program Files (x86)\GrapeCity\choco.png"))
img.Images.Add(Image.FromFile("C:\Program Files (x86)\GrapeCity\banana.png"))
Dim gccombo As New GrapeCity.Win.Spread.InputMan.CellType.GcComboBoxCellType()
gccombo.DropDownStyle = ComboBoxStyle.DropDownList
gccombo.Items.AddRange(New String() {"Raspberry", "Vanilla", "Chocolate", "Banana"})
gccombo.Items(0).BackColor = Color.Fuchsia
gccombo.Items(1).BackColor = Color.Ivory
gccombo.Items(2).BackColor = Color.Chocolate
gccombo.Items(3).BackColor = Color.LightYellow
gccombo.ImageList = img
gccombo.Items(0).Image = 0
gccombo.Items(1).Image = 1
gccombo.Items(2).Image = 2
gccombo.Items(3).Image = 3
gccombo.ShowListBoxImage = True
gccombo.ImageAlign = HorizontalAlignment.Right
gccombo.ListSelectedItemStyle.BackColor = Color.Bisque
gccombo.ListSelectedItemStyle.ForeColor = Color.Black
FpSpread1.Sheets(0).Cells(1, 1).CellType = gccombo
FpSpread1.Sheets(0).Columns(1).Width = 200
FpSpread1.Sheets(0).Rows(1).Height = 40
Dim currencycell As New FarPoint.Win.Spread.CellType.CurrencyCellType()
FpSpread1.Sheets(0).Cells(1, 2).CellType = currencycell
FpSpread1.Sheets(0).Cells(0, 1).Text = "Select a flavor"
FpSpread1.Sheets(0).Cells(0, 2).Text = "Total"
FpSpread1.Sheets(0).Columns(1, 2).Font = New Font("Calibri", 10, FontStyle.Bold)
FpSpread1.Sheets(0).Columns(1, 2).HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center
FpSpread1.Sheets(0).Rows(1).VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center
Private Sub FpSpread1_ComboSelChange(sender As Object, e As FarPoint.Win.Spread.EditorNotifyEventArgs) Handles FpSpread1.ComboSelChange
Dim caseSwitch As String
caseSwitch = FpSpread1.Sheets(0).Cells(1, 1).Text
Select Case caseSwitch
Case "Raspberry"
FpSpread1.Sheets(0).Cells(1, 2).Value = 5.0
Exit Select
Case "Vanilla"
FpSpread1.Sheets(0).Cells(1, 2).Value = 4.0
Exit Select
Case "Chocolate"
FpSpread1.Sheets(0).Cells(1, 2).Value = 4.25
Exit Select
Case "Banana"
FpSpread1.Sheets(0).Cells(1, 2).Value = 4.75
Exit Select
End Select
End Sub


The following help topic has information about adding the Spread component to a Visual Studio project: http://sphelp.grapecity.com/WebHelp/SpreadNet10/WF/webframe.html#spwin-vs2015add.html.

For more information about the GcComboBox cell, refer to http://sphelp.grapecity.com/WebHelp/SpreadNet10/WF/webframe.html#spwin-gccombo.html.