gpt4 book ai didi

c# - 点击事件查询

转载 作者:行者123 更新时间:2023-11-30 23:06:16 25 4
gpt4 key购买 nike

我有一个带有文本框的表单,可以从另一个表单的数据 GridView 中查看数据。当表单打开时,它会在文本框中显示从表中选择的数据并修剪文本以使其易于阅读。我可以通过按钮从这个表单的数据库中删除数据,并有一个按钮来关闭表单。我想要一个编辑按钮,这样我就可以更改文本框中的文本并单击保存。到目前为止,当我单击编辑时,关闭按钮文本更改为取消,但仍然关闭了表单,这就是我想要的。我想更改编辑按钮上的文本以进行更新。当点击它更新数据。是否可以为一个按钮设置 2 个 ClickEvents,一个用于开始编辑,另一个用于更新记录?或者显示隐藏按钮并隐藏编辑按钮会更好吗?

public partial class viewForm : Form
{
DataRowView Data = null;
public viewForm(DataRowView dr)
{
InitializeComponent();
Data = dr;
}

private void closeBTN_Click(object sender, EventArgs e)
{

this.Close();
}

private void viewForm_Load(object sender, EventArgs e)
{
refTxt.Text = Data["Reference"].ToString().Trim();
firstTxt.Text = Data["First Name"].ToString().Trim();
surenameTxt.Text = Data["Surename"].ToString().Trim();
address1Txt.Text = Data["Address Line 1"].ToString().Trim();
address2Txt.Text = Data["Address Line 2"].ToString().Trim();
countyTxt.Text = Data["County"].ToString().Trim();
postTxt.Text = Data["Post Code"].ToString().Trim();
contactTxt.Text = Data["Contact Number"].ToString().Trim();
emailTxt.Text = Data["Email Address"].ToString().Trim();
}

private void deleteBTN_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Customer information will be perminantly deteled. Do you with to continue? ", "Confirm Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
string constring = @"Data Source=|DataDirectory|\LWADataBase.sdf";
string Query = "delete from customersTBL where Reference ='" + this.refTxt.Text + "';";
SqlCeConnection conDataBase = new SqlCeConnection(constring);
SqlCeCommand cmdDataBase = new SqlCeCommand(Query, conDataBase);
SqlCeDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Customer information has been deleted", "Deleted Sucessfully");
while (myReader.Read())
{

}
MessageBox.Show("Please exit the Customers window and re-open to update the table");
this.Close();
//displays a system error message if a problem is found
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

}

private void editBTN_Click(object sender, EventArgs e)
{
firstTxt.ReadOnly = false;
surenameTxt.ReadOnly = false;
address1Txt.ReadOnly = false;
address2Txt.ReadOnly = false;
countyTxt.ReadOnly = false;
contactTxt.ReadOnly = false;
emailTxt.ReadOnly = false;
postTxt.ReadOnly = false;
closeBTN.Text = "Cancel";
deleteBTN.Hide();

}




}

最佳答案

不可以,同一个按钮不能有两个事件,但是,您可以根据按钮文本执行不同的操作。下面的代码只是为了演示这种可能性,我不确定是否真的理解你的场景,但你可以很容易地从这里改编

private void editBTN_Click(object sender, EventArgs e)
{
bool notEditable = true;
if(editBTN.Text == "Update")
{
UpdateDataBase();
editBTN.Text = "Edit";
deleteBTN.Visible = True;
notEditable = true;
}
else
{
deleteBTN.Visible = false;
editBTN.Text = "Update";
deleteBTN.Visible = False;
notEditable = false;
}
firstTxt.ReadOnly = notEditable;
surenameTxt.ReadOnly = notEditable;
address1Txt.ReadOnly = notEditable;
address2Txt.ReadOnly = notEditable;
countyTxt.ReadOnly = notEditable;
contactTxt.ReadOnly = notEditable;
emailTxt.ReadOnly = notEditable;
postTxt.ReadOnly = notEditable;
}

关于c# - 点击事件查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21767355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com