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 控件。

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

相关推荐
88号技师几秒前
2025年2月最新SCI-鹰鱼优化算法HawkFish Optimization Algorithm-附Matlab免费代码
开发语言·算法·matlab·优化算法
Joey_friends41 分钟前
【Linux】线程详解
开发语言
_GR1 小时前
Qt开发⑧Qt的窗口_下_浮动窗口+对话框
开发语言·css·c++·qt·microsoft
Good Note1 小时前
Golang——常用库context和runtime
开发语言·爬虫·golang
清河__1 小时前
【Go】十七、grpc 服务的具体功能编写
开发语言·后端·golang
张声录12 小时前
国密算法Sm2工具类--golang实现版
开发语言·后端·golang
HH牛码2 小时前
C# 中 Array、ArrayList 和 List 的比较
开发语言·c#
HH牛码2 小时前
C#学生管理系统 进阶(通过接口,继承接口的类,实现接口约束_对List中存储的数据进行排列)
c#
吾与谁归in3 小时前
C#实现本地Deepseek模型及其他模型的对话
人工智能·c#·wpf·deepseek
zr想努力3 小时前
Lua的table类型的增删改查操作
java·开发语言·lua