Sau Bro ko nói là Control nào cà : Mình demo cho cậu cái nỳ tham khảo nha . Control của mình tên là : MySubmitControl , nhiệm vụ kiểm tra giá trị nhập liệu của ô textbox . Nếu rổng (empty)--> báo lổi . Ngược lại show lên màng hình chuổi mà người dùng đả nhập .
Tạo 1 dự án mới ngôn ngử thể hiện là C# , sử dụng Class Library Template .
Đầu tiên nó là như vậy nè :
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class Class1
{
}
}
Sau đó đổi tên của control lại thành MySubmitControl và thêm vào 2 namespace vào. và kết thừa từ Class UserControl
Code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SubmitControl
{
public class MySubmitControl:UserControl
{
}
Sau đó Paste đoạn code sau vào :
Code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SubmitControl
{
public class MySubmitControl:UserControl
{
/// <summary>
/// Tao moi 1 uy thac ten la SubmitClickedHandler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void SubmitClickedHandler(object sender,EventArgs e);
/// <summary>
/// Tao moi 1 su kien SubmitClicked thong wa uy thac SubmitClickedHandler
/// </summary>
public event SubmitClickedHandler SubmitClicked;
/// <summary>
/// Khai bao 1 control TextBox ten la txtTextSubmit
/// </summary>
private TextBox txtTextSubmit;
/// <summary>
/// Thuoc tinh cua txtTextSubmit
/// </summary>
public string TextboxSubmit
{
set { this.txtTextSubmit.Text = value;}
get { return this.txtTextSubmit.Text; }
}
/// <summary>
/// Tao doi tuong button moi ten la btnSubmit
/// </summary>
private Button btnSubmit;
private Label label1;
/// <summary>
/// Phuong thuc khoi tao cho mySubmitControl
/// </summary>
public MySubmitControl()
{
InitializeComponent();
}
/// <summary>
/// Tao cac doi tuong controls
/// </summary>
private void InitializeComponent()
{
this.txtTextSubmit = new System.Windows.Forms.TextBox();
this.btnSubmit = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtTextSubmit
//
this.txtTextSubmit.Location = new System.Drawing.Point(80, 13);
this.txtTextSubmit.Name = "txtTextSubmit";
this.txtTextSubmit.Size = new System.Drawing.Size(176, 20);
this.txtTextSubmit.TabIndex = 0;
//
// btnSubmit
//
this.btnSubmit.Location = new System.Drawing.Point(123, 42);
this.btnSubmit.Name = "btnSubmit";
this.btnSubmit.Size = new System.Drawing.Size(75, 23);
this.btnSubmit.TabIndex = 1;
this.btnSubmit.Text = "Submit";
this.btnSubmit.UseVisualStyleBackColor = true;
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 18);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(61, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Input Text :";
//
// MySubmitControl
//
this.Controls.Add(this.label1);
this.Controls.Add(this.btnSubmit);
this.Controls.Add(this.txtTextSubmit);
this.Name = "MySubmitControl";
this.Size = new System.Drawing.Size(289, 85);
this.ResumeLayout(false);
this.PerformLayout();
}
/// <summary>
/// Dang ky su kien cho control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnSubmitClicked(object sender,EventArgs e)
{
if (SubmitClicked != null)
{
SubmitClicked(sender, e);
}
}
/// <summary>
/// Viet ham kiem tra gia tri nhap cho Textbox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSubmit_Click(object sender, EventArgs e)
{
if(txtTextSubmit.Text=="")
{
MessageBox.Show("The Empty String does not allow",
" Error ",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
else
{
OnSubmitClicked(sender, e);
}
}
}
}
Build ứng dụngg (F6) biên dịch ra *.DLL, chú ý Visual Studio ko cho phép Run Class Library nha , vì ko có "điêm đột nhập" . Tức là hàm Main .
Sau đó từ Cửa xổ solution Explorer, tạo mới 1 Project WinForm , ngôn ngử là C# ( Vb.NET cũng được ) . Từ Toot box Kéo thả Control mình vừa tạo , nếu ko có thì chọn choose Items , vào browse đến file *.DLL mà vửa nảy vửa biên dịch xong .

Chú ý Control ta vừa tạo có thêm 1 thuộc tính và 1 phương thức . Ta sẻ viết code cho phương thức này .
Code:
private void mySubmitControl1_SubmitClicked(object sender, EventArgs e)
{
MessageBox.Show(mySubmitControl1.TextboxSubmit);
}
Thế là xong , mổi lân làm chương trình muốn khỏi mắc công kiểm tra dử liệu nhập của người dùng thì ta có thể sài lại control của ta rùi .
Chú bạn thành công .
Mọi thắc mắc , bài vở liên hệ : Y!M : mr_ha_hcm
Thân ái xin kính chào tạm biệt và hẹn gặp lại .