1 回答

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"
};
}
}
- 1 回答
- 0 关注
- 212 浏览
添加回答
举报