C# .NET - Link ComboBox with Last and Next Button

Asked By kunal mehta on 25-Aug-08 03:00 AM

I have comboBox that has some values. Now I want to link this with Last and Next button. If I Click Last button combobox will display value -1 to current value and If I click next than it will next value from Current value.

Please post code snippet for this

Link the buttons with cboList.SelectedIndex - Kalit Sikka replied to kunal mehta on 25-Aug-08 03:03 AM

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

cboList.Items.Add("First");

cboList.Items.Add("Second");

cboList.Items.Add("Third");

cboList.Items.Add("Fourth");

}

public private void btnLast_Click(object sender, EventArgs e)

{

if(cboList.SelectedIndex > 0)

{

int k = cboList.SelectedIndex;

cboList.SelectedIndex = k-1;

}

}

private void btnNext_Click(object sender, EventArgs e)

{

if(cboList.SelectedIndex < cboList.Items.Count-1)

{

int k = cboList.SelectedIndex;

cboList.SelectedIndex = k+1;

}

}

write the code in buttons click event - Deepak Ghule replied to kunal mehta on 25-Aug-08 03:05 AM

write the code in last button click event

comboBox1.CurrnetIndex=comboBox1.items.Count -1


write the code in next button click event

if (comboBox1.CurrnetIndex > comboBox1.items.Count -1)

comboBox1.CurrnetIndex=comboBox1.CurrnetIndex  + 1


Try this code - Atul Shinde replied to kunal mehta on 25-Aug-08 03:07 AM

Combobox1.SelectedIndex is the property which u can use.

ComboBox1.SelectedIndex = -1;

AND

ComboBox1.SelectedIndex = ComboBox1.SelectedItem.Count - 1

hi - Binny ch replied to kunal mehta on 25-Aug-08 03:16 AM
Hi try this,

 private void button4_Click(object sender, EventArgs e)
        {
            int i=comboBox1.SelectedIndex-0;
           string s = i.ToString();
           comboBox1.Text = s.ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int i = comboBox1.SelectedIndex +2;
            string s = i.ToString();
            comboBox1.Text = s.ToString();
        }
u can do as - Web Star replied to kunal mehta on 25-Aug-08 03:24 AM

public Load()

{

InitializeComponent();

cboList.Items.Add("First");

cboList.Items.Add("Second");

cboList.Items.Add("Third");

cboList.Items.Add("Fourth");

}

public private void btnLast_Click(object sender, EventArgs e)

{

//first u store current index in another variable

Currnetindex = cboList.SelectedIndex

//then u  need to just set start index this show from start to current as well as all

cboList.SelectedIndex = -1;

 

}

}

private void btnNext_Click(object sender, EventArgs e)

{

//here u set previous current index to next value

cboList.SelectedIndex = Currnetindex ;

}

}

NOte:-

if u wnat to show only one option then u need to set visible false for selected item in  a loop

See this code - Sagar P replied to kunal mehta on 25-Aug-08 03:30 AM

private void btnFirst_Click(object sender, EventArgs e)

{

comboBox1.SelectedIndex = 0;

}

private void btnPrevious_Click(object sender, EventArgs e)

{

if (comboBox1.SelectedIndex > 1)

{

comboBox1.SelectedIndex = comboBox1.SelectedIndex - 1;

}

}

private void btnNext_Click(object sender, EventArgs e)

{

if (comboBox1.SelectedIndex < comboBox1.Items.Count - 1)

{

comboBox1.SelectedIndex = comboBox1.SelectedIndex + 1;

}

}

private void btnLast_Click(object sender, EventArgs e)

{

comboBox1.SelectedIndex = comboBox1.Items.Count - 1;

}

Best Luck!!!!!!!!!!!!!!!!!!!!!
Sujit.

Try this... - san san replied to kunal mehta on 25-Aug-08 03:49 AM
Hi

Try the code bellow..
It may help u out...

Let's take 15 items and display 5 items per click on Next & Previous buttons
If u click on "Next",  items  1-5  will be displayed.
If u click once again on "Next", items 6-10 will be displayed.
If u click once again on "Next", items 11-15 will be displayed.
then
If you click on "Previous", items 6-10 will be displayed.
If you click on "Previous", items 1-5 will be displayed.


Arraylist list = new ArrayList();
            list.Add("Item-1");
            list.Add("Item-2");
            list.Add("Item-3");
            list.Add("Item-4");
            list.Add("Item-5");
            list.Add("Item-6");
            list.Add("Item-7");
            list.Add("Item-8");
            list.Add("Item-9");
            list.Add("Item-10");
            list.Add("Item-11");
            list.Add("Item-12");
            list.Add("Item-13");
            list.Add("Item-14");
            list.Add("Item-15");
            btnNext.Tag = 5;

code for btnNext
  private void btnNext_Click(object sender, EventArgs e)
        {
            int start =Convert.ToInt32(btnNext.Tag.ToString());
            if (start >= list.Count)
            {
                return;
            }
            comboBox1.Items.Clear();
            int i = start;
            for (i = start; i < start + 5; i++)
            {
                comboBox1.Items.Add(list[i].ToString());
            }
            if (comboBox1.Items.Count > 0)
                comboBox1.SelectedIndex = 0;
            // set the tag to the current endpoint
            btnNext.Tag = i;
        }

code for btnPrevious
private void btnPrevious_Click(object sender, EventArgs e)
        {
// take the endpoint index of btnNext
            int start = Convert.ToInt32(btnNext.Tag.ToString());
            start = start - 5;
            start = start - 5;
            if (start < 0)
            {
                return;
            }
            comboBox1.Items.Clear();
            int i = start;
            for (i = start; i < start + 5; i++)
            {
                comboBox1.Items.Add(list[i].ToString());
            }
            if (comboBox1.Items.Count > 0)
                comboBox1.SelectedIndex = 0;
            // set the tag to the current endpoint index to btnNext
            btnNext.Tag = i;
        }


Hope this helps
SAN