私はユーザーコントロールを作成しています。ユーザーが顧客をダブルクリックしたときに顧客番号を返すようにしたい。私はそれを働かせるように見えない。ユーザーコントロールが表示され、ダブルクリックするとデータがメッセージボックスに表示されますが、メインフォームの値を更新することはできません。フォームでユーザーコントロールを使用しても値が返されない
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では実行されていないようです。
「私はFindCustomerControl_ItemHasBeenSelectedに戻り値を追加しようといつでも、私はエラーを取得します。」。 [正確にどのエラーが表示され、どのように追加しようとしていますか?](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)?また、私があなたが試したものが正しいと推測した場合、[これは](http://stackoverflow.com/questions/1210026/return-a-value-from-an-event-is-there-a-good-practice-for - これはあなたにいくつかのアイデアを与えるかもしれません。 –
あなたは 'MessageBox'に値が表示されていますが、' TestForm.lblCustomer'テキストボックスに値が表示されておらず、 'TestForm.uc_ItemHasBeenSelected()'メソッドが呼び出されないことを意味します。メッセージボックスが表示されているので、 'ItemHasBeenSelected'イベントは明らかに期待どおりに発生しています。 'TestForm'がイベントに登録されていないか、' lblCustomer'が実際に更新されていますが、何らかの理由でラベルテキストが見えません。 –
問題を確実に再現する良い[mcve]がなければ、問題が何であるかを知ることはできません。答えることができるように質問を改善するか、もっと自分でデバッグしてください。あなたのコードは_some_オブジェクトのイベントを購読していることを示していますが、 'MakeUserControlPrimaryWindow()'を表示していないので、あなたが_correct_オブジェクトのイベントを購読したことを確認することはできません。実際にユーザーの入力を受け入れる別のインスタンスのユーザーコントロールがあることはわかっています。 –