Solved

Stopping Repeating Text In ListBox - C#

Posted on 2006-11-30
5
391 Views
Last Modified: 2012-08-14
Hey guys,

In my program I have code to refresh the form every 20 seconds or so to see if values have been updated, this works fine.   The problem comes from a message it is to post into a listbox.  Every 20 seconds it goes through this 'loop' and posts the same message.   I know that it is because I havent got any code to tell it not to.

namespace TimerTesting
{
    public partial class frmMain:Form
    {
        Timer _RefreshFormTimer = new Timer();

        public frmMain()
        {
            InitializeComponent();
            _RefreshFormTimer.Interval = 1000 * 10;
            _RefreshFormTimer.Tick += new EventHandler(RefreshFormTimer_Tick);
            _RefreshFormTimer.Enabled = true;

        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

               private void RefreshFormTimer_Tick(object sender, EventArgs e)
        {
            GetUpdatedResults();
        }
 
        public void GetUpdatedResults();
        {
             textBox1.Text = "2";
             listBox.Items.Insert(0, "The number is 2");
       }
  }
}

Is there a way that I can stop it from posting the same message if it is already present within the listbox?  If so what code should I use?
0
Comment
Question by:angus_young_acdc
  • 3
5 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 50 total points
ID: 18045736
You can check the items in the listbox to see if it is there and only insert it if it's not, or just clear the listbox before you add it.

<pseudocode>
bool found = false;
foreach(object o in listbox.Items){
    if (o.ToString() == "The number is 2"){
        found = true;
        break;
    }
}
if (!found){
     listBox.Items.Insert(0, "The number is 2");
}
</pseudocode>
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 18046071
OR...

        public void GetUpdatedResults();
        {
             textBox1.Text = "2";
             listBox.Items.Clear();
             listBox.Items.Insert(0, "The number is 2");
       }

This simply clears the items first and then adds it
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 18046305
Cheers guys.  I did consider clearing the text box, but that would have caused more hassle than it was worth as I need to keep certain records in it.
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 18046315
I am not clearing the text box, I am clearing the ListBox
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 18046318
I see what you mean lol
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now