刚刚开始程序设计的码农生涯,也许一些开发工具上的控件可以满足我们的需求,但是随之时间的迁移,我们对控件的呈现形式需求越来越多样化,这个时候就需要我们来自定义控件,我是一个刚刚入职没多久的菜鸟,接触软件开发也没有超过两年的时间,在以前的日子里,开发工具上的控件基本可以达到我学习的目的。但是现在工作了,用户的需求多样化,也造就了我们程序员的鬼斧神手。

今天由于需求,我需要作出如下图这样的界面并绑定数据库数据,开始我有几个思路,并且一个一个去尝试,对于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;
}
}

C#自定义控件的应用(数据绑定,属性等)的更多相关文章

  1. 背水一战 Windows 10 (78) - 自定义控件: 基础知识, 依赖属性, 附加属性

    [源码下载] 背水一战 Windows 10 (78) - 自定义控件: 基础知识, 依赖属性, 附加属性 作者:webabcd 介绍背水一战 Windows 10 之 控件(自定义控件) 自定义控件 ...

  2. 自定义控件的自定义的属性attrs.xml下的declare-styleable中format详解

    最近在摸索自定义控件,查找到一些自定义属性的一些资料,解决转载记载下来:看了此详解才方便理解! 我们在做项目的时候,由于android自带的属性不能满足需求,android提供了自定义属性的方法,其中 ...

  3. Silverlight用户自定义控件件中增加属性和方法

    下面的例子在用户控件MyCpmzSelect中增加了一个myCaption属性 public static readonly DependencyProperty myCaptionProperty ...

  4. android 自定义控件 使用declare-styleable进行配置属性(源码角度)

          android自定义styleableattrs源码 最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行配置,于是便查询了解了下declare-styleabl ...

  5. 【转】android 自定义控件 使用declare-styleable进行配置属性(源码角度)

    原文网址:http://blog.csdn.net/vipzjyno1/article/details/23696537 最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行 ...

  6. Vue学习之vue属性绑定和双向数据绑定

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. c# 用户控件,usercontrol,自定义控件属性

    1.C#用户控件的使用 2.拖动添加:画面上如需多个usercontrol,添加TableLayoutPanel,然后在工具箱中找到usercontrol,拖到相应框中 3.代码添加:主窗口中有多个T ...

  8. [学习总结]8、android 自定义控件 使用declare-styleable进行配置属性(源码角度)

    declare-styleable:declare-styleable是给自定义控件添加自定义属性用的. 官方的相关内部控件的配置属性文档:http://developer.android.com/r ...

  9. WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定

    原文:WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定 WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件) 上面的 ...

  10. [WPF系列]-数据邦定之DataTemplate 使用 DataTrigger 来应用属性值

    使用 DataTrigger 来应用属性值 当前表示不会告诉我们某个 Task 是家庭任务还是办公室任务.记住 Task 对象拥有类型为 TaskType 的 TaskType 属性,该类型是一个枚举 ...

随机推荐

  1. 第14章 web前端开发小白学爬虫结束语

    老猿学爬虫应该是2019年7月初开始的,到现在2个多月了,有段时间了,这部分一直是老猿期待能给大家带来收获的,因为老猿爬虫实战应用的场景与网上老猿已知的场景基本都不一样,是从复用网站登录会话信息来开发 ...

  2. Xray批量化自动扫描

    关于Xray高级版破解: https://www.cnblogs.com/Cl0ud/p/13884206.html 不过好像新版本的Xray修复了破解的BUG,亲测Xray1.3.3高级版仍然可以破 ...

  3. 华大MCU单片机之HC32F003/HC32F005 hc32f005_ddl_Rev1.9.0 Lite精简版库使用心得

    之前几个项目开发都是用的华大HC32F003_DDL_Rev1.0.2的库函数,今年刚开始入手华大,刚开始不是很了解这个芯片,看到库能用就上手了.这个版本的库编译效率很低,16K的芯片一下就写爆了.后 ...

  4. element ui中循环出来的表格勾选问题

    需求是这样的,一个房主屋里面有多个电表,每一个表是一个账户,一次只能给一个账户缴费,在点击go按钮进行缴费,这个时候判断是否跨表勾选,跨表格勾选则弹窗提示,反之符合需求,走缴费逻辑 上代码 <! ...

  5. React Native学习记录

    1.端口问题 在调试的时候,监听的是8081端口.如果被占用,会报错,并且在reload的时候导致app直接崩掉. 2.插件网站收集 https://nativebase.io/ https://js ...

  6. 数据结构与算法——循环链表的算法实现(Joseph 问题)

    Joseph 问题: 如果有10 个人,按编号顺序1,2,...,10 顺时针方向围成一圈.从1 号开始顺时针方向1,2,...,9 报数,凡报数9 者出列(显然,第一个出圈为编号9 者). 最后一个 ...

  7. .NET5下的三维应用程序开发

    终于等到了.NET5的发布,怀着激动的心情体验了一下:"香"就一个字. 如何基于.NET5开发工业软件,也广大三维应用开发者关心的问题.我们的Rapid SDK已经率先支持.NET ...

  8. PHP与Mysql之间的纠缠(超详细)

    目录 第一章 PHP操作mysql数据库 index.html代码 connect.php代码如下: list.php代码如下: 第二章 PHP 会话管理和控制 一.php 会话控制 之 PHP中的C ...

  9. Python使用requests发送请求

    Python使用第三方包requests发送请求,实现接口自动化 发送请求分三步: 1.组装请求:包括请求地址.请求头header.cookies.请求数据等 2.发送请求,获取响应:支持get.po ...

  10. DirectX 11的初始化

    总体来说可以概括为以下几个步骤: 创建Device和Context 创建SwapChain 为BackBuffer创建View 创建Depth/Stencil Buffer,并为之创建View 将Vi ...