繁体   English   中英

如何从文本框中获取文本到 c# 中的按钮事件

[英]How do I get text from the textbox into Button event in c#

我已经创建了这个代码

在 .dll 文件中创建表单并添加控件

        TextBox dbname = new TextBox();  
        dbname.BorderStyle = BorderStyle.Fixed3D;
        dbname.Location = new Point(236, 81);

        Button Create = new Button();
        Create.FlatStyle = FlatStyle.Flat;
        Create.FlatAppearance.BorderSize = 0;
        Create.Location = new Point(261, 115);
        Create.Text = "Create";
        Create.Click += new System.EventHandler(this.Create_Click);

如何从文本框中获取文本?

private void Create_Click(object sender , EventArgs e)
    {

        SaveFileDialog _sfd_ = new SaveFileDialog();

        _sfd_.Filter = "Housam Printing |*.HP";
        _sfd_.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
        _sfd_.FileName = dbname.text;
        _sfd_.Title = "Database location";
    }

为了让其他类可以访问您的控件,您需要在类级别定义它们。 然后您可以在构造函数或Form_Load事件(或您想要的任何地方)中初始化它们,并且可以从其他类方法访问它们:

public partial class Form1 : Form
{
    // Declare your controls at the class level so all methods can access them
    private TextBox dbName;
    private Button create;

    private void Form1_Load(object sender, EventArgs e)
    {
        dbName = new TextBox
        {
            BorderStyle = BorderStyle.Fixed3D,
            Location = new Point(236, 81)
        };
        Controls.Add(dbName);

        create = new Button
        {
            FlatStyle = FlatStyle.Flat,
            Location = new Point(261, 115),
            Text = "Create",
        };
        create.FlatAppearance.BorderSize = 0;
        create.Click += create_Click;
        Controls.Add(create);
    }

    private void create_Click(object sender , EventArgs e)
    {
        var sfd = new SaveFileDialog
        {
            Filter = "Housam Printing |*.HP",
            InitialDirectory = AppDomain.CurrentDomain.BaseDirectory,
            FileName = dbName.Text,
            Title = "Database location"
        };
    }
}        

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM