C#创建一个自定义控件类

如果你希望在 TextBox 内部嵌入一个按钮,并且这个按钮用于打开文件选择对话框,可以创建一个自定义控件来实现这一功能。下面是一个示例,展示如何在 Windows 窗体应用程序中创建一个自定义控件,其中 Button 嵌入到 TextBox 内部。

首先,创建一个自定义控件类:

javascript 复制代码
using System;
using System.Drawing;
using System.Windows.Forms;

public class TextBoxWithButton : UserControl
{
    private TextBox textBox;
    private Button button;

    public TextBoxWithButton()
    {
        // 初始化 TextBox 控件
        textBox = new TextBox();
        textBox.Dock = DockStyle.Fill;
        textBox.Margin = new Padding(0);

        // 初始化 Button 控件
        button = new Button();
        button.Text = "...";
        button.Dock = DockStyle.Right;
        button.Width = 30;
        button.Margin = new Padding(0);

        // 按钮点击事件
        button.Click += new EventHandler(Button_Click);

        // 将控件添加到 UserControl 上
        this.Controls.Add(textBox);
        this.Controls.Add(button);

        // 设置 UserControl 的边距和大小
        this.Padding = new Padding(0);
        this.Size = new Size(230, textBox.Height);
    }

    private void Button_Click(object sender, EventArgs e)
    {
        // 这里可以实现按钮点击后的逻辑,例如打开文件选择器
        OpenFileDialog openFileDialog = new OpenFileDialog();
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            textBox.Text = openFileDialog.FileName;
        }
    }

    // 公开 TextBox 的 Text 属性
    public override string Text
    {
        get { return textBox.Text; }
        set { textBox.Text = value; }
    }
}
然后,在你的主窗体中使用这个自定义控件:

using System;
using System.Windows.Forms;

public class MainForm : Form
{
    private TextBoxWithButton textBoxWithButton;

    public MainForm()
    {
        // 初始化 TextBoxWithButton 控件
        textBoxWithButton = new TextBoxWithButton();
        textBoxWithButton.Location = new System.Drawing.Point(20, 20);

        // 将 TextBoxWithButton 控件添加到窗体上
        this.Controls.Add(textBoxWithButton);

        // 设置窗体属性
        this.Text = "嵌入按钮的 TextBox 示例";
        this.StartPosition = FormStartPosition.CenterScreen;
        this.Size = new System.Drawing.Size(300, 100);
    }

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

在这个示例中:

创建了一个名为 TextBoxWithButton 的自定义控件,继承自 UserControl。

在 TextBoxWithButton 控件中,创建了一个 TextBox 和一个 Button,并将它们添加到控件中。

将 Button 的 Dock 属性设置为 DockStyle.Right,将 TextBox 的 Dock 属性设置为 DockStyle.Fill,使按钮嵌入到文本框的右侧。

在按钮的点击事件中,显示一个文件选择对话框,并将选中的文件路径显示在文本框中。

在主窗体中,创建并添加 TextBoxWithButton 控件。

运行这个示例代码后,你会看到一个带有"..."按钮的文本框,按钮嵌入在文本框内部,点击按钮会打开文件选择对话框,选择的文件路径会显示在文本框中。

相关推荐
金增辉12 分钟前
基于C#的OPCServer应用开发,引用WtOPCSvr.dll
c#
wei_shuo34 分钟前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
熊猫钓鱼>_>34 分钟前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
GO兔1 小时前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
好开心啊没烦恼1 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
future14122 小时前
C#学习日记
开发语言·学习·c#
king_harry2 小时前
Java程序-OceanBase Connector/J 示例
开发语言
傻啦嘿哟3 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#
翻滚吧键盘3 小时前
js代码09
开发语言·javascript·ecmascript
q567315233 小时前
R语言初学者爬虫简单模板
开发语言·爬虫·r语言·iphone