简体   繁体   English

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

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

I have created this code我已经创建了这个代码

created form in .dll file and added controles在 .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);

How do I get text from the text box?如何从文本框中获取文本?

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";
    }

In order to make your controls accessible to the rest of your class, you need to define them at the class level.为了让其他类可以访问您的控件,您需要在类级别定义它们。 Then you can initialize them in the constructor or the Form_Load event (or wherever you want), and can access them from other class methods:然后您可以在构造函数或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