我在聚焦列表视图时遇到问题。在我的表单中,我有一个列表视图,其中包含两列(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。单击取消按钮时,对话框将关闭。但焦点不会保留在列表视图中的同一项目上。我再次单击编辑按钮进行编辑,而不在列表视图中选择项目,它会自动选择前一个而不显示焦点。
公共字符串 question_id;
私人无效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();
}