Solved

Stopping Repeating Text In ListBox - C#

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

778 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