0

リストビューのフォーカスに問題があります。私のフォームには、2つの列(Question_text、question_id)を含むリストビューがあります。ボタンをクリックすると(ボタンを表示)、ダイアログボックスが開き、選択したquestion_textとquestion_id.今、ダイアログボックスに情報を表示することができます.しかし、問題はダイアログボックスを閉じたときにのみ発生し、リストビューのフォーカスが失われます.リストの同じ項目にフォーカスを残すにはどうすればよいですかダイアログボックスを閉じた後に表示します。誰か助けてくれませんか。よろしくお願いします。

これは私のコードです。

listview1_selectionIndexchanged(); を使用して、選択した項目の質問 ID を取得しています。

     private void btnAdd_Question_Click(object sender, EventArgs e)
    {
        //Add Question Dialog box is shown
        add.ShowDialog();
    }

    private void btnEdit_Question_Click(object sender, EventArgs e)
    {
        //Getting the listview selected Item
        for (int i = 0; i < listView1.SelectedItems.Count; i++)
        {

            String a1 = listView1.SelectedItems[i].Text;
            int b1 = listView1.SelectedIndices[i];
            //Open the connection
             myConnection = new SqlConnection(@"User ID=sa;Password=password123;Initial Catalog=dishtv;Persist Security Info=True;Data Source=ENMEDIA-EA6278E\ENMEDIA");
            try
            {

                myConnection.Open();
                String start_time = string.Format("SELECT Question_text  from otvtbl_question where question_id={0}", a1);
                com = new SqlCommand(start_time, myConnection);
                dr = com.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        now = DateTime.Now;
                     //Getting the start time and convert into date time Format
                       String a = dr["question_id"].ToString();
                       date = Convert.ToDateTime(a);



                    }
                    myConnection.Close();
                }
                //If data and time is greater then current time then allow the 
               // Edit question dialog box to launch
                if (date > now)
                {
                    edit.question_id = a1;

                    edit.ShowDialog();
                }
                else
                {
                    MessageBox.Show("you cant edit this question");
                }




            }

            //Catch the Exceptional error
            catch (Exception ex)
            {
                MessageBox.Show("Error while Deleteing the Data" + ex);
            }

        }

    }

その後、別のフォームを呼び出して情報を表示しています。ここでは、ダイアログ ボックスを起動するためのクラス edit.showdialog() を作成しました。

私の編集ダイアログボックスで:

ここでは、メイン フォームからダイアログ ボックスに question_id を渡し、ダイアログ ボックスに question_text を表示しています。キャンセル ボタンをクリックすると、ダイアログ ボックスが閉じます。しかし、リスト ビューの同じ項目にフォーカスが残っていません。リストビューでアイテムを選択せず​​に編集するためにもう一度編集ボタンをクリックすると、フォーカスを表示せずに前のアイテムが自動的に選択されます。

public String question_id;

private void Edit_Question_Load(オブジェクト送信者, EventArgs e) {

        EditData();
    }



   public void EditData()
    { 
         myConnection = new SqlConnection(@"User ID=sa;Password=password123;Initial Catalog=dishtv;Persist Security Info=True;Data Source=ENMEDIA-EA6278E\ENMEDIA");
           myConnection.Open();
            String question = string.Format("SELECT question_text from otvtbl_question where question_id={0}", question_id);
            com = new SqlCommand(question, myConnection);
            dr = com.ExecuteReader();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    //Assign to your textbox here   
                    txtQuestion.Text = dr["question_text"].ToString();
                }
            }
            myConnection.Close();

     private void btnCancel_Click_1(object sender, EventArgs e)
    {

        this.Close();
    }
4

2 に答える 2

1
private void btnAdd_Question_Click(object sender, EventArgs e)
{
    var focusedItem = listView1.FocusedItem;
    add.ShowDialog();
    listView1.FocusedItem = focusedItem;
}
于 2011-06-02T07:59:12.270 に答える
0

ShowDialog メソッドが呼び出されると、それに続くコードは、ダイアログ ボックスが閉じられるまで実行されません。したがって、次の行の ListView にフォーカスを設定できます。

例えば:

private void btnAdd_Question_Click(object sender, EventArgs e)
    {
        //Add Question Dialog box is shown
        add.ShowDialog();
        ListView.Focus();
    }
于 2011-06-02T07:19:11.380 に答える