开发者

c# List view focus

I have a problem in focusing the listview.In my form i have a listview which contains two columns(Question_text, question_id).When a button is get clicked(Show button) a dialog box is get opened and it show show the selected question_text and question_id.Right now i am able to show the information in the dialog box.But the problem only occurs only when i close the dialog box,the focus on the list view is gone.I what the focus to remain on the same item of list view after closing the dialog box.Can any one help me.Thanks in advance.

Ok this is my code.

There i am reading getting the selected item question id using the listview1_selectionIndexchanged();

     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);
            }

        }

    }

After that i am calling a another form and showing the information.Here i created a class edit.showdialog() to launch a dialog box.

In my edit dialog box:

Here I am passing the question_id from the main form to dialog box and displaying the question_text in the dialog box.When the cancel button is get clicked then the dialog box get closed.But the focus is not remaining on the same item in list view.Once again i click on edit button for editing without selecting the item in list view it select the previous one automatically without showing the focus on it.

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 SqlCom开发者_Go百科mand(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();
    }


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


When ShowDialog method is called, the code following it is not executed until after the dialog box is closed. So, you can set the focus to the ListView in next line.

Eg:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜