自定义控件---动态无限级生成 级联comboBox 和 treeView

本文介绍了如何创建一个动态无限级的级联ComboBox和TreeView控件。通过新建Windows窗体控件库项目,实现了控件的功能,并提供了在WinForm中调用这些控件的步骤。在实际应用中,需要与数据库的Area表关联以填充数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始 -〉新建 -〉新建项目 -〉“Windows窗体控件库”

名称栏 里 改为: myControl1

注: 若要添加城市国家区县等记录,请关联着 Area 表一同进行


上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace myControls1
{
    public partial class UserControl1 : UserControl
    {
        private int LeftComboBox = 90;
        private int TopComboBox = 50;
        private int LeftRadioButton = 40;
        private int TopRadioButton = 48;
        private int Vertical = 45;
        public int ctWidth = 270;// tabControl 控件 属性
        public int ctHeight = 480;

        private TabControl tc = new TabControl();
        private TabPage tp层级 = new TabPage();
        private TabPage tp树型 = new TabPage();
        private TreeView tv = new TreeView();
        private Button button确定 = new Button();
        private RadioButton radioButtonTemp = new RadioButton();//临时的、不可见的

        private string connStr = "";
        private int _id_返回值 = 0;
        public bool flag { get; set; }
        public string tab表名1 { get; set; }
        public string tab表名2 { get; set; }

        public int id_返回值
        {
            get { return _id_返回值; }
        }

        private SqlConnection sqlconn;
        private DataSet ds = new DataSet();

        public UserControl1(string s)//s 系传进来的数据库连接字符串
        {
            connStr = s;
            InitializeComponent();
            sqlconn = new SqlConnection(connStr);
            flag = false;
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {
            setMainControls(tab表名1, tab表名2);
        }

        /// <summary>
        /// tablename1 和 tablename2 系表名
        /// </summary>
        /// <param name="tablename1"></param>
        /// <param name="tablename2"></param>
        private void setMainControls(string tablename1, string tablename2)
        {
            tc.Location = new Point(40, 30);
            tc.Width = ctWidth;
            tc.Height = ctHeight;

            tp层级.Text = "层级";
            tp树型.Text = "树型";

            tv.Dock = DockStyle.Fill;

            tc.C
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值