Solved

Stopping Repeating Text In ListBox - C#

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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