473,796 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invalidate help needed!

6 New Member
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace timertest
  10.     {
  11.     public partial class Form1 : Form
  12.         {
  13.  
  14.         public Form1()
  15.             {
  16.             InitializeComponent();
  17.             }
  18.  
  19.         int a = 0;
  20.  
  21.         System.Windows.Forms.Timer timer = new Timer();
  22.  
  23.         System.Windows.Forms.Timer timer2 = new Timer(); 
  24.  
  25.         public void flash(object sender, EventArgs e)
  26.             {
  27.             if (this.BackColor == System.Drawing.Color.Red)
  28.                 {
  29.                 this.BackColor = System.Drawing.Color.White;
  30.                 }
  31.             else 
  32.                 {
  33.                 this.BackColor = System.Drawing.Color.Red;
  34.                 }
  35.             Invalidate();
  36.             }
  37.  
  38.         public void noflash(object sender, EventArgs e)
  39.             {
  40.                 {
  41.                 timer.Stop;
  42.                 timer2.top;
  43.                 }
  44.             }
  45.  
  46.         private void button1_Click(object sender, EventArgs e)
  47.             { 
  48.             timer.Interval = 300;
  49.             timer2.Interval = 3000;
  50.             timer.Start();
  51.             timer2.Start();
  52.             timer.Tick += new EventHandler(flash);
  53.             timer2.Tick += new EventHandler(noflash);            
  54.             }
  55.         }
  56.     }
//This is code I wrote to test a routine to cause a windows form to flash when a specific event occurs. In this code the event is button1_click. The problem I have is when I click the button the form flashes until timer2 stops at which point I presume the "noflash" method has been called by the timer2.Tick event. But when I click the button again the form does nothing. When I click the button the third time, the form flashes again, acting as if the flash action has been toggled.
Why is this happening, are the timers not actually being stopped, is timer2 really stopping or not. I am very confused, please help a guuy out.
Oct 5 '07 #1
3 1350
Plater
7,872 Recognized Expert Expert
You should not be assigning MORE event handlers everytime a button is clicked. They should be assigned once. Or assigned, then removed, and assigned again, although there's no reason for that.
Oct 5 '07 #2
mxmillercell
6 New Member
I'm not sure how that answers my question. Is assigning an event the reason the form is only flashing every other time I click? How do I get the form to flash every time I click the button?


You should not be assigning MORE event handlers everytime a button is clicked. They should be assigned once. Or assigned, then removed, and assigned again, although there's no reason for that.
Oct 10 '07 #3
mxmillercell
6 New Member
Whoa, I know what you meant, it was the problem. I moved it into the Form class declaration Thank you very much. I was pulling my hair out over this one.

I'm not sure how that answers my question. Is assigning an event the reason the form is only flashing every other time I click? How do I get the form to flash every time I click the button?
Oct 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1736
by: Alexander Jhin | last post by:
I have this very basic Custom Control: public class TestPanel : Panel { public TestPanel() : base() { this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque | ControlStyles.UserPaint, true);
3
3786
by: Marge Inoferror | last post by:
I am trying to display a number of images and have them scale when the window is resized. I have all the code to draw the window in an OnPaint() override. However, it only draws correctly the first time it is displayed. When I resize the window, it keeps the remnants of the first window and redraws only in "new" areas of the window. Is there a way to invalidate the entire window when OnPaint is invoked? I
3
17846
by: Chan | last post by:
Invalidate() is a member of System.Windows.Forms.Form. It marks an area of the client window as invalid and, therefore, in need of repainting, and then makes sure a Paint event is raised. But why we are doing it this way. If we know that something needs painting, why don't we just call OnPaint() or some other method to do the painting directly? Thank you very much
3
7524
by: jerry chapman | last post by:
Based on a mouse click I change something on the screen. I then want to repaint the screen to show the change. When I tried to use .Invalidate in my mouse routine, I got a compilation error. Here is my OnPaint code: protected override void OnPaint( PaintEventArgs paintEvent ) { Graphics g = paintEvent.Graphics;
3
3430
by: RS | last post by:
if i want to invalidate the client area of my Windows.Form and execute an overrided OnPaint i would use Form1.ActiveForm.Invalidate() ?? However, when i use this line of code i receive a : An unhandled exception of type 'System.NullReferenceException' occurred in OCRTicker.exe Additional information: Object reference not set to an instance of an object.
17
8544
by: SamSpade | last post by:
picDocument is a picturebox When I do picDocument.Invalidate() the box paints. But if instead I do picDocument.Refresh() the box does not paint. What does Refresh do. I guessed it did an Invalidate and an Update. Can someone shed some light?
4
4640
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this program is like so: static void Main() {
1
3617
by: sean | last post by:
I'm trying to create "rubber-band" rectangles by overriding the OnPaint method to place rectangles on top of all graphic controls, but when I call Me.Invalidate() (when the user moves the mouse), OnPaint is not getting called... Here is the relevent code: I'm trying to create a "rubber band" rectangle effect on my form. Private Sub HighlightSectionOfTrack(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles...
0
3038
by: raylopez99 | last post by:
Hi, I'm getting into GDI+ Forms 2.0 graphics for C#3 using Visual Studio 2008. One thing I notice: the graphics are really slow and flicker on a Pentium IV, with 2 GB RAM, even with doublebuffering turned on. I did learn tricks such as not invalidating everything, but just the control that is part of the form you are working on (i.e.,
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.