简体   繁体   中英

Trying to display default selected value from ListView

When I select a particular item in ListView item, I am able to display that particular item, and when the form loads I am getting the last item displayed by default (say the 10th item). However, what I want is to display the first item by default. How can I do this? I tried to do something like

listView1.Items[1].Selected = true;

but it's not working:

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

Thanks in advance for any help.

试试这个

  listView1.SelectedItems[0].Focused = true;

To select the first item, access it by its zero based index. Place the code just after your items initialization code.

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

Remove the following code:

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

Ok try the below code:

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

That's All.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM