Dynamically add custom controls to the flow layout with simple button paging technology

Source: Internet
Author: User

Since nothing recently, has been looking at the Java Foundation, a few days ago received the task of the superior, you have to make an access control system of the CS interface out, to achieve paging, data binding requirements such as

See here, because I basically did not contact the CS thing, a time only two or three ideas, the first idea is: the Custom control +panle control (ultimately feel not working not to do) The second idea is: Custom control +datagridview Control, try for a few days, feel a bit cumbersome, It is not necessarily possible to achieve this effect; The third idea is: custom control + Flow layout + automatic generation. I finally made it through the third Way; the fourth idea is that WCF has only this idea, not yet implemented, because it has not yet learned about WCF. First of all: I define my own custom controls first, and the code and effects are as follows:
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 released, otherwise false. </param> protected override void Dispose (bool disposing) {if (disposing && (compon Ents = null)) {components.            Dispose (); } base.        Dispose (disposing);        The code generated by the #region Component Designer///<summary>///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.componentm Odel.            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.ImageListStrea MER) (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; }}

The custom control effect is as follows:

Step Two: Stream layout + dynamically add custom controls

The implementation code and effects are as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacemenu{ Public Partial classForm3:form { PublicForm3 () {InitializeComponent (); }        Private voidForm3_load (Objectsender, EventArgs e) {                    }                Private voidButton1_Click (Objectsender, EventArgs e) {             This. FlowLayoutPanel1.Controls.Clear (); N=n+1; intCount = the*N;  for(intI=1+ the* (n1); i<=count;i++) {mycontrol MCL=Newmycontrol (); Mcl.groupBox1.Text=i.tostring (); McL.                Dataload (i);  This. FLOWLAYOUTPANEL1.CONTROLS.ADD (MCL); }        }        intn =1; Private voidButton3_Click (Objectsender, EventArgs e) {             This. FlowLayoutPanel1.Controls.Clear (); intCount = the*N;  for(inti =1; I <= count; i++) {mycontrol MCL=Newmycontrol (); Mcl.groupBox1.Text=i.tostring (); McL.                Dataload (i);  This. FLOWLAYOUTPANEL1.CONTROLS.ADD (MCL); }        }        Private voidButton2_Click (Objectsender, EventArgs e) {             This. FlowLayoutPanel1.Controls.Clear (); if(n>=2) n= N-1; intCount = the*N;  for(inti =1+ the* (N-1); I <= count; i++) {mycontrol MCL=Newmycontrol (); Mcl.groupBox1.Text=i.tostring (); McL.                Dataload (i);  This. FLOWLAYOUTPANEL1.CONTROLS.ADD (MCL); }        }            }}

Dynamically add custom controls to the flow layout with simple button paging technology

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.