Solved

Stopping Repeating Text In ListBox - C#

Posted on 2006-11-30
5
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

623 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