为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
侃侃尔雅 2022-01-16 15:09:02
我已经创建了这段代码在 .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";    }
查看完整描述

1 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

为了使您的控件可供类的其他成员访问,您需要在类级别定义它们。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"

        };

    }

}        


查看完整回答
反对 回复 2022-01-16
  • 1 回答
  • 0 关注
  • 212 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号