针对教材2.3.4部分讲到的通用对话框控件和MessageBox的使用进行演示
public void DisplayValue()
        
{
            
//获取ShowDialog()返回值
            DialogResult userResponse = openFileDialog1.ShowDialog();
            
if(userResponse == DialogResult.OK)//如果用户选定文件
            {
                
//获取文件完整路径
                string filePath = openFileDialog1.FileName.ToString();
                MessageBox.Show(
"You successfully opened: '"+
                    filePath
+"'",//MessageBox主体信息
                    "Success",//MessageBox标题
                    MessageBoxButtons.OK,//MessageBox显示的按钮
                    MessageBoxIcon.Information,//MessageBox显示的图标
                    MessageBoxDefaultButton.Button3);//设置MessageBox默认按钮
            }

            
else//如果用户没有选定文件
            {
                MessageBox.Show(
"You canceled "+
                    
"the open file operation.",
                    
"Warning",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
//指定将对消息框使用哪些显示和关联选项
            }

        }
完整源代码下载:use_Dialog.rar
posted @ 2007-03-01 19:51 dn 阅读(111) 评论(0) 编辑
对应教材2.2.2中的(12)部分,主要对ListBox控件进行演示
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

public class EnvironmentVars:Form
{
    Label label;

    
public static void Main()
    
{
        Application.Run(
new EnvironmentVars());
    }

    
public EnvironmentVars()
    
{
        
//设置窗体属性,这些属性属于基类Form
        Text = "Environment Variables";
        
        
//动态创建Label控件
        label = new Label();
        label.Parent 
= this;
        label.Anchor 
= AnchorStyles.Left | AnchorStyles.Right;
        label.Location 
= new Point(Font.Height,Font.Height);
        label.Size 
= new Size(ClientSize.Width - 2 * Font.Height,Font.Height);
 
        
//动态创建ListBox控件
        ListBox listbox = new ListBox();
        listbox.Parent 
= this;
        listbox.Location 
= new Point(Font.Height,3 * Font.Height);
        listbox.Size 
= new Size(12 * Font.Height,8 * Font.Height);
        listbox.Sorted 
= true;
        listbox.SelectedIndexChanged 
+= new EventHandler(ListBoxOnSelectedIndexChanged);

        
//获取环境变量
        IDictionary dict = Environment.GetEnvironmentVariables();
        
string[] astr = new string[dict.Keys.Count];

        
//将环境变量的键值(Key)显示到ListBox中
        dict.Keys.CopyTo(astr,0);
        listbox.Items.AddRange(astr);
        listbox.SelectedIndex 
= 0;

    }


    
void ListBoxOnSelectedIndexChanged(object obj,EventArgs ea)
    
{
        
//获取用户点击的ListBox条目及数据
        ListBox listbox = (ListBox)obj;
        
string strItem = (string)listbox.SelectedItem;

        
//将对应键值的环境变量值显示到Label上
        label.Text = Environment.GetEnvironmentVariable(strItem);
    }

}


完整源代码下载:EnvironmentVars.rar
posted @ 2007-03-01 19:47 dn 阅读(65) 评论(0) 编辑
对应教材2.2.2的(4)部分
using System;
using System.Drawing;
using System.Windows.Forms;

public class DateAndTimeStatus:Form
{
    StatusBarPanel sbpMenu,sbpDate,sbpTime;
    
public static void Main()
    
{
        Application.Run(
new DateAndTimeStatus());
    }

    
public DateAndTimeStatus()
    
{
        
//设置窗体属性,这些属性属于基类Form
        Text = "Date and Time Status";
        BackColor 
= SystemColors.Window;
        ForeColor 
= SystemColors.WindowText;

        
//动态创建StstusBar控件
        StatusBar sb = new StatusBar();
        sb.Parent 
= this;
        sb.ShowPanels 
= true;

        
//动态创建StatusBarPanel控件
        sbpMenu = new StatusBarPanel();
        sbpMenu.Text 
= "Reserved for menu help";
        sbpMenu.BorderStyle 
= StatusBarPanelBorderStyle.None;
        sbpMenu.AutoSize 
= StatusBarPanelAutoSize.Spring;

        sbpDate 
= new StatusBarPanel();
        sbpDate.AutoSize 
= StatusBarPanelAutoSize.Contents;
        sbpDate.ToolTipText 
= "The Current date";

        sbpTime 
= new StatusBarPanel();
        sbpTime.AutoSize 
= StatusBarPanelAutoSize.Contents;
        sbpTime.ToolTipText 
= "The Current time";

        
//将StatusBarPanel控件实例添加到StstusBar控件的Panels集合
        sb.Panels.AddRange(new StatusBarPanel[]{sbpMenu,sbpDate,sbpTime});

        
//创建计时器控件并加载事件处理函数
        Timer timer = new Timer();
        timer.Tick 
+= new EventHandler(TimerOnTick);
        timer.Interval 
= 1000;
        timer.Start();
    }


    
//计时器控件事件处理函数
    void TimerOnTick(object obj,EventArgs ea)
    
{
        
//获取当前时间,日期
        DateTime dt = DateTime.Now;

        
//在StatusBarPanel上设置当前日期,时间
        sbpDate.Text = dt.ToShortDateString();
        sbpTime.Text 
= dt.ToShortTimeString();
    }

}


完整源代码下载:DateAndTimeStatus.rar
posted @ 2007-03-01 19:44 dn 阅读(62) 评论(0) 编辑

对应教材2.2.2中的(2)部分

using System;
using System.Drawing;
using System.Windows.Forms;


public class TwoStatusBarPanels:Form
{
    
public static void Main()
    
{
        Application.Run(
new TwoStatusBarPanels());
    }

    
public TwoStatusBarPanels()
    
{
        
//设置窗体属性,这些属性属于基类Form
        Text = "Two Status Bar Panels";
        BackColor 
= SystemColors.Window;
        ForeColor 
= SystemColors.WindowText;

        
//动态创建StstusBar控件
        StatusBar sb = new StatusBar();
        sb.Parent 
= this;
        sb.ShowPanels 
= true;

        
//动态创建StatusBarPanel控件
        StatusBarPanel sbPanel1 = new StatusBarPanel();
        sbPanel1.Text 
= "Panel 1";

        StatusBarPanel sbPanel2 
= new StatusBarPanel();
        sbPanel2.Text 
= "Panel 2";
        
        
//将StatusBarPanel控件实例添加到StstusBar控件的Panels集合
        sb.Panels.Add(sbPanel1);
        sb.Panels.Add(sbPanel2);
    }

}
完整源代码下载:TwoStatusBarPanels.rar
posted @ 2007-03-01 19:41 dn 阅读(52) 评论(0) 编辑