我有一个带有文本框的窗体,该窗体可从另一窗体的数据网格视图中查看数据。表单打开时,它将在文本框中显示从表中选择的数据,并修剪文本以使其易于阅读。
我可以通过按钮从此表单中从数据库中删除数据,并有一个按钮来关闭表单。
我想要一个“编辑”按钮,以便可以更改文本框中的文本,然后单击“保存”。
到目前为止,当我单击“编辑”时,“关闭”按钮的文本更改为取消,但仍关闭该窗体,这就是我想要的。我想更改“编辑”按钮上的文本以进行更新。并单击它更新数据。一个按钮是否可以有2个ClickEvent,一个可以开始编辑,另一个可以更新记录?还是最好显示隐藏按钮,然后隐藏编辑按钮?

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# - ClickEvent查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21767355/

10-10 22:30