如何在x秒内自动选择下一个组合框选择?

时间:2018-12-03 15:17:45

标签: c# winforms

使用C#,Windows窗体。

我正在尝试使程序每x秒自动选择组合框上的下一个项目,一旦到达最后一个项目,它就会返回到列表中的第一个项目。我得到了几乎所有东西减去自动组合框选择部分。 :(

请帮助。

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }       

    private void Form1_Load(object sender, EventArgs e)
    {
        //pictureBox1.Image = Image.FromFile(@"Z:\DSCF1661.jpg");

        DirectoryInfo test = new DirectoryInfo(@"C:\temp");//Assuming Test is your Folder
        FileInfo[] Files = test.GetFiles("*.pdf"); //Getting Text files

        comboBox1.DataSource = Files;
        comboBox1.DisplayMember = "Name";
        timerset();
    }


    public void axSetting()
    {
        axAcroPDF1.setShowToolbar(false);
        axAcroPDF1.setView("FitH");
        axAcroPDF1.setPageMode("none");
        axAcroPDF1.setLayoutMode("SinglePage");
        axAcroPDF1.Show();
    }



    private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        axAcroPDF1.LoadFile(@"C:\temp\" + comboBox1.Text);
        axAcroPDF1.src = @"C:\temp\" + comboBox1.Text;
        axSetting();

    }
    //private System.Windows.Forms.Timer timer1;


    public void comboBoxSelect()
    {
        if (comboBox1.SelectedIndex < comboBox1.Count) // this part... :(
        {
            comboBox1.SelectedIndex += 1;
        }
        else
        {
            comboBox1.SelectedIndex = 0;
        }
    }

    public void timerset()
    {
        timer1 = new System.Windows.Forms.Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 5000; // in miliseconds
        timer1.Start();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        comboBoxSelect();
    }

1 个答案:

答案 0 :(得分:1)

这应该有效:

if(combobox.SelectedIndex < (combobox.Items.Count -1))
{
    combobox.SelectedIndex += 1;
}
else
{
    combobox.SelectedIndex = 0;
}