Windows應用程序常用的知識點(Winform設計必看)

 
最近忙着把一個項目有B/S架構 變成 C/S架構,由於本人是B/S出身,對C/S不是很熟,就摸索着做,在這過程中,總結了一些經驗。當看到我的Blog瀏覽數達到3000時心裏滿高興的,就沖沖寫了這篇文章,以表感謝!有不足之處,望多多交流!本篇會隨時更新,望留意。
一、C#中的全局變量
 
C#中沒有了像VB.Net中的全局變量,那麼我們如何實現在不同的頁面間傳遞參數呢?
下面舉例說明如何實現這一功能.
1.新建一個項目.
2.在該工程中添加一個窗體Form1.
3.在該窗體中定義靜態型字符串變量myTestStr1:
public static string myTestStr1="";
4.在該窗體的構造函數中對該變量進行賦值,併爲該窗體類添加屬性GetStrValue.
 
public Form_Form1()
         {
              InitializeComponent();
             
              myTestStr1="Hello!";
         }
         public string GetStrValue
         {
              get
              {
                   return myTestStr1;
              }
              set
              {
                   myTestStr1=value;
              }
         }      
5.在該工程中另添加一個窗體Form2.
6.在Form1窗體上添加一個button按鈕(name:but_Test);
7.在Form1窗體的but_Test_Click 事件中添加以下代碼:
private void but_Test_Click(object sender, System.EventArgs e)
         {
              Form2 frm1=new Form2();
              frm1.ShowDialog(this) ;
              frm1.Close();
 
         }
8.在Form2窗體上添加一個button按鈕(name:but_Yes);
9.在Form1窗體的but_Yes_Click 事件中添加以下代碼:
private void but_Yes_Click(object sender, System.EventArgs e)
         {
              MessageBox.Show (Form_Form1.myTestStr1 );    //直接訪問. 顯示.結果:" Hello!"
              Form_Form1 frm2=new Form_Form1();
              frm2.GetStrValue ="How do you do?";                                       //生成一個新的實例對該靜態變量進行操作(修改該靜態變量的值).
              MessageBox.Show (frm2.GetStrValue );                     //通過該實例的內部成員對它進行訪問 .顯示.結果: How do you do?"
 
              MessageBox.Show (Form_Form1.myTestStr1 );   //直接訪問. 顯示.結果:" How do you do?"
 
         }
 
二、在Windows窗體開發中使用Cache
Cache在程序設計時可以帶來很大的便利,這點想必在web程序開發中大家都有深刻的體會!同樣,在windows程序設計中也可以使用它。
 
使用時需要將web引用添加到窗體項目的引用中,下面以一個實例介紹之!
首先要將程序的Main()函數放在一個添加的類裏,這裏就是AppMain類。下面是AppMain類的代碼:
 
using System;
using System.Threading;
using System.Web;//添加引用
using System.Web.Caching; //添加引用
 
using System.Windows.Forms;
 
namespace WindowsApplication1
{
     public class AppMain
     {
          private static HttpRuntime _httpRuntime;
 
         public static Cache Cache
         {
              get
              {
                   EnsureHttpRuntime();
                   return HttpRuntime.Cache;
              }
         }
 
          [STAThread]
         static void Main()
         {
              Application.Run(new Form1());
         }
     
          private static void EnsureHttpRuntime()
         {
              if( null == _httpRuntime )
              {
                   try
                   {
                        Monitor.Enter( typeof( AppMain ) );
                        if( null == _httpRuntime )
                       {
                            // Create an Http Content to give us access to the cache.
                            _httpRuntime = new HttpRuntime();
                       }
                   }
                   finally
                   {
                        Monitor.Exit( typeof( AppMain ) );
                   }
              }
         }
     }
}
然後在Form1窗體中定義和使用Cache,代碼如下:
using System;
using System.Web;//添加引用
using System.Web.Caching; //添加引用
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace WindowsApplication1
{
     ///<summary>
     /// Form1 的摘要說明。
     ///</summary>
     public class Form1 : System.Windows.Forms.Form
     {
          private System.Windows.Forms.Label label1;
          private System.Windows.Forms.Label label2;
          private System.Windows.Forms.TextBox txtValueToPutInCache;
          private System.Windows.Forms.TextBox txtValueInCache;
          private System.Windows.Forms.Button btnPutInCache;
          private System.Windows.Forms.Button btnGetFromButton;
     
          private const string CACHE_KEY = "APPCACHEKEY";
     
         ///<summary>
         /// Required designer variable.
         ///</summary>
          private System.ComponentModel.Container components = null;
     
         public Form1()
         {
              //
              // Required for Windows Form Designer support
              //
              InitializeComponent();
        
              //
              // TODO: Add any constructor code after InitializeComponent call
              //
         }
     
         ///<summary>
         /// Clean up any resources being used.
         ///</summary>
          protected override void Dispose( bool disposing )
         {
              if( disposing )
              {
                   if (components != null)
                   {
                        components.Dispose();
                   }
              }
              base.Dispose( disposing );
         }
 
          #region Windows Form Designer generated code
         ///<summary>
         /// Required method for Designer support - do not modify
         /// the contents of this method with the code editor.
         ///</summary>
          private void InitializeComponent()
         {
              this.label1 = new System.Windows.Forms.Label();
              this.label2 = new System.Windows.Forms.Label();
              this.txtValueToPutInCache = new System.Windows.Forms.TextBox();
              this.txtValueInCache = new System.Windows.Forms.TextBox();
              this.btnPutInCache = new System.Windows.Forms.Button();
              this.btnGetFromButton = new System.Windows.Forms.Button();
              this.SuspendLayout();
              //
              // label1
              //
              this.label1.AutoSize = true;
              this.label1.Location = new System.Drawing.Point(8, 16);
              this.label1.Name = "label1";
              this.label1.Size = new System.Drawing.Size(113, 16);
              this.label1.TabIndex = 0;
              this.label1.Text = "Value to put in cache:";
              //
              // label2
              //
              this.label2.AutoSize = true;
              this.label2.Location = new System.Drawing.Point(8, 40);
              this.label2.Name = "label2";
              this.label2.Size = new System.Drawing.Size(95, 16);
              this.label2.TabIndex = 1;
              this.label2.Text = "Value from cache:";
              //
              // txtValueToPutInCache
              //
              this.txtValueToPutInCache.Location = new System.Drawing.Point(128, 16);
              this.txtValueToPutInCache.Name = "txtValueToPutInCache";
              this.txtValueToPutInCache.Size = new System.Drawing.Size(200, 20);
              this.txtValueToPutInCache.TabIndex = 2;
              this.txtValueToPutInCache.Text = "";
              //
              // txtValueInCache
              //
              this.txtValueInCache.Location = new System.Drawing.Point(128, 40);
              this.txtValueInCache.Name = "txtValueInCache";
              this.txtValueInCache.ReadOnly = true;
              this.txtValueInCache.Size = new System.Drawing.Size(200, 20);
              this.txtValueInCache.TabIndex = 3;
              this.txtValueInCache.Text = "";
              //
              // btnPutInCache
              //
              this.btnPutInCache.Location = new System.Drawing.Point(352, 16);
              this.btnPutInCache.Name = "btnPutInCache";
               this.btnPutInCache.Size = new System.Drawing.Size(104, 23);
              this.btnPutInCache.TabIndex = 4;
              this.btnPutInCache.Text = "Put in Cache";
              this.btnPutInCache.Click +=
                   new System.EventHandler(this.btnPutInCache_Click);
              //
              // btnGetFromButton
              //
              this.btnGetFromButton.Location = new System.Drawing.Point(352, 40);
              this.btnGetFromButton.Name = "btnGetFromButton";
              this.btnGetFromButton.Size = new System.Drawing.Size(104, 23);
              this.btnGetFromButton.TabIndex = 5;
              this.btnGetFromButton.Text = "Get from Cache";
              this.btnGetFromButton.Click +=
                   new System.EventHandler(this.btnGetFromButton_Click);
              //
              // Form1
              //
              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
              this.ClientSize = new System.Drawing.Size(488, 133);
              this.Controls.Add(this.btnGetFromButton);
              this.Controls.Add(this.btnPutInCache);
              this.Controls.Add(this.txtValueInCache);
              this.Controls.Add(this.txtValueToPutInCache);
              this.Controls.Add(this.label2);
              this.Controls.Add(this.label1);
              this.Name = "Form1";
              this.Text = "Form1";
              this.ResumeLayout(false);
         }
          #endregion
 
          private void btnPutInCache_Click(object sender, System.EventArgs e)
         {
              AppMain.Cache.Insert(
                   CACHE_KEY,
                   txtValueToPutInCache.Text,
                   null,
                   Cache.NoAbsoluteExpiration,
                   TimeSpan.FromSeconds( 60 ) );
         }
 
          private void btnGetFromButton_Click(object sender, System.EventArgs e)
         {
              string value;
              value = AppMain.Cache[ CACHE_KEY ] as string;
              if( null == value )
              {
                   value = "[No value in the cache.]";
              }
              txtValueInCache.Text = value;
         }
     }
}
三、windows form參數傳遞過程
Windows 程序設計中參數的傳遞,同樣也是非常的重要的。
這裏主要是通過帶有參數的構造函數來實現的,
 
說明:Form1爲主窗體,包含控件:文本框textBoxFrm1,多選框checkBoxFrm1和按鈕buttonEdit  
Form2爲子窗體,包含控件:文本框textBoxFrm2,多選框checkBoxFrm2和按鈕buttonOKbuttonCancel
當我們新建一個窗體的時候,設計器會生成默認的構造函數:  
     public Form2() 
              { 
                   InitializeComponent(); 
            } 
它不帶參數,既然我們要把Form1中的一些數據傳到Form2中去,爲什麼不在Form2的構造函數裏做文章呢?
 假設我們要實現使Form2中的文本框顯示Form1textBoxFrm1的值,修改子窗體的構造函數:  
public Form2(string text)         
              {
                   InitializeComponent(); 
                   this.textBoxFrm2.Text = text;  
            } 增加Form1中的修改按鈕點擊事件,處理函數如下:  
     private void buttonEdit_Click(object sender, System.EventArgs e)
              {
                   Form2 formChild = new Form2(this.textBoxFrm1.Text); 
                   formChild.Show(); 
              }
 
我們把this.textBoxFrm1.Text作爲參數傳到子窗體構造函數,以非模式方式打開,這樣打開的formChild的文本框就顯示了”主窗體”文本,是不是很簡單,接下來我們傳一個boolean數據給子窗體。 
     Public Form2(string text,bool checkedValue) 
         { 
              InitializeComponent(); 
              this.textBoxFrm2.Text = text; 
              this.checkBoxFrm2.Checked = checkedValue; 
         } 
 
private void buttonEdit_Click(object sender, System.EventArgs e)
         { 
              Form2 formChild = new Form2(this.textBoxFrm1.Text,this.checkBoxFrm1.Checked); 
              formChild.Show(); 
         } 
 
參數傳過來了!是不是挺經典的啊!
 
 
四、WinForm例子動態獲取Datagrid的選中行的各字段的值(C#技術)
 
獲得行索引
              int i = this.datagrid.CurrentRowIndex;
用索引獲得某列值
              string str = this.datagrid[i,3].ToString();
此時就得到當前行的第三列的數據。
本篇就先寫到這裏,一會不停的更新、添加。望多多留意。
做人要厚道,看後發表看法哦!
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章