Application of C # Custom controls (data binding, properties, etc.)

Source: Internet
Author: User

Just beginning to program the code of farming career, perhaps some development tools on the control can meet our needs, but the time of the migration, we have more and more diversity in the representation of the control, this time we need to customize the control, I am a new job not long ago, the rookie, No more than two years of contact with software development, in the previous days, the control on the development tools can basically achieve the purpose of my study. But now the work, the user's demand diversification, also created our Programmer's delicated God hand.

Today because of the demand, I need to make such an interface and binding database data, I began to have a few ideas, and one to try, for the WinForm of this format, has never done before, if it is the format of ASP.

Idea one: The ListView control, it seems that the ListView control can implement this interface, but due to the limitations of the interface, it seems unlikely to make this effect, after a day or two of trying, I feel bound data is not a problem, but, The problem is to implement the layout in the ListView control, such as this format. So I chose to give up, do not know that you use the ListView control to achieve this effect is not. Later I heard that the way to rewrite the ListView works, but I didn't try.

Idea two: DataGridView control, began not to think about this aspect, the Department of the Daniel said can, I carefully looked at the DataGridView control of the property, where the type of the column inside the control format does not have the control I need, you can get, but also the method rewrite. This workload is estimated to be large, and the layout can be laid out as such effect I do not know. I didn't think about it for a moment and I didn't try.

Idea three: Custom control, previously in the school to touch the custom control, but are simple splicing of the various controls, there is no special features, today deliberately learned the dictionary control, finally done a similar to the IDE inside the same control can be easily used.

The implementation of the effect no longer boil, here I write down the code I wrote:

Using System.Data;
Using System.Data.SqlClient;
Namespace Menu
{
Partial Class MyControl
{

<summary>
Clean up all the resources that are in use.
</summary>
<param name= "disposing" > True if the managed resource should be disposed, otherwise false. </param>
protected override void Dispose (bool disposing)
{
if (disposing && (components = null))
{
Components. Dispose ();
}
Base. Dispose (disposing);
}

Code generated by the #region Component Designer

<summary>
The designer supports the required method-do not
Use the Code Editor to modify the contents of this method.
</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)//Custom Data binding method
{
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 = "Work number:" +sdr["Workno"]. ToString ();
Label3. Text = "Name:" +sdr["name"]. ToString ();
Label4. Text = "type:" + sdr["leixing"]. ToString ();
Label5. Text = sdr["Time"]. ToString ();
if (sdr["leixing"]. ToString () = = "in")
{
pictureBox1.Image = imagelist1.images[1];
}
Else
{
pictureBox1.Image = Imagelist1.images[0];
}
/*if (label4. text== "Out")
{
pictureBox1.Image = imagelist1.images[1];
}*/
}

}

Public System.Windows.Forms.ImageList ImageList1;
Private System.ComponentModel.IContainer components;
}
}

Application of C # Custom controls (data binding, properties, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.