猿问

如何从文本框中获取文本到 C# 中的 Button 事件

我已经创建了这段代码


在 .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";

    }


侃侃尔雅
浏览 188回答 1
1回答

POPMUISE

为了使您的控件可供类的其他成员访问,您需要在类级别定义它们。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"        };    }}        
随时随地看视频慕课网APP
我要回答