国家  china

Spread for Windows Forms Combobox花式操作

        Spread for  Windows Forms 可以定义Combobox这个是众所周知的,但是大多数人并不知道Spread for Windows Forms 定义的Combobox是可以改变options是可以加入图片的,也可以改变背景颜色的。

image

        下面就让我们来看看,Spread for  Windows Forms 是如何操作的:

        1.创建一个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.创建Combobox对象

GrapeCity.Win.Spread.InputMan.CellType.GcComboBoxCellType gccombo = new GrapeCity.Win.Spread.InputMan.CellType.GcComboBoxCellType();

       3.设置options的值

gccombo.DropDownStyle = ComboBoxStyle.DropDownList;
gccombo.Items.AddRange(new String[] { "Raspberry", "Vanilla", "Chocolate", "Banana" });

        4.设置图片与options的背景颜色

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.设置combobox的显示样式

gccombo.ImageAlign = HorizontalAlignment.Right;
gccombo.ListSelectedItemStyle.BackColor = Color.Bisque;
gccombo.ListSelectedItemStyle.ForeColor = Color.Black;

        6.给指定单元格设置celltype

fpSpread1.Sheets[0].Cells[1, 1].CellType = gccombo;

        7.如果您需要联动逻辑,可以参考设置联动单元格

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

        8.设置单元格的样式

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.设置并绑定ComboSelChange事件

fpSpread1.ComboSelChange += fpSpread1_ComboSelChange;
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;
}
}

           大功告成,让我们来看看结果如何:

image

           选择其中后会触发ComboSelChange事件,自动计算total

image

             就是这么简单,就是这么freestyle。

image

 

相关源码下载请点击以下地址:

http://gcdn.gcpowertools.com.cn/forum.php?mod=viewthread&tid=38566&extra=page%3D1

有兴趣想要下载本产品的可以在以下网址进行下载试用:

http://www.gcpowertools.com.cn/products/download.aspx?pid=46

Spread Windows Forms还可以实现很多精彩的功能,您可以访问以下站点进行详细了解:

http://www.gcpowertools.com.cn/products/spread_winforms.htm

转载声明:
欢迎将本站文章进行转载、演绎或用于商业目的,转载时请注明以下信息
文章转自:葡萄城控件产品博客,http://blog.gcpowertools.com.cn
原文地址:http://blog.gcpowertools.com.cn/post/Spread-Windows-Forms-ComboBox-Cell.aspx

不允许评论