2011-06-02 103 views
0

我有一個聚焦listview的問題。在我的形式我有一個列表視圖,其中包含兩列(Question_text,question_id)。當一個按鈕被點擊(顯示按鈕)一個對話框被打開,它顯示顯示所選的question_text和question_id.Right現在我能夠在對話框中顯示信息。但問題只發生在我關閉對話框時,列表視圖的焦點消失了。關閉對話框後保持在同一項列表視圖上。可以任何一個幫助我。提前感謝。c#列表視圖焦點

好吧,這是我的代碼。

在那裏我正在閱讀使用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); 
      } 

     } 

    } 

之後,我打電話一另一種形式的顯示information.Here我創建了一個類edit.showdialog()發起一個對話框。

在我的編輯對話框:

在這裏,我傳遞question_id從主窗體對話框,box.When取消按鈕的對話框中顯示QUESTION_TEXT點擊得到那麼對話框關閉掉。但重點不在於列表視圖中的同一項目。再次單擊編輯按鈕進行編輯而不選擇列表視圖中的項目時,它會自動選擇前一個項目而不顯示焦點。

public String question_id;

私人無效Edit_Question_Load(對象發件人,EventArgs的) {

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

哪裏碼? – 2011-06-02 07:08:34

+0

發佈您用來顯示對話框的代碼。 – CharithJ 2011-06-02 07:13:45

+0

我已添加代碼 – bharathi 2011-06-02 07:15:36

回答

1
private void btnAdd_Question_Click(object sender, EventArgs e) 
{ 
    var focusedItem = listView1.FocusedItem; 
    add.ShowDialog(); 
    listView1.FocusedItem = focusedItem; 
} 
0

當ShowDialog方法被調用時,它後面的代碼直到關閉對話框之後執行。因此,您可以將焦點設置到下一行的ListView中。

如:

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

我在我的代碼中添加了dialogBox窗體。看看它 – bharathi 2011-06-02 07:35:39