3

当我在 ListView 项目中选择一个特定项目时,我可以显示该特定项目,并且当表单加载时,我会默认显示最后一个项目(比如第 10 个项目)。但是,我想要的是默认显示第一项。我怎样才能做到这一点?我试图做类似的事情

listView1.Items[1].Selected = true;

但它不起作用:

public partial class GroupExmStart : Form
    {  
        string[] totDisplayQsn = null;
        string[] QAndA = null;
        string[] words = null;
        private List<Question> questions;
        ListViewItem lvi;
        public GroupExmStart(string GroupName, string DurationID)
        {
            InitializeComponent();

            this.GrpID=GroupName;
            TopiID=db.GetTopicIDForGroup(GrpID);

            string[] conf = db.GetConfiguration(Convert.ToInt16(DurationID)).Split('|');            

            Question qsn = new Question();
            questions = qsn.Foo(TopiID, conf);
            int z = Quiz(questions);
            totQsn = Convert.ToInt16(conf[0]);            
            for (int kk = 1; kk <= totQsn; kk++)//using this I am adding items to listview
            {
                lvi = new ListViewItem();
                lvi.Text = kk.ToString();
                listView1.Items.Add(lvi);                
            }

            totDisplayQsn = new string[totQsn + 1];           
           }
        int Quiz(List<Question> questions)//using this I a passing set of questions to be displayed
        {

            foreach (Question question in questions)
            {
                DisplayQuestion(question);
            }
            return 0;
        }

        private void DisplayQuestion(Question question)//using this i am displaying questions
        {
            string Q = question.Text;
            label5.Text = Q;
            string OP1 = question.Option1;
            string OP2 = question.Option2;
            string OP3 = question.Option3;
            string OP4 = question.Option4;

            radioButton12.Text = OP4;
            radioButton11.Text = OP4;
            radioButton10.Text = OP4;
            radioButton9.Text = OP4;                
            }
        }



 private void listView1_MouseClick(object sender, MouseEventArgs e)//using this i am selection particular item and displaying it
        {
            if (totDisplayQsn.GetLength(0) >= 0)
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    var q = Convert.ToInt16(listView1.SelectedItems[0].Text);
                    var selectedQuestion = questions[q-1];
                    DisplayQuestion(selectedQuestion);
                }
            }
        }

提前感谢您的帮助。

4

3 回答 3

3

尝试这个

  listView1.SelectedItems[0].Focused = true;
于 2013-08-09T11:56:47.803 回答
1

要选择第一项,请通过其从零开始的索引来访问它。将代码放在项目初始化代码之后。

for (int kk = 1; kk <= totQsn; kk++)//using this I am adding items to listview
        {
            lvi = new ListViewItem();
            lvi.Selected = false;
            lvi.Text = kk.ToString();
            listView1.Items.Add(lvi);                
        }    

listView1.Items[0].Selected = true;
DisplayQuestion(questions[0]);

删除以下代码:

    int Quiz(List<Question> questions)//using this I a passing set of questions to be displayed
    {

        foreach (Question question in questions)
        {
            DisplayQuestion(question);
        }
        return 0;
    }
于 2013-08-09T13:37:09.190 回答
0

好的,试试下面的代码:

ListViewItem foundItem = listView1.FindItemWithText("Select4", false, 0, true);
        if (foundItem != null)
        {
            listView1.Items[foundItem.Index].Selected = true;
        }

就这样。

于 2013-08-09T13:44:44.447 回答