2011-06-02 74 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