Share Your Source Code or Article

Do you have source code, articles, tutorials, web links, and books to share? You can write your own content here. You can even have your own blog.

Submit now...

Database Programming Made Easy

This tutorial will teach you step by step on how to connect and manipulate database. If you'd like to suggest a tutorial please write a comment at the bottom of this article.

Read more...

Hire Us to Do Your Work

Do you want a customized system? Do you want to setup your own website to do business? Then we are here to help you in your programming needs.

Read more...

Search

How to Prevent Events to Fire More Than Once
admin's picture


0
Your rating: None
Language: 

How to Prevent Events to Fire More Than Once

If you are new to VB.NET most likely you encounter a problem with events like TextChanged or ValueChanged events.

In VB 6.0, change event is not fired when changing a value programmatically. However, in the .NET version this has been changed.

In order to avoid this problem you need to call a RemoveHandler Statement.

The following code is an example of this.

  1. Public Class Form1
  2. Const curAmount As Double = 100
  3.  
  4. Private Sub ComputeTotal()
  5. txtTotal.Text = curAmount * txtDays.Text
  6. End Sub
  7.  
  8. Private Sub dtpFrom_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpFrom.ValueChanged
  9. ComputeTotal()
  10. End Sub
  11.  
  12. Private Sub dtpTo_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpTo.ValueChanged
  13. ComputeTotal()
  14. End Sub
  15.  
  16. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  17. Dim dtpStartDate As Date
  18.  
  19. 'Remove/comment the line below to simulate the error
  20. RemoveHandler dtpTo.ValueChanged, AddressOf dtpTo_ValueChanged
  21.  
  22. dtpStartDate = dtpFrom.Value
  23.  
  24. dtpTo.Value = System.DateTime.FromOADate(dtpStartDate.ToOADate + 1)
  25.  
  26. AddHandler dtpTo.ValueChanged, AddressOf dtpTo_ValueChanged
  27.  
  28. txtDays.Text = dtpTo.Value.Subtract(Format(dtpFrom.Value, "Short Date")).Days.ToString
  29.  
  30. ComputeTotal()
  31. End Sub
  32. End Class

Remove the line: RemoveHandler dtpTo.ValueChanged, AddressOf dtpTo_ValueChanged to produce the error.


Download Code



Hello admin !

Do you have a "grading utility system" which the authorized user can upload a file, an excel file in particular..i have no idea how to do it, it is our project in our visual basic programming...

pls help me

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <java>, <java5>, <javascript>, <mysql>, <php>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Step by Step Java Tutorial

In this tutorial you will learn how to program with Java. It has a rich of information to be educated well with Java.

Read more...

Do You Have Question?

Do you have any question related to computer programming? Visit our forum and post new topic on the category you like. Be gentle when asking a question.

Ask now...

Point of Sale

Point of Sale is very useful especially for supermarkets or restaurants. I have included a barcode scanner in this program. Please check it out.

Read more...