ClickEvent查询

时间:2014-02-13 22:55:53

标签: c# mysql database

我有一个带有文本框的表单,可以在另一个表单上查看数据网格视图中的数据。当表单打开时,它会在文本框中显示表格中的选定数据,并修剪文本以使其可读。 我可以通过按钮从这个表单中删除数据库中的数据,并有一个关闭表单的按钮。 我想要一个编辑按钮,所以我可以在文本框中更改文本并单击“保存”。 到目前为止,当我单击编辑时,关闭按钮文本更改为取消,但仍然关闭表单,这是我想要的。我想更改编辑按钮上的文本进行更新。点击它时会更新数据。是否可以为一个按钮设置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();

    }




}

2 个答案:

答案 0 :(得分:0)

通常,单击按钮引发的事件可能具有以下逻辑:

void MyButtonClicked(object sender, EventArgs e) {
   if(((Button)sender).Text == "Save"){
      // do this
   } else {
      // do that
   }
}

然而,我觉得这种形式很糟糕。我很可能在主窗体中换出控件。

否则,您可以根据需要在其中切换.Visible = true/false的2个按钮 如果您决定使用“可见性”来区分按钮,则可能会发现在继续此练习时,您无法在Designer上找到控件。

我的做法是为特定功能构建不同的UserControl。即使只需要2个按钮,我也能做到这一点。这使代码责任在控件本身的范围内。

//Psuedo-code
if(operation == Edit) {
   MyPanel.Load(EditControls);
} else if (operation == Save) {
   MyPanel.Load(SaveControls);
}

答案 1 :(得分:0)

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

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;
}