C#自定義控件的應用(數據綁定,屬性等)


     剛剛開始程序設計的碼農生涯,也許一些開發工具上的控件可以滿足我們的需求,但是隨之時間的遷移,我們對控件的呈現形式需求越來越多樣化,這個時候就需要我們來自定義控件,我是一個剛剛入職沒多久的菜鳥,接觸軟件開發也沒有超過兩年的時間,在以前的日子里,開發工具上的控件基本可以達到我學習的目的。但是現在工作了,用戶的需求多樣化,也造就了我們程序員的鬼斧神手。

    今天由於需求,我需要作出如下圖這樣的界面並綁定數據庫數據,開始我有幾個思路,並且一個一個去嘗試,對於winform的這種格式,之前從來也沒有做過,如果是asp.net這種格式的話就非常的簡單了。

    思路一:listview控件,看上去listview控件可以實現這種界面,但是由於界面的局限性,做出這種效果似乎不大可能,在嘗試過一兩天的時間后,我覺得綁定數據是沒有任何問題的,但是,問題就是在listview控件中實現如下圖這種格式的布局。所以我選擇了放棄,不知道各位用listview控件實現過這種效果沒有。后來聽說重寫listview的方法可行,但是我沒有去試過。

    思路二:datagridview控件,開始沒有往這個方面去考慮,部門里大牛們說可以,我仔細的看了一下datagridview控件的一下屬性,其中列的類型里面控件格式沒有我需要的控件,可得的話,又要方法重寫了。這個工作量估計很大,而且布局能不能布局成這樣的效果我也不知道。這個我想了一下也沒有去嘗試。

    思路三:自定義控件,以前在學校里面接觸過自定義控件,但都是簡單的拼接一下各個不同的控件,沒有什么特別的功能,今天特意的學習了一下字典控件,最終做好了一個類似於IDE里面的控件一樣可以輕松使用。

    實現效果不再熬數,這里我記下我寫過的代碼:

using System.Data;
using System.Data.SqlClient;
namespace menu
{
partial class MyControl
{

/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region 組件設計器生成的代碼

/// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MyControl));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.pictureBox1);
this.groupBox1.Location = new System.Drawing.Point(0, 14);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(99, 163);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(6, 116);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(3, 17);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(90, 90);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(114, 31);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 2;
this.label2.Text = "label2";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(114, 70);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 12);
this.label3.TabIndex = 3;
this.label3.Text = "label3";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(116, 109);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(41, 12);
this.label4.TabIndex = 4;
this.label4.Text = "label4";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(114, 148);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(41, 12);
this.label5.TabIndex = 5;
this.label5.Text = "label5";
//
// imageList1
//
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
this.imageList1.Images.SetKeyName(0, "001.jpg");
this.imageList1.Images.SetKeyName(1, "002.jpg");
//
// MyControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label2);
this.Controls.Add(this.label3);
this.Controls.Add(this.groupBox1);
this.Name = "MyControl";
this.Size = new System.Drawing.Size(235, 207);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

public System.Windows.Forms.GroupBox groupBox1;
public System.Windows.Forms.Label label1;
public System.Windows.Forms.PictureBox pictureBox1;
public System.Windows.Forms.Label label2;
public System.Windows.Forms.Label label3;
public System.Windows.Forms.Label label4;
public System.Windows.Forms.Label label5;
public void DataLoad(int m) //自定義數據綁定方法
{
SqlConnection con = Class1.CyCon();
con.Open();
string str="select * from tb_test where id='"+m+"'";
SqlCommand cmd = new SqlCommand(str, con);
//DataSet ds = new DataSet();
//sda.Fill(ds);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
groupBox1.Text = sdr["id"].ToString();
label1.Text = sdr["position"].ToString();
label2.Text = "工號:"+sdr["workno"].ToString();
label3.Text = "姓名:"+sdr["name"].ToString();
label4.Text = "類型:" + sdr["leixing"].ToString();
label5.Text = sdr["time"].ToString();
if (sdr["leixing"].ToString()=="進")
{
pictureBox1.Image = imageList1.Images[1];
}
else
{
pictureBox1.Image = imageList1.Images[0];
}
/*if(label4.Text=="出")
{
pictureBox1.Image = imageList1.Images[1];
}*/
}

}

public System.Windows.Forms.ImageList imageList1;
private System.ComponentModel.IContainer components;
}
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM