Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace ListBoxItems
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- List<string> values = new List<string>();
- values.Add("6");
- values.Add("3");
- values.Add("9");
- values.Add("1");
- values.Add("5");
- values.Add("3");
- values.Add("85");
- values.Add("85");
- values.Add("85");
- values.Add("85");
- values.Add("85");
- values.Add("5");
- listBox1.DataSource = values;
- int which = ItemsRepeated(listBox1, 5); // check if a item repeated 5 times by order.
- if (which != -(int.MaxValue))
- {
- MessageBox.Show("Item Repeated: " + which.ToString());
- }
- else
- {
- MessageBox.Show("none repeated like that!");
- }
- }
- private int ItemsRepeated(ListBox box, int times)
- {
- int attempts = 0, len = box.Items.Count;
- for(int i = 0; i < len; i++)
- {
- if(i > 0 && int.Parse(box.Items[i].ToString()) == int.Parse(box.Items[( i - 1 )].ToString()))
- {
- attempts++;
- if(attempts == times)
- {
- return int.Parse(box.Items[i].ToString());
- }
- }
- else
- {
- attempts = 1;
- if(times <= 1)
- {
- return int.Parse(box.Items[i].ToString());
- }
- }
- }
- return -(int.MaxValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement