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

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

相关推荐
克里斯蒂亚诺更新几秒前
微信小程序 点击某个marker改变其大小
开发语言·前端·javascript
Alberta ゙1 小时前
C++初阶
开发语言·c++
the白勺1 小时前
RabbitMQ-基础-总结
开发语言·c#
Dev7z2 小时前
基于Matlab多目标粒子群优化的无人机三维路径规划与避障研究
开发语言·matlab·无人机
沐知全栈开发2 小时前
HTML 脚本:基础、应用与未来趋势
开发语言
@菜菜_达2 小时前
interact.js 前端拖拽插件
开发语言·前端·javascript
专注VB编程开发20年3 小时前
C#VB.NET中实现可靠的文件监控(新建、删除、改名、内容修改等事件的准确捕获)
spring·c#·.net·文件监控
APIshop3 小时前
实战解析:苏宁易购 item_search 按关键字搜索商品API接口
开发语言·chrome·python
百***92023 小时前
java进阶1——JVM
java·开发语言·jvm
蓝桉~MLGT3 小时前
Python学习历程——Python面向对象编程详解
开发语言·python·学习