在listView1_MouseClick上显示特定项目

时间:2013-08-08 10:46:02

标签: c# .net

在Quize方法中,我传递qestions,其中包含要使用DisplayQuestion()显示的所有问题的集合,Question是我的类,问题是我只显示第一个问题,如果我点击listviewItem,如果假设questions包含10个问题,那么我怎样才能显示它们,而不是listviewItem我显示的数字(1 2 3 4 5 .... 10),当我点击在每个数字上如何在点击时显示特定的Questiondisplyed,如果没有点击,如何使用计时器逐个显示所有问题

public partial class GroupExmStart : Form
 {
   string[] randomQsn = new string[totQsn + 1];   //totQsn is the total number of question  for e.g.10     
   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();
        var questions = qsn.Foo(TopiID, conf);
        int z = Quiz(questions);

        totQsn = Convert.ToInt16(conf[0]);            
        for (int kk = 1; kk <= totQsn; kk++)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = kk.ToString();
            listView1.Items.Add(lvi);
        }
        randomQsn = new string[totQsn + 1]; 
        timer1.Interval = 1000; //1000ms = 1sec
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Start();
    }
int Quiz(List<Question> questions)
        {
            foreach (Question question in questions)
            {
                DisplayQuestion(question);
            }
            return 0;
        }
private void DisplayQuestion(Question question)
        {
            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 = OP1;
            radioButton11.Text = OP2;
            radioButton10.Text = OP3;
            radioButton9.Text = OP4;
        }
private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (randomQsn.GetLength(0) >= 0)
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    //here how should i get That particular Question so that i can display it 
                    //something like this ? //Convert.ToInt16(listView1.SelectedItems[0].SubItems[0].Text)
                    DisplayQuestion(question);
                }
            }
        }
   private void timer1_Tick(object sender, EventArgs e)
    {
        tik++;
        if (tik == 60)
        {
            label1.Text = (Convert.ToInt16(label1.Text) - 1).ToString();
            tik = 0;
        }
    }
}

感谢您提前提供任何帮助

1 个答案:

答案 0 :(得分:1)

以下是您要找的内容。您必须获取列表视图项的文本,并将其用作问题的索引。

if (listView1.SelectedItems.Count > 0)
{
    var q = Convert.ToInt16(listView1.SelectedItems[0].Text);
    var selectedQuestion = questions[q - 1];
    DisplayQuestion(selectedQuestion);
}

为了使其工作,请将构造函数修改为以下内容:

private List<Question> questions;
public partial class GroupExmStart : Form
 {
   string[] randomQsn = new string[totQsn + 1];   //totQsn is the total number of question  for e.g.10     
   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();

        /// THIS IS MODIFIED //
        questions = qsn.Foo(TopiID, conf);
        int z = Quiz(questions);

        totQsn = Convert.ToInt16(conf[0]);            
        for (int kk = 1; kk <= totQsn; kk++)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = kk.ToString();
            listView1.Items.Add(lvi);
        }
        randomQsn = new string[totQsn + 1]; 
        timer1.Interval = 1000; //1000ms = 1sec
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Start();
    }