2014-02-13 30 views
1

我有一個窗體的文本框,從另一個窗體上的數據網格視圖查看數據。當窗體打開時,它將在文本框中顯示錶中選定的數據,並修剪文本以使其可讀。 我可以通過按鈕從這個表單中刪除數據庫中的數據,並且有一個關閉表單的按鈕。 我想要一個編輯按鈕,所以我可以更改文本框中的文本並單擊保存。 到目前爲止,當我點擊編輯時,關閉按鈕文本更改爲取消,但仍然關閉窗體,這是我想要的。我想更改編輯按鈕上的文字進行更新。當點擊它更新數據。是否有可能有2個ClickEvents按鈕,一個開始編輯,另一個更新記錄?或者隱藏的按鈕顯示和編輯按鈕被隱藏會更好嗎?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(); 

    } 




} 
+0

你可以添加一些代碼嗎?我的眼睛有點看到你的文本塊。 – admdrew

+0

所有表單代碼(至今)新增 – user3237403

+1

不,您只能點擊一個按鈕的事件。 – Steve

回答

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

看起來它可以工作,我會嘗試這兩種方法,看看我更喜歡哪一個!謝謝。 – user3237403

+0

完美的作品,正是我想要的。 – user3237403

0

通常情況下,從一個單一的按鈕點擊引發的事件可能有一些邏輯,例如:

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

不過,我覺得這不好的形式。我很可能會在主窗體中換出控件。

否則,您可以有2個按鈕,根據需要切換.Visible = true/false
如果您決定使用可見性來區分按鈕,則可能會發現在繼續此練習時,您無法在Designer上找到控件。

我的做法是爲特定功能構建不同的UserControl。即使只需要2個按鈕,我也可以做到這一點。這將代碼責任保留在控制本身的範圍內。

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

我想我要去切換可見和隱藏按鈕,保持代碼簡單。無論如何感謝您的回覆。 – user3237403

+0

隱藏控件的問題駐留在設計器本身中。一個控件會直接放在另一個控件上 - 我覺得這很煩人:) 我經常使用'UserControl'並交換它們。 – IAbstract