各位:我想写一个基类窗体,把一些常用的功能放在这个类里。其余窗体可以继承这个基类 。但是老搞不对,那位能干给些示例代码呢?

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;namespace asrs
    {
    /// <summary>
    /// PublicFrm 的摘要说明。
    /// </summary>
    public class PublicFrm : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
            public DbLink DataProcess=new DbLink("wxh","asrs");
    public PublicFrm()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // PublicFrm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(272, 237);
    this.Name = "PublicFrm";
    this.Text = "PublicFrm";
    this.Load += new System.EventHandler(this.PublicFrm_Load); }
    #endregion
    public void InitFrm(GroupBox groupBox)
    {
    foreach(Control ctl in groupBox.Controls)
    {
    switch(ctl.GetType().Name)
    {
    case "TextBox":
    case"ComboBox":
    ctl.Text=string.Empty;
    break;
    case "CheckBox":
    ((CheckBox)ctl).Checked=false;
    break;
    case"LisBox":
    ((ListBox)ctl).Items.Clear();
    break;
            case"DataGrid":
    ((DataGrid)ctl).DataSource=null;
    break;
    case"DateTimePicker":
    ((DateTimePicker)ctl).Value=DateTime.Today;
    break;
    }
    }
    } public bool checkEmpty(GroupBox groupBox)
    {
    foreach(Control ctl in groupBox.Controls)

    switch(ctl.GetType().Name)
    {   
    case"ComboBox":
    case"TextBox":
    if(ctl.Text=="")
    {   
    MessageBox.Show(ctl.Name+" is not Null!","ERR!");
    ctl.Focus();
    return false;
    }
    break;
    }
    }
                return true;
    } public bool IsNumeric(string itemValue)
    {
    try
    {
    //Regex numRegex=new Regex(@"(-?\d*)(\.\d+)?");
    Regex numRegex=new Regex(@"^(-?[0-9]*[.]*[0-9]*)$");
    Match Result=numRegex.Match(itemValue);
    if(Result.Success)
    {return true;}
    else
    {return false;}
    }
    catch(FormatException ex)
    {
    MessageBox.Show("格式化错误:\n\r"+ex.Message,"Err!");
    return false;
    }
    catch(Exception ex)
    {
    MessageBox.Show("其他错误:\n\r"+ex.Message,"Err!");
    return false;
    }
    }
    private void PublicFrm_Load(object sender, System.EventArgs e)
    {

    }
    }
    }
      

  2.   

    public class frmMain :PublicFrm    //System.Windows.Forms.Form
    {
    private System.Windows.Forms.TreeView treeView1;
    private System.Windows.Forms.Splitter splitter1;
    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.Windows.Forms.MenuItem menuItem2;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    string                     sql;
    DataTable dTable;
    private System.Windows.Forms.StatusBar statusBar1;
    private System.Windows.Forms.StatusBarPanel statePanel1;
    private System.Windows.Forms.StatusBarPanel statePanel2;
    TreeNode rootNode=new TreeNode();

    public frmMain()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    //data.Open();
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    statusBar1.Panels[0].Text="用户ID:"+frmLogin.strID;
    statusBar1.Panels[1].Text="用户名称:"+frmLogin.strName; } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// 
    [STAThread]
    static void Main() 
    {
    frmLogin login=new frmLogin();
    login.ShowDialog();
    if(frmLogin.blLogin)
    {
    Application.Run(new frmMain());
    }
    }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.treeView1 = new System.Windows.Forms.TreeView();
    this.splitter1 = new System.Windows.Forms.Splitter();
    this.mainMenu1 = new System.Windows.Forms.MainMenu();
    this.menuItem1 = new System.Windows.Forms.MenuItem();
    this.menuItem2 = new System.Windows.Forms.MenuItem();
    this.statusBar1 = new System.Windows.Forms.StatusBar();
    this.statePanel1 = new System.Windows.Forms.StatusBarPanel();
    this.statePanel2 = new System.Windows.Forms.StatusBarPanel();
    ((System.ComponentModel.ISupportInitialize)(this.statePanel1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.statePanel2)).BeginInit();
    this.SuspendLayout();
    // 
    // treeView1
    // 
    this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;
    this.treeView1.ImageIndex = -1;
    this.treeView1.Location = new System.Drawing.Point(0, 0);
    this.treeView1.Name = "treeView1";
    this.treeView1.SelectedImageIndex = -1;
    this.treeView1.Size = new System.Drawing.Size(128, 409);
    this.treeView1.TabIndex = 1;
    this.treeView1.DoubleClick += new System.EventHandler(this.treeView1_DoubleClick);
    this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
    // 
    // splitter1
    // 
    this.splitter1.Location = new System.Drawing.Point(128, 0);
    this.splitter1.Name = "splitter1";
    this.splitter1.Size = new System.Drawing.Size(8, 409);
    this.splitter1.TabIndex = 2;
    this.splitter1.TabStop = false;
    // 
    // mainMenu1
    // 
    this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem1});
    // 
    // menuItem1
    // 
    this.menuItem1.Index = 0;
    this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem2});
    this.menuItem1.Text = "库存管理";
    // 
    // menuItem2
    // 
    this.menuItem2.Index = 0;
    this.menuItem2.Text = "入库管理";
    this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
    // 
    // statusBar1
    // 
    this.statusBar1.Location = new System.Drawing.Point(0, 409);
    this.statusBar1.Name = "statusBar1";
    this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
      this.statePanel1,
      this.statePanel2});
    this.statusBar1.ShowPanels = true;
    this.statusBar1.Size = new System.Drawing.Size(696, 24);
    this.statusBar1.TabIndex = 4;
    this.statusBar1.Text = "statusBar1";
    // 
    // statePanel1
    // 
    this.statePanel1.Text = "statusBarPanel1";
    // 
    // statePanel2
    // 
    this.statePanel2.Text = "statusBarPanel1";
    // 
    // frmMain
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(696, 433);
    this.Controls.Add(this.splitter1);
    this.Controls.Add(this.treeView1);
    this.Controls.Add(this.statusBar1);
    this.IsMdiContainer = true;
    this.Menu = this.mainMenu1;
    this.Name = "frmMain";
    this.Text = " ";
    this.Load += new System.EventHandler(this.Main_Load);
    ((System.ComponentModel.ISupportInitialize)(this.statePanel1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.statePanel2)).EndInit();
    this.ResumeLayout(false); }
    #endregion private void Main_Load(object sender, System.EventArgs e)
    {
    rootNode=treeView1.Nodes.Add("ASRS管理系统");
    rootNode.Tag="main";
            sql="Select Prog_ID,Prog_Name From PRG_LST";
    dTable=DataProcess.Sql_Select(sql);
    add_Node("库存管理","S",rootNode);
    add_Node("盘点管理","C",rootNode);
    add_Node("查询管理","F",rootNode);
    add_Node("维护管理","M",rootNode);
    // foreach(DataRow dr in ds.Tables[0].Rows)
    // {   
    // switch(dr["UserId"].ToString())
    // {
    // case "S":
    //                      add_Node(dr["UserName"].ToString(),dr["UserId"].ToString(),S);
    // break;
    // case "P":
    //   add_Node(dr["UserName"].ToString(),dr["UserId"].ToString(),P);
    // break;
    // case"C":
    // add_Node(dr["UserName"].ToString(),dr["UserId"].ToString(),C);
    // break;
    // case"W":
    // add_Node(dr["UserName"].ToString(),dr["UserId"].ToString(),W);
    // break;
    // default:
    // add_Node(dr["UserName"].ToString(),dr["UserId"].ToString(),S);
    // break;
    // }
    //
    // }
    } /// <summary>
    /// 增加子结点
    /// </summary>
    /// <param name="sNode"></param>
    /// <param name="sTag"></param>
    /// <param name="pNode"></param>
    private  void add_Node(string sNode,string sTag,TreeNode pNode)
    {
    TreeNode xNode=new TreeNode();
    xNode=pNode.Nodes.Add(sNode);
    xNode.Tag=sTag;
    foreach(DataRow dr in dTable.Rows)
    {
    if(dr[0].ToString().Substring(0,1)==sTag)
    {
    TreeNode cNode=new TreeNode();
    cNode=xNode.Nodes.Add(dr[1].ToString());
    cNode.Tag=dr[0].ToString();
    }
    }
    } private void menuItem2_Click(object sender, System.EventArgs e)
    {
    if(checkChildFrmExist("frmInput"))
    {
    return;
    }
    frmInput newFrm=new frmInput();
    newFrm.MdiParent=this;
    newFrm.Show();
    }
            
    /// <summary>
    /// 检查子窗体是否存在
    /// </summary>
    /// <param name="childFrmName"></param>
    /// <returns></returns>
    private bool checkChildFrmExist(string childFrmName)
    {
    foreach(Form childFrm in this.MdiChildren)
    {
    if(childFrm.Name==childFrmName)
    {
    if(childFrm.WindowState==FormWindowState.Minimized)
    {
    childFrm.WindowState=FormWindowState.Maximized;
    }
    childFrm.Activate();
    return true;
    }
    }
    return false;
    } private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
    {

    switch(e.Node.Tag.ToString())
    {
    case "S1":
    if(checkChildFrmExist("frmInput"))
    {
    return;
    }
    frmInput FrmS1=new frmInput();
    FrmS1.MdiParent=this;
    FrmS1.Show();
    break;
    case"M5":
    if(checkChildFrmExist("frm_Item_Mst"))
    {
    return;
    }
    frm_Item_Mst FrmM5=new frm_Item_Mst();
    FrmM5.MdiParent=this;
    FrmM5.Show();
    break; }

    }