2024年2月9日发(作者:)
DotNet第三方控件使用笔记
一、ButtonX控件
可实现如下效果:
(1)在ButtonX上,是否显示图像,取决于“images”属性。
(2)在“ButtomItem”控件中,是否“只是显示图像”,“只是文本”,还是“图像和文本都显示”,取决于
。
二、BalloonTip控件(气泡提示)
在添加了BalloonTip控件后,其他各个控件会出现对应的“……上的BalloonText”和“……上的BalloonCaption”属性,通过修改这两个属性可设置鼠标移动到控件时弹出的气泡提示的标题和内容,也可通过代码设置这个两个属性,如:
loonCaption(txtUserName, "提示");
loonText(txtUserName, "输入你的大名吧");
loonCaption(txtUserPw, "提示;
loonText(txtUserPw, "大名输完了就是你的密码咯");")
1. 使用效果
1) 效果一:在鼠标在控件上面停留时,出现提示信息,如下图:
2) 效果二:当控件获得焦点时,也出现如上图一样的信息。
2. 实现上述两种效果的途径
1) 将BalloonTip控件的“ShowBalloonOnFacus”属性设置为“False”即可实现效果一。
3. 设置BalloonTip显示的内容
2) 将“ShowBalloonOnFacus”属性设置为“True”,即可实现效果二。
1) 在欲设置该属性的控件的“BalloonTipOnFocus上的BalloonCaption”和“BalloonTipOnHover上的BalloonCaption”属性中,分别设置这两种效果的“标题”属性;
2) 在“BalloonTipOnFocus上的BalloonText”和在“BalloonTipOnHover上的BalloonText”属性中,分别设置这两种效果的“显示内容”的属性。
4. 属性“AlerAnimation”设置“BalloonTip”出现的效果
5. “BalloonTip”除了出现在该控件附近,还可以出现在屏幕的右下角,示例程序如下:
private AlertCustom m_AlertOnLoad=null;
m_AlertOnLoad=new AlertCustom();
Rectangle r=kingArea(this);
m_on=new Point(-m_,-m_);
m_ose=true;
m_oseTimeOut=15;
m_nimation=ToTop;
m_nimationDuration=300;
m_(false);// false指示该控件是否需要获得焦点才出现“BalloonTip”
6. “BalloonTip”除了可以通过“添加控件”的方式使用,也可以通过编程的方式使用,示例程序如下:
n b=new n();
=;
nImage=() as Image;
nText="Balloon Status InFormation";
="Balloons are now enabled for Balloon Tip Test area. Hover mouse over the area and set the focus to any control.";
nimation=ottom;
size();
ose=true;
oseTimeOut=4;
=this;// 指示父控件
(button2,false);// button2指示在那个控件附近出现“BalloonTip”
7. 还可以对“BalloonTip”出现时的效果进行程序控制,如下:
// BalloonTriggerControl property returns control that invoked balloon
// BalloonTriggerControl属性返回触发“BalloonTip”的控件
if(nTriggerControl==groupBox1)
{
// BalloonControl is already prepared Balloon control that is just about to be displayed
// Setting BalloonControl to null will cancel balloon display
Point p=osition;
// Adjust cursor position so cursor is below tip
(-set,nControl.
TipLength+4);
//Offset属性将点p进行平移
on=p;
}
8. 与之类似的是DotNetToolTip所有控件都有的“ToolTip”属性,也可以是控件在鼠标划过时出现类似的东西,如下图:
三、DotNetBarManager控件与Bar控件的使用
示例效果如下图:
1. DotNetBarManager控件
将该控件添加到工程后,将给Bar控件提供了“停靠点(DockSite)”,可以实现工具栏在窗体的“上、下、左、右”摆放。
可以通过程序实现,也可以通过“属性页”的“Dock”属性进行设置。
de = ;
2. DotNetBarManager控件,通过右键菜单
可以轻松实现如下图所示的布局。
通过拖动也可以轻易的实现如下视图的布局
与上图相应的代码:
// Create new document and add it to existing bar
ntainerItem DockItem=new ntainerItem();
="Custom Document";
// Add control to it
TextBox t=new TextBox();
ze=false;
ine=true;
=;
// PanelDockContainer will be used to host any controls. It provides automatic focus management so focused
// document tab appears bold
ockContainer panel = new ockContainer();
= ;
(t);
l=panel;
(DockItem);
//添加第二个选项卡
/****************************************************************************************************/
ntainerItem DockItem1 = new ntainerItem();
= "This is the second!";
Label l = new Label();
= ;
ockContainer panel1 = new ockContainer();
= ;
(l);
l = panel1;
(DockItem1);
/****************************************************************************************************/
if(!e)
e=true;
else
Layout();
// Optimize display by disabling layout for all Dock sites
dLayout=true;
try
{
foreach( bar in )
{
if(de==nt)
{
foreach(ntainerItem Dock in )
e=true;
}
if(!e)
e=true;
}
}
finally
{
dLayout=false;
}
代码实现的界面如下图:
3. 下面的代码通过编程的方式实现如下图所示的功能
private void Form1_Load(object sender, rgs e)
{
bChange+=new bChangeEventHandler(bChanged);
CreateBottomBarAutoHide();
CreateLeftDockedBars();
}
private void DockTabChanged(object sender, DockTabChangeEventArgs e)
{
// 使Bar的标题与当前停靠选项卡同步
Bar bar=sender as Bar;
if(bar==null || ==null)
return;
=;
}
private void CreateBottomBarAutoHide()
{
Bar bar=new Bar("Bottom Bar in auto-hide state");
="bottomBar";
Type=ntainer; // 停靠容器布局需要可停靠窗口
h=true; // 可停靠窗口拉伸至填补容器
deAnimationTime=0; // 一些控件不支持动画所以关掉它
ndleStyle=n; // 可停靠窗口有标题
(bar); // DotNetBar需要添加bar以便能管理它的停靠等
// 创建托管控件
DockContainerItem DockItem=new DockContainerItem("bottomDockItem1","First
Dock item");
(DockItem);
// 创建我们想放在可停靠窗口的控件
UserControl1 DockedControl=new UserControl1();
=+" - "+;
lor=;
l=DockedControl; // 指定该控件是托管在停靠容器上
=128; // 指定可停靠容器的高度和同一时间控制
// 创建第二个停靠容器并添加到Bar中
DockItem=new DockContainerItem("bottomDockItem2","Second Dock item");
(DockItem);
DockedControl=new UserControl1();
=+" - "+;
lor=rine;
l=DockedControl; // 指定该控件是托管在停靠容器上
// 将Bar停靠到下面的停靠位置
umentUIManager().Dock(bar);
=128; // 指定可停靠容器的高度和同一时间控制
Layout(); // 适用所有的变化...
de=true; // 将Bar置为自动隐藏模式。Bar需要停靠的地方,才能置为自动隐藏模式
}
private void CreateLeftDockedBars()
{
// 将前两个Bar并排停靠并将第三个Bar停靠在他们之后...
Bar bar=new Bar("Bar1");
="leftBar1";
Type=ntainer; // 停靠容器布局需要可停靠窗口
h=true; // 可停靠窗口拉伸至填补容器
deAnimationTime=0; // 一些控件不支持动画所以关掉它
ndleStyle=n; // 可停靠窗口有标题
e=true;
// 为Bar创建DockContainerItem,该项应该在Bar停靠前添加
DockContainerItem DockItem=new DockContainerItem("leftDockItem1","Top Left
Dock Container");
(DockItem);
// 创建我们想放在可停靠窗口的控件
UserControl1 DockedControl=new UserControl1();
=+" - "+;
lor=;
l=DockedControl; // 指定该控件是托管在停靠容器上
(bar); // DotNetBar需要添加bar以便能管理它的停靠等
umentUIManager().Dock(bar); // 将Bar停靠到左边的可停靠位置
=128; // 在停靠之后指定停靠容器项的宽度
// 创建第二个Bar并将它停靠低于第一个Bar但仍在同一行
Bar bar2=new Bar("Bar2");
="leftBar2";
Type=ntainer; // 停靠容器布局需要可停靠窗口
deAnimationTime=0; // 一些控件不支持动画所以关掉它
h=true; // 可停靠窗口拉伸至填补容器
e=true;
ndleStyle=n; // 可停靠窗口有标题
// 添加新的停靠容器到Bar上,应该在添加Bar之前执行以便大小可以计算正确
DockItem=new DockContainerItem("leftDockItem2","Bottom Left Dock Container");
(DockItem);
// 创建我们想放在可停靠窗口的控件
DockedControl=new UserControl1();
=+" - "+;
lor=er;
l=DockedControl; // 指定该控件是托管在停靠容器上
(bar2); // DotNetBar需要添加bar以便能管理它的停靠等
umentUIManager().Dock(bar, bar2, ); // 停靠新bar2低于我们先前创建的Bar
// 创建第三个Bar,停靠在第一个和第二个之后
// i.e. on the line 1
bar=new Bar("Bar3");
="leftBar3";
Type=ntainer; // 停靠容器布局需要可停靠窗口
deAnimationTime=0; // 一些控件不支持动画所以关掉它
h=true; // 可停靠窗口拉伸至填补容器
e=true;
ndleStyle=n; // 可停靠窗口有标题
DockItem=new DockContainerItem("leftDockItem3","Left Dock Container line 1");
(DockItem);
// 创建我们想放在可停靠窗口的控件
DockedControl=new UserControl1();
=+" - "+;
lor=hiffon;
l=DockedControl; // 指定该控件是托管在停靠容器上
(bar); // DotNetBar需要添加bar以便能管理它的停靠等
umentUIManager().Dock(bar);
// 设置停靠点的宽度并将bars按比例停靠在里面
// 但是这个尺寸应该大到足以容纳所有的Bar包括约束像MinimumSize等等
= 150;
}
BaseItem::定义了DotNetBar使用的项的基类
示例:
BaseItem item = sender as BaseItem;
4. 添加“菜单项“也可以通过两种方式实现
1) “可视化“的方式
在“设计器“里可以通过”右键“来实现;
2) 程序的方法
private void CreateBar()
{
//创建Bar
Bar bar=new Bar("Standard");
e=true;
=2003;
ndleStyle=Flat;
emsDock=true;
emsFloat=false;
// 向Bar内添加项
ButtonItem item, fileItem;
// 添加新建菜单
item=new ButtonItem("bNew");
ndex=0;
// = [0];
="&New";
();
ry="Standard";
(item);
m_(()); // 这将创建类别条目
// 添加打开菜单
item=new ButtonItem("bOpen");
ndex=1;
="&Open";
();
ry="Standard";
(item);
m_(());
// 添加子菜单项到打开菜单,例如最近使用的文件之类的东西
fileItem=new ButtonItem("file1");
="&1. ";
(fileItem);
fileItem=new ButtonItem("file2");
="&2. ";
(fileItem);
fileItem=new ButtonItem("file3");
="&3. ";
(fileItem);
fileItem=new ButtonItem("file4");
="&4. ";
(fileItem);
fileItem=new ButtonItem("file5");
="&5. ";
(fileItem);
// 添加关闭菜单
item=new ButtonItem("bClose");
ndex=2;
="&Close";
();
ry="Standard";
(item);
m_(());
// 添加保存菜单
item=new ButtonItem("bSave");
ndex=3;
="&Save";
();
ry="Standard";
(item);
m_(());
// 添加打印预览菜单
}
item=new ButtonItem("bPrintPreview");
ndex=6;
="Print Pre&view";
ry="Standard";
roup=true;
(item);
m_(());
// 添加打印菜单
item=new ButtonItem("bPrint");
ndex=5;
="&Print";
ry="Standard";
();
(item);
m_(());
// 添加邮件菜单
item=new ButtonItem("bEmail");
ndex=4;
="&Email";
ry="Standard";
roup=true;
(item);
m_(());
// 添加“添加/删除“按钮
CustomizeItem citem=new CustomizeItem();
(citem);
// 由于我们将使用ImageList,Bar酒吧必须被添加到DotNetBarManager中
// 将Bar加入DotNetBarManager
m_(bar);
de=;
5. 为所有选项添加“事件”
this.m_ick += new andler(mClick);
private void BarItemClick(object sender, EventArgs e)
{
BaseItem item = sender as BaseItem;
if (item == null || == "" || Item)
return;
("Item '" + + "' clicked");
}
6. 设置窗体的大小
AutoScaleBaseSize属性的值在窗体显示时使用,用来计算该窗体的缩放因子。窗体将自动缩放基大小(用作与系统的字体大小进行比较的基准),以确定使用自动缩放时窗体的缩放量。如果要确定根据特定字体窗体将自动缩放到的大小,请使用GetAutoScaleSize方法。
示例:
aleBaseSize = new (11, 17);
该属性将按着Size(a,b)内a与b的比例大小自动调整窗口大小,但是值得注意的是:a与b的绝对值越大,窗口越小。
//设置工作区域的大小
Size = new (351, 207);
四、ImageList组件
1. 概述
ImageList组件就是一个图像列表。一般情况下,这个属性用于存储一个图像集合,这些图像用作工具栏图标或TreeView控件上的图标。许多控件都包含ImageList属性。这个属性一般和ImageIndex属性一起使用。ImageList属性设置为ImageList组件的一个实例,ImageIndex属性设置为ImageList中应在控件中显示的图像的索引。使用属性的Add方法可以把图像添加到ImageList组件中。Images属性返回一个ImageCollection。
两个最常用的属性是ImageSize和ColorDepth。ImageSize使用Size结构作为其值。其默认值是16×16,但可以取1~256之间的任意值。ColorDepth使用ColorDepth枚举作为其值。颜色深度值可以从4位~32位。在.NET Framework 1.1中,默认是8Bit。
2. 示例
private ist imageList1;
tream = ((istStreamer)(ect("tream")));
arentColor = arent;
// 下面加几个Image
Name(0, "");
Name(1, "address ");
Name(2, "adjust ");
//把imageList1赋给treeView1
ndex = 0;
ist = ist1;
on = new (3, 3);
= "treeView1";
edImageIndex = 0;
= new (579, 450);
ex = 0;
// 以上code在Designer完成
// 在添加TreeNode的时候, 把Image加上
// 未选中时,这个node显示imageList1中的"", 选中后显示"address "
TreeNode node = new TreeNode("TreeNodeName", 0, 1);
(node);
All();
3. 讨论
1) ImageList里面的图片的颜色
2) ImageList里面的图片的大小
引起ImageList里面图片颜色失真的原因是在Design-Time就在中往ImageList里面添加了Images。
当用户一边在“Image Collection Editor”对话框里面添加图片,一边就已经把这些图片装载到resource文件里面了。这样,以后程序运行时就只需要访问resource文件就可以载入所有图片而不需要依赖原始的图片文件。
但是问题在于从结果看,当在Design-Time往resource文件里面添加图片时并没有使用用户指定的ColorDepth(例如Depth32Bit),而用了epth的默认值(Depth8Bit)。这样,等程序运行时,即使epth指定了Depth32Bit也无济于事,因为原始的素材本身只有8bit的颜色。这基本上就是失真的问题的原因。
因此,解决方案是:不在Design-Time用往ImageList里面添加图片,而是在程序运行时先指定32Bit的ColorDepth,然后再添加图片,如以下例子代码:
epth=32Bit;
(le(@"C:"));
(le(@"C:Inetpubwwwrootimagesinit_"));
(le(@"C:"));
(le(@"C:"));
这里需要注意的是,必须先指定ColorDepth,然后再添加图片。因为对ColorDepth赋值会清空所有图片。BTW,epth的默认值是Depth8Bit,而非文档上所述Depth4Bit。这一点很容易可以通过写一段例子代码来验证,也可以通过很多Decompiler来查看ImageList的构造函数的实现来验证。
的确,通过[i]获得图片的大小都是统一的,都等于ize。这个问题的原因在于ImageList在返回Images[i]的过程中并没有返回原始的图片,而是按照ize创建了一个新的Bitmap,并把原始图片的内容重新绘制上去以后再返回给用户。关于这一点,可以用一些Decompiler工具如或者Anakrino通过察看私有函数map(int index)来验证。我想现在paulluo0739应该能够理解为什么ImageList里面的图片都是一样大的了。
ImageSize同ColorDepth类似,也不宜在运行时改动,一旦重新赋值,就会清空所有的图片。因此,如果程序运行时需要某一图片的不同大小的版本,可以考虑使用多个不同ImageSize的ImageList。
4. 关于ImageList组件的用法还存在问题
五、控件BubbleBar的使用
1. 使用效果
2. 其实使用很简单,只有将BubbleBar控件拖到窗体上,然后,就可以添加“Tab”和“Button”了。
3. 一些有用的语句
private void bubbleButton_Click(object sender, ventArgs e)
{
Button button=sender as Button;
=+", "+pText;
}
private void checkBox2_CheckedChanged(object sender, rgs e)
{
if(d)
BackgroundStretch=true;
else
BackgroundStretch=false;
// Apply UI changes and refresh
//刷新
Layout();
h();
}
private void numericUpDown1_ValueChanged(object sender, rgs e)
{
// 图像分配给控件时总是匹配最佳外观的尺寸是最好的
izeNormal=new Size((int),(int));
Layout();
h();
}
六、控件DataGridViewX的使用
1. 说明
(1)
(2)
(3)
(4)
控件DataGridView与DataGridView的基本用法类似。
DataGridView有自动按列排序的功能。
。
edRows[0].Cells[0].ng()。
2. DataGridView的示例代码
#region 设置DataGridView的显示格式
zeColumnsMode = ;
nerateColumns = false;
serToAddRows = false;
serToResizeColumns = true;
//////////////////////////////////////////////////////
TabControlPanel tcp;
foreach (Control c in ls)
if (c is TabControlPanel)
{
tcp = (TabControlPanel)c;
= gb(227, 239, 255);
= gb(175, 210, 255);
foreach (Control ctrl in ls)
{
if (ctrl is idViewX)
{
idViewX dgv = (idViewX)ctrl;
nerateColumns = false;
oundColor = gb(227, 239, 255);
serToAddRows = false;
serToDeleteRows = false;
//ze = false;
zeColumnsMode = ;
zeRowsMode = idViewAut
ls;
serToResizeColumns = true;
//dersWidth = 900;
elect = false;
ly = true;
dersVisible = false;
Bars = ;
ionMode = wSelect;
}
}
}
();
();
();
();
();
();
();
("HID", "硬件ID号");
—————————————————图片示例——————————————————
("HType", "硬件类型");
("HDetails", "硬件信息");
("MID", "所属机器ID号");
("IP", "所属机器的IP地址");
foreach (DataGridViewColumn col in s)
opertyName = ;
—————————————————图片示例——————————————————
("MID", "机器ID");
("MName", "机器名");
("UID", "用户ID");
s["UID"].Visible = false;
("UName", "用户名");
("IP", "IP地址");
("DeptID", "DeptID");
s["DeptID"].Visible = false;
("Department", "所属部门");
foreach (DataGridViewColumn col in s)
opertyName = ;
urce = Machine( , );
// 怎样获得鼠标右击处的行
tInfo hittestinfo = t(e.X, e.Y);
[ex].Selected = true;
七、控件Color Picker的使用
1. 使用效果:
1) 单击相应的按钮,工作区(整体)的颜色发生变化。
2) 随着鼠标在各种颜色上的移动,工作区(整体)的颜色随着变化。
3) 还可以选择“More Colors”进一步改变工作区的颜色。
2. 下面是相应的代码
private bool m_ColorSelected = false;
private eOffice2007ColorScheme m_BaseColorScheme = ;
// 预览,随着鼠标在各种颜色上的移动,工作区(整体)的颜色随着变
private void colorPickerDropDown1_ColorPreview(object sender, ColorPreviewEventArgs e)
{
Office2007ColorTable(m_BaseColorSche
me, );
}
//在colorPickerDropDown1上选择颜色后,工作区(整体)的颜色随着变
private void colorPickerDropDown1_ExpandChange(object sender, EventArgs e)
//扩展区域弹出后,所激发的事件
{
if (ed)
{
// 刚弹出,则初始化一个颜色值
// Remember the starting color scheme to apply if no color is selected during live-preview
m_ColorSelected = false;
m_BaseColorScheme = ((Office2007Renderer)er).lColorScheme;
}
else //否则,是“隐去”
{
if (!m_ColorSelected)
{
Office2007ColorTable(m_BaseColorScheme);
}
}
}
// 选择颜色后,发生的事件
private void colorPickerDropDown1_SelectedColorChanged(object sender, EventArgs e)
{
m_ColorSelected = true; // Indicate that color was selected for buttonStyleCustom_ExpandChange method
Office2007ColorTable(m_BaseColorScheme, edColor);
}
3. 说明
1) 初始化一个颜色值
m_BaseColorScheme = ((Office2007Renderer)er).lColorScheme;
2) 改变颜色值
Office2007ColorTable(m_BaseColorScheme,
edColor);
3) 应用颜色值
Office2007ColorTable(m_BaseColorScheme);
4. 工作区的控件的颜色有可能不同时变化
原因:是各个控件的“Style”和“ColorSchemeStyle”采用的风格不统一
解决办法:都采用一种风格,如“Office2007”即可
八、控件SideBar的使用
1. 功能
可实现如下图似的的分组:
2. 实现方式
1) 右键——>“Add Panel”,进行“分组”。
2) 对每一个组进行设置。
3) 右键,对每一个组,添加控件。
3. 该控件的颜色策略
——arColorScheme
scheme=(arColorScheme)(typeof(arColorScheme),sSel,false);
inedColorScheme=scheme;
private void sideBar1_ItemClick(object sender, rgs e)
{
em item=sender as em;
="ItemClick: "+ +" ("++")n"+;
}
九、Enum枚举类
1. erlyingType 方法
--返回指定枚举的基础类型。
- public static Type GetUnderlyingType(Type enumType)
参数:
enumType
类型:枚举类型。
返回值:
类型: enumType的基础Type。
--示例
static object GetAsUnderlyingType(Enum enval)
{
Type entype = e();
Type undertype = erlyingType(entype);
return Type( enval, undertype );
}
2. e 方法
--在指定枚举中检索具有指定值的常数的名称。
- public static string GetName( Type enumType, Object value)
参数:
enumType
类型:
枚举类型。
value
类型:
特定枚举常数的值(根据其基础类型)。
返回值:
类型:
一个字符串,该字符串包含enumType 的枚举常数的名称,该常数的值为value;或者,如果没有找到这样的常数,则为nullNothingnullptrnull引用(在Visual Basic中为Nothing)。
--示例
using System;
public class GetNameTest
{
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static void Main()
{
ine("The 4th value of the Colors Enum is {0}",
e(typeof(Colors), 3));
ine("The 4th value of the Styles Enum is {0}", e(typeof(Styles), 3));
}
}
3. es 方法
--检索指定枚举中常数名称的数组。
--public static string[] GetNames( Type enumType)
参数:
enumType
类型:System..::.Type
枚举类型。
返回值:
类型:array<>[]()[]
enumType
的常数名称的字符串数组。
--示例
using System;
public class GetNamesTest
{
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static void Main()
{
ine("The values of the Colors Enum are:");
foreach(string s in es(typeof(Colors)))
ine(s);
ine();
ine("The values of the Styles Enum are:");
foreach(string s in es(typeof(Styles)))
ine(s);
}
}
4. ues 方法
--检索指定枚举中常数值的数组。
--public static Array GetValues( Type enumType)
参数:
enumType
类型:
枚举类型。
返回值:
类型:
enumType 的常数值的 Array。该数组的元素按枚举常数的二进制值排序。
--示例
using System;
public class GetValuesTest
{
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 };
public static void Main()
{
ine("The values of the Colors Enum are:");
foreach(int i in ues(typeof(Colors)))
ine(i);
ine();
ine("The values of the Styles Enum are:");
foreach(int i in ues(typeof(Styles)))
ine(i);
}
}
// The example produces the following output:
// The values of the Colors Enum are:
// 0
// 1
// 2
// 3
//
// The values of the Styles Enum are:
// 0
// 23
// 65
// 78
5. Enum..::.Parse 方法
--将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。
名称
Parse(Type,
String)
说明
将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。
String包含要转换的值或名称的字符串。
备注:一个已命名常数或由逗号 (,value
参数中包含一个值、)
分隔的已命名常数列表。在 value
中的每个值、名称或逗号的前面或后面都可以有一个或多个空格。如果 value
是一个列表,则返回值是各指定名称通过按位“或”运算结合而成的值。
示例:
[FlagsAttribute]
enum Colors
{ Red = 1, Green = 2, Blue = 4, Yellow = 8 };
ine("The entries of the Colors Enum are:");
foreach (string colorName in es(typeof(Colors)))
{
ine(
"{0}={1}", colorName
,
32((typeof(Colors), colorName))
);
}
ine();
Colors myOrange = (Colors)(typeof(Colors),
"Red, Yellow");
ine("The myOrange value {1} has the combined entries of {0}",
myOrange, 64(myOrange));
Parse(Type,
String, Boolean)
将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。一个参数指定该操作是否区分大小写。
Boolean参数指定该操作是否区分大小写
1)
示例:
es(typeof(arColorScheme))
(arColorScheme)(typeof(arColorScheme),sSel,false)
十、Money Style
1. 示例代码1
// File
menu=new ButtonItem("mFile","&File");
(menu);
// New
menuItem=new ButtonItem("mNew","New");
(menuItem);
// Open
menuItem=new ButtonItem("mOpen","&Open");
();
(menuItem);
// Password manager
menuItem=new ButtonItem("mPasswordManager","Password Manager");
roup=true;// 在该按钮前加一条线
(menuItem);
// Backup
menuItem=new ButtonItem("mBackup","&");
roup=true;// 在该按钮前加一条线
(menuItem);
// Restore
menuItem=new ButtonItem("mRestore","&");
(menuItem);
// Archive
menuItem=new ButtonItem("mArchive","&");
(menuItem);
// Exit
menuItem=new ButtonItem("mExit","E&xit");
roup=true;
(menuItem);
// 将新建的Bar添加到dotNetBarManager1
(bar);
// 然后,设置摆放位置
de=;
2. 示例代码2
// 标准工具栏Standard Toolbar
bar=new Bar("Standard");
// 当被添加到集合里时,风格匹配将被设置在Bar里的其他风格是重要的
=;
="barStandard";
h=true;
uttonSize=true;
yMoreItemsOnMenu=true;
oundImage=new Bitmap(e(),"");
oundImagePosition=;// 平铺覆盖
SetToolbarColorScheme(cheme);
// 必须添加bar到bars集合里,以便DotNetBarManager能知道它
(bar);
// 设置最初停靠线
ne=1;
// 当DotNetBarManager知道它后,我们可以将它停靠在适当的一边
de=;
// Account List
(CreateToolbarButton("bAccountList","Account List","",""));
// Portofolio
(CreateToolbarButton("bPortofolio","Portofolio","",""));
// Bills and Deposits
(CreateToolbarButton("bBills","Bills && Deposits","",""));
// Reports
(CreateToolbarButton("bReports","Reports","",""));
// Cash Flow
(CreateToolbarButton("bCashFlow","Cash Flow","",""));
// Budget
(CreateToolbarButton("bBudget","Budget","",""));
private void SetToolbarColorScheme(ColorScheme scheme)
{
// 设置工具栏自定义配色方案……
xt=;
tText=gb(247,222,156);
essedText=gb(247,222,156);
// 这部分的颜色方案变化仅用于溢出项即如果他们不能安装在工具栏显示那他们不能在菜单中显示
tBackground=gb(80,80,80);
tBackground2=;
tBorder=gb(80,80,80);
essedBackground=gb(91,91,91);
essedBackground2=;
essedBorder=gb(91,91,91);
pandedText=gb(247,222,156);
pandedBackground=gb(91,91,91);
pandedBackground2=;
pandedShadow=;
ckground=gb(91,91,91);
ckground2=;
rBackground=gb(91,91,91);
rBackground2=;
rder=;
de=gb(91,91,91);
de2=;
}
private ButtonItem CreateToolbarButton(string name, string text, string imageName, string hotImageName)
{
ButtonItem button=new ButtonItem(name,text);
=new Bitmap(e(),imageName);
mage=new Bitmap(e(),hotImageName);
dImage=mage;
ckingStyle=;
Style=ndText;
osition=;
=;
return button;
}
十一、控件PanelEx的使用
(1)下图中的六部分都是PanelEx。
(2)PanelEx有如上图的几种风格。
(3)PanelEx在“Text”属性里,可以使用Html语言,可以加入超链接,只是只能链接到某个函数名,注意的是:必须将该函数,在“属性”—“事件”选项卡中的“MarkupLinkClick”事件中,进行关联。
界面:
1. 弹出式菜单
设置该图左边图的代码如下:
// 确定:在side-bar中使用的图都是可用的,
// 另外,确定,图的大小要一定小于Side-Bar的高度
rImage si=new rImage();
e=new Bitmap(typeof(1),"");
// If image exceeds the size of the popup menu this specifies the image alignment
ent=;
// If there is no image specified gradient can be used
ntColor1=;
ntColor2=;
m_ideBar=si;
2. 弹出式工具栏
--1和2创建按钮(菜单选项)时,所用的函数一样,所用的风格一样,只是在最后弹出菜单时,使用的函数不一样,才导致的外观不一样。
--这是Menu的风格设置。
// 设置风格
tBarStyle style = 2003;
if(((tem)edItem).Text != "Office2003")
style=2005;
m_=style;
// 如果弹出式菜单不属于ContextMenu(即没有在属性“contextMenuBar1上的ContextMenuEx”上进行相关设置)的话,该弹出式菜单必须在DotNetBar上进行注册
erPopup(m_PopupFromCode);
// 把菜单放在按钮下面
Control ctrl=sender as Control;
Point p=oScreen(new Point(,));
// 下面这两句才是弹出菜单显示效果不一样的原因
m_enu(p);
m_ar(p);
--下面是Bar的风格设置
3. 右键菜单
要想加入右键菜单,需要:
1) 加入控件“ContextMenuBar”。
2) 在“ContextMenuBar”上加入所需的右键菜单。
示例:在“ContextMenuBar”上,右键,选择“Add Context Menu”,出现“ButtomItem1”,设置其“Name”属性,然后在该ButtomItem1上,单击右键,添加一系列button,这才是具体的右键菜单。
3) 在要加入右键菜单的控件上,设置属性“contextMenuBar1上的ContextMenuEx”,使该属性值与“右键菜单”ButtomItem1相关联即可。
4) ContextMenuEx弹出菜单时,激活的事件是PopupOpen,可以对弹出的菜单进行相应的设置。
十二、控件ItemPanel和控件ItemContainer的使用
1. 示例的使用效果
控件ItemPanel和控件ItemContainer都是用来放其他控件的容器,只是ItemPanel只能纵向放置,而ItemContainer都可以。
--示例
private void itemPanel2_ItemClick(object sender, rgs e)
{
if(sender is ButtonItem)
{
// 因为图表选择器在不同的容器里,Group的设置将不会取消选择
// 按钮来自另一个容器,所以我们必须做
ButtonItem button = GetCheckedButton(, sender as ButtonItem);
if(button != null)
d = false;
}
}
private ButtonItem GetCheckedButton(SubItemsCollection parentCollection, ButtonItem exclude)
{
foreach(BaseItem item in parentCollection)
{
if(item is ButtonItem && item != exclude && ((ButtonItem)item).Checked)
return item as ButtonItem;
else if(ainer)
{
ButtonItem button = GetCheckedButton(ms, exclude);
if(button != null)
return button;
}
}
return null;
}
private void columnContainer_Click(object sender, EventArgs e)
{
Visible( columnSelector );
d = true;
}
十三、控件ListViewEx的使用
1. 示例的使用效果
其中:
1) 标题头部分通过属性“Columns”设置
2) 内容部分
前面有图标部分,通过“Items”属性设置
后面的几列,通过“Items”的“SubItems”属性设置
3)
可以通过设置该属性,可以使实现各种排列方式。
十四、控件SuperToolTip的使用
1. 示例的使用效果
2. 用法:
只是将控件Super Tool Bar添加到工程中,然后,再在要加入上述效果的控件的属性中,找到“superTooltip上的SuperTooltip”,单击。
随后,会出现如上图所示的对话框,然后,就可以根据该对话框上的提示进行操作。
值得注意的是:这里也可以使用Html语言,使用超链接(实际的效果类似于LinkButton控件),为该控件添加更多的功能。
只不过的是:需要在SuperToolTip控件的事件进行必要的设置。
通过程序为控件设置“superTooltip上的SuperTooltip”所应该设置的内容,下面以为TreeView控件的node节点进行相关设置进行说明:
1) 首先,定义了一个NodeSuperTooltipProvider类,为每一个node对象,提供SuperTooltip功能
///
/// 包装,使得SuperTooltips能为节点对象显示
///
public class NodeSuperTooltipProvider : Component, TooltipInfoProvider
{
private TreeNode m_Node=null;
public NodeSuperTooltipProvider(TreeNode node)
{
m_Node=node;
}
// 为给定的node显示tooltip
public void Show()
{
if(yTooltip != null)
DisplayTooltip(this,new EventArgs());
}
// 为给定的node隐藏tooltip
public void Hide()
{
if(oltip != null)
oltip(this,new EventArgs());
}
#region ISuperTooltipInfoProvider Members
// 返回node对象的屏幕坐标
public gle ComponentRectangle
{
get
{
Rectangle r=m_;
on=m_oScreen(on);
return r;
}
}
//下面的事件是程序自动提供的
public event EventHandler DisplayTooltip;
public event EventHandler HideTooltip;
#endregion
}
2) 为树TreeView的每一个node节点添加ToolTip
// 为每个节点加载SuperTooltip信息
TreeNode node=[0];
while(node != null)
{
// 创建包装器提供SuperTooltip控件访问节点对象
NodeSuperTooltipProvider sp=new NodeSuperTooltipProvider(node);
=sp;
// 将包装器连同关于如何显示在SuperTooltip的这个节点的信息分配到SuperTooltip
控件
erTooltip(sp,new ooltipInfo("Header text for "+,"","Additional body text for this node. nr"+,null,null,));
// 必须展开节点用于通过NextVisibleNode获得子节点
();
node=sibleNode;
}
3) 和TreeView相关的一些操作
private TreeNode m_LastMouseOverNode=null;
private void treeView1_MouseMove(object sender, ventArgs e)
{
TreeNode nodeAt=eAt(e.X,e.Y);
if(nodeAt != m_LastMouseOverNode)
{
HideNodeTooltip();
if( nodeAt != null)
{
m_LastMouseOverNode=nodeAt;
// 延迟显示
();
}
}
}
private void ShowNodeTooltip(TreeNode node)
{
if(node==null)
return;
NodeSuperTooltipProvider sp= as NodeSuperTooltipProvider;
();
m_LastMouseOverNode=node;
}
private void treeView1_MouseLeave(object sender, rgs e)
{
// 当鼠标离开树控件时隐藏tooltip
HideNodeTooltip();
();
}
private void HideNodeTooltip()
{
if(m_LastMouseOverNode != null)
{
NodeSuperTooltipProvider sp=m_ as NodeSuperTooltipProvider;
();
m_LastMouseOverNode=null;
}
}
private void tooltipDisplayDelay_Tick(object sender, rgs e)
{
();
if(m_LastMouseOverNode != null)
ShowNodeTooltip(m_LastMouseOverNode);
}
private void treeView1_MouseDown(object sender, ventArgs e)
{
// 当任何都是可见时隐藏tooltip
HideNodeTooltip();
}
private void superTooltipWithLink_MarkupLinkClick(object sender, LinkClickEventArgs e)
{
("Markup Link Clicked. href = " + );
}
十五、控件expandableSplitter和控件TabControl的使用
1. expandableSplitter控件的使用示例
控件expandableSplitter将容器的可显示区域分成两个可以调整大小的区域。
建议用法:
1)从左到右,按顺序,一次,拖入如下控件,某个容器控件(Dock设置为Left),ExpandableSplitter控件(Dock设置为Left)以及另一个容器控件(Dock设置为Fill)
2)如果想实现“在控件expandableSplitter 的SplitterBar上单击一下,也能将该控件控制的区域收起或放开“的功能,可在属性中设置。
注意的是:在选项中,选择那个控件,那在单击“SplitterBar“时,就收起或打开哪个控件。
3)从网上下载的相关资料
DotNetBar中ExpandableSplitter使用技巧
为了实现如下图1到图2效果,用ExpandableSplitter很方便,设置ExpandableSplitter的ExpandedControl属性为panelEx_Right即可。
但是想实现图3的效果,本以为用这个控件加上Form的Autosize属性即可。没想搞了半天发现不管是Form用Autosize让panel_right的Dock为Fill,还是panel_right锚定都无法实现这个效果。
图1
图2
仔细想想,Autosize肯定不行,其和Dock在实现上就是是矛盾的,呵呵。
真是懒习惯了,动手加下边几句就可以解决,在这里记录一下。
public partial class Test : Form
{
// 记录右侧Panel宽度
private int m_RightWidth = 0;
public Test()
{
InitializeComponent();
m_RightWidth = panelEx_;
}
private void expandableSplitter_Click(object sender, EventArgs e)
{
if (ed)
{
m_RightWidth = panelEx_;
-= m_RightWidth;
}
else
{
+= m_RightWidth;
}
}
}
图3
另外记得设置MinExtra属性为0,不然ExpandableSplitter拖不到最边上。
2. 关于控件TabControl的比较高级的用法示例
private void itemPanel1_ItemClick(object sender, rgs e)
{
ButtonItem button = sender as ButtonItem;
if(button==null)
return;
eTabStripStyle style = (eTabStripStyle)(typeof(eTabStripStyle), );
SetTabStyle(style);
Layout();
}
private void ChangeTabColor(object sender, EventArgs e)
{
if(m_TabMouseDown != null)
{
m_inedColor=(eTabItemColor)(typeof(eTabItemColor),((BaseItem)sender).Text);
}
}
private void SetTabStyle(eTabStripStyle style)
{
=style;
foreach(TabItem tab in )
inedColor=t;
}
private void tabControl1_MouseDown(object sender, ventArgs e)
{
if( != )
return;
// 转换到上下文菜单的屏幕坐标
Point p=new Point(e.X,e.Y);
Point pScreen=oScreen(p);
TabItem tab=t(p.X,p.Y);
if(tab != null)
{
m_TabMouseDown=tab;
// 创建右键菜单
CreateContextMenu();
ButtonItem item=["tabColors"] as ButtonItem;
// 弹出右键菜单
(pScreen);
}
}
private void CreateContextMenu()
{
if(ns("tabColors"))
return;
ButtonItem item=new ButtonItem("tabColors","Tab Colors Context menu");
=2003;
(item);
// 为eTabItemColor的每一个实体创建一个菜单项
string[] names=es(typeof(eTabItemColor));
foreach(string s in names)
{
ButtonItem menuItem=new ButtonItem(s,s);
// 构建菜单项的图像
TabItem tabTemp=new TabItem();
inedColor=(eTabItemColor)(typeof(eTabItemColor),s);
Bitmap bmp=new Bitmap(16,16,
at24bppRgb);
Graphics g=age(bmp);
try
{
(l);
Rectangle r=new Rectangle(1,1,14,14);
using(GradientBrush brush=new GradientBrush(r,lor,lor2,lorGradientAngle))ctangle(brush,r);
using(Pen pen=new Pen(ay,1))ctangle(pen,r);
}
finally
{
e();
}
ansparent(l);
=bmp;
+=new EventHandler(TabColor);
(menuItem);
}
}
十六、控件Reflection的使用(倒影)
Image与控件Reflection Label可生成如下图所示的效果
十七、控件MonthCalendarAdv的使用
下面两个图的区别,产生于属性“Dimension”。
十八、控件MonthCalendarAdv的特殊使用
1. 使用效果
1) 正常情况,即没有特殊设置时
2) 进行相应设置后
3) 当然,这主要指的是周末的日期上的红叉“4) 该功能和控件的
事件相关,下面是实现上述功能的具体代码:
private void monthCalendarAdv2_PaintLabel(object sender, DayPaintEventArgs e)
{
// 叉掉星期天和禁用选择
el day = sender as el;
if (day != null && > ue && eek ==
)
{
”,及不能被选择
able = false; // 标记标签为不能选择
ouse = false; // 不跟踪鼠标移动
ackground();
ext();
Rectangle r = ;
e(-2, -2);
ingMode sm = ingMode;
ingMode = ias;
using (Pen pen = new Pen(gb(128, )))
{
ne(pen, r.X, r.Y, , );
ne(pen, , r.Y, r.X, );
}
ingMode = sm;
// 确保没有呈现在控件内部的部件
Parts = ;
}
}
private void monthCalendarAdv2_MonthChanged(object sender, EventArgs e)
{
// 你可以使用这个事件来添加定制的标记到calendar上,例如为某一天做预约
// 在本例中,我们只会选择从今天开始的8天
DateTime date = s(8);
// DayLabel控件是用来表示日历上的每个日期
el day = (date);
if (day != null) // 如果日期没有显示在日历上则返回Null
{
// 在Label上显示日期
= global::;
lign = Right;
ign = Left;
p = "Click image to see appointments";
// 当某天的图像被点击时,你可以显示例如预约细节的弹出框
(new ButtonItem("appointment1", "10:45 AM Call Dave to
setup meeting"));
(new ButtonItem("appointment2", "12:05 PM Lunch with Kurt"));
}
}
十九、控件DataTimeInput的使用
1) 可产生如下图所示的效果
2) 该图右上角的的出现与否取决于下面几个属性
3) 关于格式的控制
主要与属性“Format”有关,其有如下几个选项:short、long、shorttime、longtime与custom等。
注意:当使用“custom”自主控制格式时,要与属性“customFormat”配合使用。
4) 在输入框中是否有复选框,与属性“ShowCheckBox”有关
5) 输入框复选框是否处于“被选择(激活)”状态,与属性“LockUpdateChecked”
有关
6) 至于,就相当于一个按钮(Buttom控件),可以添加一些特殊控制的代码。
7) 控件DataTimeInput的
事件集是很重要的,其中
是个重要的事件。
示例程序:
private void dateTimeInput11_MonthCalendar_PaintLabel(object sender, ntEventArgs e)
{
el day = sender as el;
if (day == null || == ue)
return;
// 叉掉所有周末并禁用选择
if ((eek == ay || eek == ) && != s(1))
{
able = false; // 标记标签为不能选择
ouse = false; // 不跟踪鼠标移动
ackground();
ext();
Rectangle r = ;
e(-2, -2);
ingMode sm = ingMode;
ingMode = ias;
ne(, r.X, r.Y, , );
ne(, , r.Y, r.X, );
ingMode = sm;
// 确保没有呈现在控件内部的部件
Parts = ;
}
// 标识明天
else if (day != null && == s(1))
{
ackground();
ext();
ingMode sm = ingMode;
ingMode = ias;
Rectangle r = ;
--;
--;
using(Pen pen = new Pen(gb(128, )))
lipse(pen, r);
ingMode = sm;
// 确保没有呈现在控件内部的部件
Parts = ;
}
}
8) 用户定义的日期/时间格式(Format 函数)
C#时间格式
有时候我们要对时间进行转换,达到不同的显示效果。
默认格式为:2005-6-6 14:33:34
如果要换成200506,06-2005,2005-6-6或更多的该怎么办呢?
我们要用到:ng的方法(String, IFormatProvider)
using System;
using ization;
String Format="D";
DateTime date=;
(ng(Format, antInfo));
结果输出:
Thursday, June 16, 2005
参数Format格式详细用法
格式字符 关联属性/说明
d ShortDatePattern
D LongDatePattern
f 完整日期和时间(长日期和短时间)
F FullDateTimePattern(长日期和长时间)
g 常规(短日期和短时间)
G 常规(短日期和长时间)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用当地时间的SortableDateTimePattern(基于 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern用于显示通用时间的格式
U 使用通用时间的完整日期和时间(长日期和长时间)
y、Y YearMonthPattern
下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的,如识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。
格式模式 说明
d 月中的某一天。一位数的日期没有前导零。
dd 月中的某一天。一位数的日期有一个前导零。
ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd 周中某天的完整名称,在 DayNames 中定义。
M 月份数字。一位数的月份没有前导零。
MM 月份数字。一位数的月份有一个前导零。
MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM 月份的完整名称,在 MonthNames 中定义。
y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy 包括纪元的四位数的年份。
gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h 12 小时制的小时。一位数的小时数没有前导零。
hh 12 小时制的小时。一位数的小时数有前导零。
H 24 小时制的小时。一位数的小时数没有前导零。
HH 24 小时制的小时。一位数的小时数有前导零。
m 分钟。一位数的分钟数没有前导零。
mm 分钟。一位数的分钟数有一个前导零。
s 秒。一位数的秒数没有前导零。
ss 秒。一位数的秒数有一个前导零。
f 秒的小数精度为一位。其余数字被截断。
ff 秒的小数精度为两位。其余数字被截断。
fff 秒的小数精度为三位。其余数字被截断。
ffff 秒的小数精度为四位。其余数字被截断。
fffff 秒的小数精度为五位。其余数字被截断。
ffffff 秒的小数精度为六位。其余数字被截断。
fffffff 秒的小数精度为七位。其余数字被截断。
t 在AMDesignator或PMDesignator中定义的AM/PM指示项的第一个字符(如果存在)。
tt 在AMDesignator或PMDesignator中定义的AM/PM指示项(如果存在)。
z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
: 在TimeSeparator中定义的默认时间分隔符。
/ 在DateSeparator中定义的默认日期分隔符。
% c 其中c是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
c 其中c是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“”。
只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如:
ng( "d")返回DateTime值;“d”是标准短日期模式。
ng( "%d")返回月中的某天;“%d”是自定义模式。
ng( "d ")返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。
比较方便的是,上面的参数可以随意组合,并且不会出错,多试试,肯定会找到你要的时间格式。
如要得到“2005年06月”这样格式的时间。
二十、IntegerInput控件和DoubleInput控件的用法类似
(1)关于格式的控制,这两个控件有好多地方与DateTimeInput控件相似,这里介绍一个不同的。
控件激活前是这样的,但是,当控件激活后,就变成这样的,然后,就和其他设置的IntegerInput控件的用法一致了。
(2)图标表示“自增/自减”,与属性有关,而增量与属性有关。
(3)与增量有关:怎么实现任意步长?
(4)数字的格式化
1) 内置类型的字母格式
有一种格式命令以单个字母开头,表示下列设置:
G——常规,E或F中较短的
F——浮点数,常规表示法
E——用E表示法表示的浮点数(其中,E代表10的次幂)
N——带有分隔符的浮点数(在美国为逗号)
C——货币,带有货币符号和分隔符(在美国为逗号)
D——十进制数,仅用于整型
X——十六进制数,仅用于整型
字母可以后跟一个数字,根据字母的不同,该数字可以表示总位数、有效位数或小数点后面的位数。
2) 下面是字母格式的一些示例:
double pi = ;
double p0 = pi * 10000;
int i = 123;
ine("浮点格式,无分隔符(逗号)");
ine("pi, Left {0, -25}", pi); // 3.97931
ine("p0, Rt. {0, 25}", p0); // 3.97931
ine("pi, E {0, 25:E}", pi); // 3.1416E+000
ine("使用 E 和 F 格式,小数点后保留 n(此处为 4)位");
ine("pi, E4 {0, 25:E4}", pi); // 3.1416E+000
ine("pi, F4 {0, 25:F4}", pi); // 3.1416
ine("使用 G 格式,保留 4 位有效数字——如果需要请使用 E 格式");
ine("pi, G4 {0, 25:G4}", pi); // 3.142
ine("p0, G4 {0, 25:G4}", p0); // 3.142E4
ine("N 和 C 格式带有逗号(分隔符)," +
"默认小数点后保留两位,四舍五入。");
ine("p0, N {0, 25:N}", p0); // 31,415.93
ine("p0, N4 {0, 25:N4}", p0); // 31,415.9265
ine("p0, C {0,25:C}", pi); // $3.14
ine("D 和 X 格式仅用于整型," +
"非整型将产生格式异常——X 指十六进制");
ine("i, D {0, 25:D}", i ); // 123
ine("i, D7 {0, 25:D7}", i ); // 0000123
ine("i, X {0, 25:X}", i ); // 7B
ine("i, X8 {0, 25:X8}", i ); // 0000007B
图片格式
与字母格式不同,Formatstring可以包含“图片格式”。下面是从代码中摘录的几个实例。(这类似于Basic中的“Print Using”语句。)图片格式功能甚至包括以不同方式设置负数、正数和零的格式的能力。还有几个图片格式功能,下面的示例中未包括在内。有关详细信息,请参阅“.NET框架开发人员指南”或文档中的主题图片格式数字串(英文)。


发布评论