2016-07-03 34 views
-2

我在寫用戶控件。我希望它在用戶雙擊客戶時返回客戶編號。我似乎無法得到它的工作。顯示用戶控件,雙擊時,數據顯示在消息框中,但似乎無法使其更新主窗體上的值。使用用戶控件的表單不會返回值

每當我試圖返回值加入到FindCustomerControl_ItemHasBeenSelected,我得到一個錯誤。這就像它掛在用戶控件中,我不能讓它返回值。到目前爲止,這是我所:

在我的主窗口:

public partial class TestForm : Form 
{ 
    public TestForm() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     // one close button on the form 
     Close(); 
    } 


    private void ShowSelectFromListWidget() 
    { 
     // show the user control 
     var uc = new FindCustomerControl(); 
     uc.ItemHasBeenSelected += uc_ItemHasBeenSelected; 

     MakeUserControlPrimaryWindow(uc); 
    } 

    void uc_ItemHasBeenSelected(object sender, 
         FindCustomerControl.SelectedItemEventArgs e) 
    { 
     // once it has been selected, change a label on the screen to show the customer number 
     var value = e.SelectedChoice; 
     lblCustomer.Text = value; 
    } 

} 

在我的用戶控制:

public partial class FindCustomerControl : UserControl 
{ 
    public class SelectedItemEventArgs : EventArgs 
    { public string SelectedChoice { get; set; } } 

    public event EventHandler<SelectedItemEventArgs> ItemHasBeenSelected; 


    public FindCustomerControl() 
     { InitializeComponent();} 


    DataTable dt = new DataTable(); 
    public void btnFind_Click(object sender, EventArgs e) 
    { var dt = GetData(); 
     dataGridView1.DataSource = dt; } 

    //Query database 
    public DataTable GetData() 
    { 
     UtilitiesClass ru = new UtilitiesClass(); 
     string connectionString = ru.getConnectionString(); 

     DataTable dt = new DataTable(); 

     SqlConnection myConnection = new SqlConnection(connectionString); 
     myConnection.Open(); 
     SqlCommand cmd = new SqlCommand("FindCustomer", myConnection); 
     cmd.Parameters.AddWithValue("@customer", txtCustomer.Text.Trim()); 
     cmd.CommandType = CommandType.StoredProcedure; 
     SqlDataAdapter ta = new SqlDataAdapter(cmd); 
     ta.Fill(dt); 
     myConnection.Close(); 
     return (dt); 
    } 

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
    { 
     var handler = ItemHasBeenSelected; 
     string choice = dataGridView1[0, e.RowIndex].Value.ToString(); 
     // this shows it 
     MessageBox.Show("Chosen: " + e.SelectedChoice); 
     if (handler != null) handler(this, new SelectedItemEventArgs { SelectedChoice = choice }); 
     // I WOULD LIKE TO RETURN A VALUE HERE 
    } 


} 

它好像這應該是很常見,但儘管我的研究和調試時間,我一直無法提出解決方案。我知道,uc.ItemHasBeenSelected + = uc_ItemHasBeenSelected;在TestForm似乎並沒有得到執行,因爲我在那裏放置了一個斷點。

+0

「任何時候我嘗試添加一個返回值到FindCustomerControl_ItemHasBeenSelected,我得到一個錯誤。」 [你到底在哪個錯誤,你如何添加它](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)?另外,如果我猜對了你試過的東西,那麼[this](http://stackoverflow.com/questions/1210026/return-a-value-from-an-event-is-there-a-good-practice-for - 這)可能會給你一些想法。 –

+0

你說值在'MessageBox'顯示,但價值並不在'TestForm.lblCustomer'文本框中顯示的,這意味着你的'TestForm.uc_ItemHasBeenSelected()'方法不會被調用。由於顯示了消息框,因此'ItemHasBeenSelected'事件顯然正在按預期提升。正如我所看到的那樣,這留下了兩種可能性:您的'TestForm'沒有訂閱該事件,或者您的'lblCustomer'實際上正在更新,但出於某種原因,標籤文本不可見。 –

+0

如果沒有一個能夠可靠地再現問題的好的[mcve],那麼僅僅通過你的問題就不可能知道問題是什麼。要麼改善問題以便回答問題,要麼自己做更多的調試。請注意,您的代碼顯示訂閱_some_對象上的事件,但由於您未顯示'MakeUserControlPrimaryWindow()',因此無法確認您是否訂閱了_correct_對象上的事件。對於我們所知道的,你已經得到了實際上接受用戶輸入的另一個用戶控件實例。 –

回答

0

用戶類沒有任何問題。它工作正常。問題在於TestForm需要在沒有FindCustomerControl的情況下啓動,並在程序中實例化控件。它將值返回到標籤或其他需要的地方。非常感謝布拉德·雷姆和這個職位:Return value from usercontrol after user action

public partial class TestForm : Form 
    { 
    public TestForm() 
    { 
     InitializeComponent(); 
     ShowSelectFromListWidget(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 


    private void ShowSelectFromListWidget() 
    { 
     var uc = new FindCustomerControl(); 
     uc.ItemHasBeenSelected += uc_ItemHasBeenSelected; 
     this.MakeUserControlPrimaryWindow(uc); 
    } 

    void uc_ItemHasBeenSelected(object sender, FindCustomerControl.SelectedItemEventArgs e) 
    { 
     var value = e.SelectedChoice; 
     lblCustomer.Text = value; 
     lblMerchant.Focus(); 

     //ClosePrimaryUserControl(); 
    } 

    private void MakeUserControlPrimaryWindow(UserControl uc) 
    { 
     // my example just puts in in a panel, but for you 
     // put your special code here to handle your user control management 
     panel1.Controls.Add(uc); 
    } 


    private void ClosePrimaryUserControl() 
    { 
     // put your special code here to handle your user control management 
     panel1.Controls.Clear(); 
    } 

}

1

我的理解我想你可以使用應用現有資源,在那裏你可以保存任何你希望的值 - 在UWP它是這樣的:

Application.Current.Resources["Name"] = customerNumber 

然後你可以將此值轉換爲所需的類型:

(int)Application.Current.Resources["Name"] 

現在你可以在任何你想要的地方使用這個值。

希望不是以任何方式幫助。

+0

非常感謝您的幫助。這是一個好主意,但我會有很多用戶,所以這次可能不適合我。不過,我知道我會有一段時間有用。 – Missy

+0

您可以隨時將數組分配給當前資源,該資源將包含所有客戶,然後將其作爲數組投入使用 –

+0

好主意。我會用它。 – Missy

相關問題