Solved

Stopping Repeating Text In ListBox - C#

Posted on 2006-11-30
5
390 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

19 Experts available now in Live!

Get 1:1 Help Now