c# - 尝试显示 ListView 中的默认选定值

标签 c# .net winforms

当我在 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);
                }
            }
        }

在此先感谢您的帮助。

最佳答案

试试这个

  listView1.SelectedItems[0].Focused = true;

关于c# - 尝试显示 ListView 中的默认选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18142028/

相关文章:

c# - 无法将 'this' 作为 ref 或 out 参数传递,因为它是只读的

c# - 将 C# ExtNet DirectMethod 实现转换为 VB.NET

c# - 在 WinForms 选项卡控件中,我如何知道您正在从/向哪个选项卡移动?

c# - 在 C# .net 中单击第二个表单上的按钮时,如何从第二个表单获取主表单中的字符串数据?

c# - 将 Windows 窗体值发送到 wpf 控件以更新位于 wpf 控件中的属性。

c# - LINQ 将数组 [x0,y0, ..., xN, yN] 转换为可枚举的 [p0, ..., pN]

c# - 对象的模型绑定(bind)集合作为 ASP.Net Core MVC 中模型的一个属性

c# - MVC中如何使用多个路由

c# - 在其关联的模式定义类型中查找 xml 元素的注释

c# - 为什么Elasticsearch搜索查询返回的匹配始终为空?