![](/img/trans.png)
[英]How can I get the text from the TextBox_KeyDown event? WPF 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.