天天看點

c# 類操作 窗體

方法一:

我在做一個聊天程式的時候遇到這樣一個問題,我需要将操作類與窗體分離,就是說類來實作所有的操作,窗體類中隻進行調用類,如果是簡單的操作,很容易實作,但是如果裡面設計到有參數或有傳回的線程,就可能有些困難,是以在這裡說一下,用類來控制窗體部分控件的方法.就寫一個很簡單的例子:

比如說現在我們有一個form1窗體,上面有一個label1控件,現在我們要實作執行Class1類來改變label1.text為"hello",可以這樣來作:

1.在form1中添加屬性

c# 類操作 窗體

    public string SetLabelText

c# 類操作 窗體
c# 類操作 窗體
c# 類操作 窗體

{

c# 類操作 窗體

        set

c# 類操作 窗體
c# 類操作 窗體
c# 類操作 窗體
c# 類操作 窗體

            this.Label1.text = value;    

c# 類操作 窗體

         }

c# 類操作 窗體

     }

c# 類操作 窗體

2.然後可以在Class1的類中寫一個方法

c# 類操作 窗體

public static voic SetText( Form1 objForm )        //這裡傳遞的是Form1窗體類的一個執行個體

c# 類操作 窗體
c# 類操作 窗體
c# 類操作 窗體
c# 類操作 窗體

     objForm.SetLabelText = "hello";                //這樣就可以找到我們定義的屬性

c# 類操作 窗體

}

3.在Form1裡的相應事件中寫入

c# 類操作 窗體

Class1.SetText( this );

這樣就可以将窗體作為參數傳入類,然後可以根據屬性來對其進行相應的操作,如果動動腦筋,可以實作更多.

方法二:

   帶線程的操作

主窗體:FrmBackup.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Threading;

namespace DataBackup

    public partial class FrmBackup : Form

    {

   public FrmBackup()

        {

            InitializeComponent();           

            Control.CheckForIllegalCrossThreadCalls = false;

        }

        #region 重寫窗體屬性<不同類調用的地方>

        public bool SetForm

            set

            {

                this.Enabled = value;               

            }          

        public bool SetProgressBar

                this.progressBar.Visible = value;

            }

        #endregion

        #region 恢    複

        private void btnRegain_Click(object sender, EventArgs e)

            this.Enabled = false;

            OpenFileDialog op = new OpenFileDialog();

            //标題

            op.Title = "選擇資料庫備份檔案";

            //檔案初始路徑

            op.InitialDirectory = dbpath;

            //目前檔案名篩選器

            op.Filter = "資料庫檔案(*.mdb)|*.mdb";

            //檔案對話框中目前標明篩選器的索引

            op.FilterIndex = 1;

            if (op.ShowDialog() == DialogResult.OK)

                //顯示進度條

                this.progressBar.Visible = true;

                this.progressBar.BringToFront();

                this.progressBar.Size = new Size(307, 35);

                this.progressBar.Location = new Point(81, 157);

                this.progressBar.Maximum = 100;

                this.progressBar.Step = 1;

                string source_path = "..\\DbMVC\\bin\\gs_gw680.mdb";

                //進度條線程

                Thread td1 = new Thread(new ThreadStart(PrgBar));

                td1.IsBackground = true;

                td1.Start();

                //資料庫檔案名

                string dbname = op.FileName;

     //這裡就是使用線程的地方,把目前窗體用this傳過去

                BackOther bac = new BackOther(dbname, source_path, this);

                Thread rec = new Thread(new ThreadStart(bac.RecoverAcsInfo));

                rec.IsBackground = true;

                rec.Start();

   }

需要改變窗體控件屬性的類,如下:

using System.Data.OleDb;

    /// <summary>

    /// Self_Class:關于備份操作的其它方法

    /// </summary>

    public class BackOther

        public BackOther()

        #region 構造函數相關<接收窗體及其要傳的值>

        string db_path = string.Empty;

        string source_path = string.Empty;

        FrmBackup frm_bk = null;

        public BackOther(string path, string sourcepath, FrmBackup frmbk)

            this.db_path = path;

            this.source_path = sourcepath;

            this.frm_bk = frmbk;

        #region 本類全局變量

        /// <summary>

        /// Self_Variable:連接配接對象

        /// </summary>

        private OleDbConnection objAcsSqlConnection = null;

        /// Self_Variable:恢複資料庫對象

        private OleDbConnection objSqlConnection = null;

        #region 數 據 恢 複

        /// Self_Function:資料恢複

        /// <param name="path">備份資料庫路徑</param>

        /// <param name="dbpath">恢複的目的資料庫</param>

        /// <returns>TRUE or FALSE </returns>

        public void RecoverAcsInfo()

            try

               //其它代碼......          

            catch (Exception)

                throw;

            finally

                //改變FrmBackup窗體屬性的入口

                MessageBox.Show("恢複完成!");

                SetText(frm_bk);               

        /// Self_Form:跨類改變控件屬性

        /// <param name="objForm">傳遞的是FrmBackup窗體類的一個執行個體</param>

        public static void SetText(FrmBackup objForm)       

            //這樣就可以找到我們定義的屬性

            objForm.SetForm = true;

            objForm.SetProgressBar = false;