2014-02-13 3 views
1

다른 양식의 데이터 격자보기에서 데이터를 보는 텍스트 상자가있는 양식이 있습니다. 양식이 열리면 테이블의 선택된 데이터가 텍스트 상자에 표시되고 텍스트를 읽을 수 있도록 잘라냅니다. 버튼을 통해이 양식에서 데이터베이스의 데이터를 삭제하고 양식을 닫을 수있는 버튼이 있습니다. 수정 버튼이있어서 텍스트 상자의 텍스트를 변경하고 저장을 클릭 할 수 있습니다. 지금까지 편집을 클릭하면 닫기 단추 텍스트가 취소로 바뀌지 만 여전히 양식을 닫습니다. 이것이 내가 원하는 것입니다. 업데이트 버튼의 텍스트를 변경하고 싶습니다. 클릭하면 데이터를 업데이트합니다. 하나의 버튼에 2 개의 ClickEvent를 가질 수 있습니까? 하나는 편집을 시작하고 다른 하나는 레코드를 업데이트 할 수 있습니까? 또는 숨겨진 버튼 표시와 수정 버튼을 숨기는 것이 더 좋을까요?ClickEvent Query

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

는 그러나, 나는 나쁜 형식에서 찾기 :

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

일반적으로, 하나의 버튼 클릭에서 제기 이벤트는 다음과 같은 몇 가지 논리가있을 수 있습니다. 나는 메인 폼의 컨트롤을 바꿀 가능성이 큽니다.

그렇지 않으면 필요에 따라 .Visible = true/false을 전환하는 2 개의 버튼을 사용할 수 있습니다.
가시성을 사용하여 단추를 구별하는 경우이 연습을 계속할 때 디자이너에서 컨트롤을 찾을 수 없습니다.

나의 연습은 특정 기능에 대해 다른 UserControl을 구축하는 것입니다. 나는 2 개의 버튼을 필요로하는 것처럼 단순한 무언가로도 그렇게했습니다. 이것은 코드 책임을 컨트롤 자체의 범위 내에서 유지합니다.

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

나는 보이는 코드와 숨겨진 코드 사이를 전환하고 코드를 간결하게 유지할 것이라고 생각합니다. 어쨌든 답장을 보내 주셔서 감사합니다. – user3237403

+0

컨트롤을 숨기는 문제는 Designer 자체에 있습니다. 하나의 컨트롤이 다른 컨트롤 바로 위에 위치 할 것입니다 - 나는이 성가신 것을 발견했습니다 :) 저는 종종'UserControl's를 사용하고 그것들을 교환했습니다. – IAbstract