c#列表视图焦点

时间:2011-06-02 07:07:08

标签: c# .net winforms showdialog

我在关注listview时遇到问题。在我的表单中,我有一个listview,其中包含两列(Question_text,question_id)。当点击一个按钮(显示按钮)时,会打开一个对话框并显示选择了question_text和question_id.Right现在我能够在对话框中显示信息。但问题只发生在我关闭对话框时,列表视图上的焦点消失了。我关注的重点是什么关闭对话框后列表视图的项目。任何人都可以帮助我。提前谢谢。

好的,这是我的代码。

我正在阅读使用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(object sender,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();
    }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

调用ShowDialog方法时,直到关闭对话框后才会执行其后面的代码。因此,您可以将焦点设置为下一行的ListView。

例如:

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