分享

Visual?C#?windows窗体示例主题(二)(MSDN整理)

 悟静 2012-08-22
21.代码:在 StatusBar 控件中显示时间 (Visual C#)

本示例在 StatusBar 控件中以 hh:mm 的格式显示当前时间。

示例:

private void timer1_Tick(object sender, System.EventArgs e)
{
    statusBar1.Panels[0].Text = DateTime.Now.ToShortTimeString();
}

编译代码-----》窗体必须含有一个 Enabled 设为 TrueTimer 控件,和一个 Panels 属性中添加有 PanelShowPanels 属性设为 TrueStatusBar

        -----》如上所示,代码是计时器控件的 Tick 事件的事件处理程序的一部分。

22.代码:在 Windows 窗体上绘制图形 (Visual C#)

本示例在窗体上画一个圆和一个正方形。

示例:

System.Drawing.Graphics graphics = this.CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);

编译代码-----》这些代码添加到从 System.Windows.Forms.Form 派生的类中。“this”引用窗体的实例。

23.代码:将 RichTextBox 控件中的字符格式化为粗体 (Visual C#)

本示例在一个现有 RichTextBox 控件中添加以下文本:“This text is in bold.”。

示例:

richTextBox1.Rtf = @"{\rtf1\ansi This is in \b bold\b0.}";

编译代码-----》本示例需要:一个名为 richTextBox1 的 RichTextBox 控件。

24.代码:从其他窗体中获取值 (Visual C#)

本示例从 Windows 窗体上的一个文本框中检索值,然后在另一个窗体上的文本框中显示该值。

示例:

在 Form1.cs 中:

private Form2 otherForm;
private void GetOtherFormTextBox()
{
    textBox1.Text = otherForm.TextBox1.Text;
}

在 Form2.cs 中:

public TextBox TextBox1
{
    get
    {
        return textBox1;
    }
}

编译代码-----》本示例需要:名称分别为 Form1 和 Form2 的两个窗体,每个窗体都包含一个名为 textBox1 的 TextBox 控件。Form1 创建 Form2 的一个实例并将其分配给 otherForm;而 GetOtherFormTextBox 将 Form2 的 textBox1 中的文本复制到 Form1 的 textBox1 中。

25.代码:隐藏 DataGrid 中 DataSource 为 DataTable 的 DataColumn (Visual C#)

本示例隐藏在现有 Windows 窗体 DataGrid 控件中显示的 DataTable 对象的“X”列。

示例:

private void HideColumnOfDataSet()
{
    System.Data.DataTable points = new System.Data.DataTable("Points");
    points.Columns.Add(new DataColumn("X", typeof(int)));
    points.Columns.Add(new DataColumn("Y", typeof(int)));
    points.Rows.Add(new object[]{1, 2});
    points.Rows.Add(new object[]{3, 5});
    dataGrid1.DataSource = points;

    DataGridTableStyle tableStyle = new DataGridTableStyle();
    tableStyle.MappingName = "Points";
    dataGrid1.TableStyles.Add(tableStyle);
    dataGrid1.TableStyles["Points"].GridColumnStyles["X"].Width = 0;
}

编译代码-----》本示例需要:具有名为 dataGrid1 的 DataGrid 控件的 Windows 窗体。

        -----》如果数据源是 DataSet 对象,则将 DataGridDataMember 属性设置为该表的名称。

        -----》类型化数据集中的 DataTableDataColumn 对象还具有字符串类型的名称。若要查找表的名称,请查看表的 Name 属性。若要查找 DataColumn 的名称,请查看列的 Name 属性。

26.代码:隐藏 DataGrid 中 DataSource 为数组的 DataColumn (Visual C#)

本示例隐藏 DataGrid 中将 Point 对象的数组用作数据源的“X”列。

示例:

private void HideColumnOfArray()
{
    System.Drawing.Point [] points = { new Point(1, 2), new Point(3, 5), new Point(5, 6)};
    dataGrid1.DataSource = points;

    DataGridTableStyle tableStyle = new DataGridTableStyle();
    tableStyle.MappingName = "Point[]";
    dataGrid1.TableStyles.Add(tableStyle);
    dataGrid1.TableStyles["Point[]"].GridColumnStyles["X"].Width = 0;
}

编译代码-----》本示例需要:具有名为 dataGrid1 的 DataGrid 控件的 Windows 窗体。

        -----》字符串 MappingName 是使用“[]”后缀的数组类型。因此,如果数据源是一组 Point 对象,则映射名称为“Point[]”。

27.代码:隐藏 DataGrid 中 DataSource 为 ArrayList 的 DataColumn (Visual C#)

本示例隐藏 DataGrid 中将 Point 对象的 ArrayList 用作数据源的一列。

示例:

private void HideColumnOfArrayList()
{
    System.Collections.ArrayList list = new System.Collections.ArrayList();
    list.AddRange( new Point[]{ new Point(1, 2), new Point(3, 5), new Point(5, 6) } );
    dataGrid1.DataSource = list;
    DataGridTableStyle tableStyle = new DataGridTableStyle();
    tableStyle.MappingName = "ArrayList";
    dataGrid1.TableStyles.Add(tableStyle);
    object o = dataGrid1.TableStyles["ArrayList"];
    dataGrid1.TableStyles["ArrayList"].GridColumnStyles["X"].Width = 0;
}

编译代码-----》本示例需要:具有名为 dataGrid1 的 DataGrid 控件的 Windows 窗体。

        -----》当数据源是 ArrayList 对象时,MappingName 为“ArrayList”。

28.代码:用字符串数组填充 ListBox 控件 (Visual C#)

本示例将字符串数组添加到 Windows 窗体 ListBox 控件中。

示例:
private void Form1_Load(object sender, System.EventArgs e)
{
    string [] myList = new string[4];
    myList[0] = "One";
    myList[1] = "Two";
    myList[2] = "Three";
    myList[3] = "Four";
    listBox1.Items.AddRange(myList);
}

编译代码-----》本示例需要:具有名为 listBox1 的 ListBox 控件的窗体 Form1。将 Form1 的 Load 事件处理程序设置为 Form1_Load。

       注意   本示例还可以使用 ComboBox 控件,方法是用名为 comboBox1 的 ComboBox 控件替换 ListBox 控件,然后将代码由 listBox1 更改为 comboBox1。

29.代码:检索作为嵌入资源的图像 (Visual C#)

本示例检索一个图像,该图像是程序集的嵌入资源。

示例:

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file =
    thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
this.pictureBox1.Image = Image.FromStream(file);

编译代码-----》本示例需要:一个含有名为 pictureBox1 的 PictureBox 控件的 Windows 窗体。

       -----》在项目中添加图像文件,然后在解决方案资源管理器中将“生成操作”属性设置为“嵌入的资源”。

       -----》将 "AssemblyName.ImageFile.jpg" 替换成程序集中已知的资源名称。可以使用程序集对象的 GetManifestResourceNames 方法查找该资源的名称。请参见代码:查找程序集中的资源名称

30.代码:从 Windows DataGrid 控件的所选单元格中检索数据 (Visual C#)

本示例从现有的 DataGrid 控件(由整数值填充)中检索选定的数据。

示例:
System.Windows.Forms.DataGridCell selectedCell = dataGrid1.CurrentCell;
object selectedItem = dataGrid1[selectedCell.RowNumber, selectedCell.ColumnNumber];
int cellValue = Convert.ToInt32(selectedItem);

编译代码-----》本示例需要: 名为 dataGrid1 的 DataGrid 控件。

31.代码:搜索 ListBox 控件中的项 (Visual C#)

本示例在 Windows 窗体 ListBox 控件中搜索字符串。

示例:

private void button1_Click(object sender, System.EventArgs e)
{
    listBox1.Items.Add("One");
    listBox1.Items.Add("Two");
    listBox1.Items.Add("Three");
    if (listBox1.FindString("Two") != -1)
        MessageBox.Show("Found it!");
}

编译代码-----》本示例需要: 具有 ListBox 控件 listBox1 和按钮控件 button1 窗体。将 button1 的 Click 事件处理程序设置为 button1_Click。

       注意   本示例还可以使用 ComboBox 控件,方法是用名为 comboBox1 的 ComboBox 控件替换 ListBox 控件,然后将代码由 listBox1 更改为 comboBox1。

32.代码:选择日历控件中的日期范围 (Visual C#)

本示例在 Windows 窗体 MonthCalendar 控件中选择某一范围的日期。在本示例中,当用户选择日期时,就会选择周。

示例:
private void monthCalendar1_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e)
{
    DateTime startDate = e.Start;
    startDate = startDate.AddDays(-(double)startDate.DayOfWeek);
    monthCalendar1.SelectionStart = startDate;
    monthCalendar1.SelectionEnd = startDate.AddDays(6);
}

编译代码-----》本示例需要:一个含有名为 monthCalendar1 的 MonthCalendar 控件的 Windows 窗体。将 monthCalendar1 的 DateSelected 事件处理程序设置为 monthCalendar1_DateSelected。

33.代码:选择 ListBox 控件中的项 (Visual C#)

本示例在 Windows 窗体 ListBox 控件中选择并突出显示一项。

示例:

private void button1_Click(object sender, System.EventArgs e)
{
    listBox1.Items.Add("One");
    listBox1.Items.Add("Two");
    listBox1.Items.Add("Three");
    listBox1.SelectedIndex = listBox1.FindString("Two");
}

编译代码-----》本示例需要: 具有 ListBox 控件 listBox1 和按钮控件 button1 窗体。将 button1 的 Click 事件处理程序设置为 button1_Click。

        注意   本示例还可以使用 ComboBox 控件,方法是用名为 comboBox1 的 ComboBox 控件替换 ListBox 控件,然后将代码由 listBox1 更改为 comboBox1。

            来源:http://msdn.microsoft.com/zh-cn/library/aa287561(VS.71).aspx

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多