• 0

[VB.NET] How to make an integer value add up / down using buttons?


Question

So basically I have a label and 2 buttons. Button A adds up 1 and button B subtracts 1.

I can't figure out why when you press the button it doesn't continue to add up / subtract down every time you press the button.

HELLLLLLLLLLLLLLLLLLP!

EDIT: I know I said integer in topic name, its supposed to be double but yeah same problem none the less.


Private Sub btnAddReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddReg.Click
' Add Regular
Dim dblRegTot As Double
dblRegTot = dblRegTot + 1
lblRegular.Text = CStr(dblRegTot)
End Sub
Private Sub btnRemReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemReg.Click
' Remove Regular
Dim dblRegTot As Double
dblRegTot = dblRegTot - 1
lblRegular.Text = CStr(dblRegTot)
End Sub
[/CODE]

4 answers to this question

Recommended Posts

  • 0

Your variables called dblRegTot are local to the function they're declared. They only exist for the duration of the function. Also, even though they have the same name, the one declared in btnAddReg_Click is a completely different variable from the one declared in btnRemReg_Click.

You need to use a variable - just one - that exists outside the functions, at the class level. That way, the value is preserved between calls to functions, and all functions within the class can add or substract to that variable. i.e.:


Public Class YourForm
Private dblRegTot = 0.0
Private Sub btnAddReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddReg.Click
' Add Regular'
dblRegTot = dblRegTot + 1
lblRegular.Text = CStr(dblRegTot)
End Sub
Private Sub btnRemReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemReg.Click
' Remove Regular'
dblRegTot = dblRegTot - 1
lblRegular.Text = CStr(dblRegTot)
End Sub
End Class
[/CODE]

  • 0
  On 13/12/2011 at 00:17, Stoffel said:

Because dblRegTot gets reset to 0 every time you run the Sub

If you would take the value of lblRegular and add or subtract 1 from that it will work

Yes!

That worked! Thank you much!

  On 13/12/2011 at 00:23, Dr_Asik said:

Your variables called dblRegTot are local to the function they're declared. They only exist for the duration of the function. Also, even though they have the same name, the one declared in btnAddReg_Click is a completely different variable from the one declared in btnRemReg_Click.

You need to use a variable - just one - that exists outside the functions, at the class level. That way, the value is preserved between calls to functions, and all functions within the class can add or substract to that variable.

Yes, I was going to do this. I just wanted to simplify this question but you guys both helped out a lot.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • In about:config, ensure these three are set to false: browser.tabs.groups.smart.enabled browser.tabs.groups.smart.optin browser.tabs.groups.smart.userEnabled
    • Excel for the web finally gets crucial data refresh feature by Usama Jawad Microsoft Excel is a very popular spreadsheet tool used for a variety of purposes in both personal and enterprise use, including data crunching, reporting, and analysis. Microsoft regularly adds new features to the software, including easier edit access and a revamped UX to fetch data from external sources. Now, the company has made some further improvements in the latter area. Excel for the web has recently netted a very important feature: the ability to refresh authenticated data sources leveraging Power Query. Previously, if you wanted to refresh an external data source in Excel for the web, you would need to download the file locally and apply the refresh there before syncing it to the web version or uploading it there. With the latest enhancement, you can simply apply Refresh All through the Data tab or refresh individual data sources through the Queries pane. Currently, refreshes are limited to 1,000 connections per user, and once you trigger a refresh, you may be prompted to enter your credentials to the authenticated data source, which includes SharePoint, SQL Server, Web API, and more. You can continue working on other aspects of your workbook while the refresh completes. In the same vein, it is now possible to view and manage data source credentials for queries authored by Power Query through the Data > Data Source Settings tab. Users have the ability to view, delete, and modify credentials at a global level or just for their current workbook. They will also be able to authenticate to a data source that already exists in the Excel workbook through the same tab. Both these capabilities are now available for all Excel for the web customers. Microsoft says that its future plans include the migration of the full-fledged Power Query experience from desktop to the web, but it is yet to share a public timeline.
    • Windows 10 KB5062649 fixes Extended Security bug, unresponsive systems, and more by Sayan Sen Microsoft has released today the non-security preview update (also known as the C-release) for Windows 10. The update, under KB5062649 and Build 19045.6159, fixes some major bugs including one for Extended Security Updates (ESU), unresponsive systems, Secure Boot, and more. Since this is a preview update for next month's Patch Tuesday it is an optional release. Microsoft says that users reported that the ESU wizard would not work correctly when they tried to use the option to extend support for Windows 10 for another year; this bug should be fixed now. The company has also fixed "stability issues" with this release as it resolves problems in the core file system. Affected users reported unresponsive systems since the May 2025 Patch Tuesday. There are other improvements as well. The full changelog is given below: ​​​​​[Extended Security Updates] Fixed: An issue impacting the Windows 10 Extended Security Updates (ESU) enrollment wizard. Some users experienced a problem where clicking “Enroll now” caused the wizard window to open, begin loading, and then close unexpectedly. This occurred due to incomplete app registration, which prevented the wizard from loading correctly. This update addresses that issue to ensure a smoother enrollment experience. [Mobile Operator Profiles] Updated: Country and Operator Settings Asset (COSA) profiles. [Secure Boot] New! Adds the ability to deploy SKUSiPolicy VBS Anti-rollback protections through the Secure Boot AvailableUpdates registry key. [Core File Systems] Fixed: An issue observed in rare cases after installing the May 2025 security update and subsequent updates causing devices to experience stability issues. Some devices became unresponsive and stopped responding in specific scenarios. [Input] Fixed: A known issue with the Microsoft Changjie Input Method. Users were unable to select words after a recent update. Fixed: A known issue when searching for an emoji in the emoji panel. After a recent update, the search always returns no results. Fixed: An issue in which phonetic input methods, including the Hindi Phonetic Input keyboard and Marathi Phonetic keyboard do not work correctly after a recent update. You can find the official support article here on Microsoft's website.
    • Are there any foldable phones where you can't actually see where the hinge is on the screen? If not, I definitely will never use one until that's possible. That makes the camera cutout look good by comparison.
    • Recall has a completely different purpose and feature-set than Timeline though. It shares some similarities, sure, but Recall isn't an evolved version of Timeline.
  • Recent Achievements

    • Week One Done
      SmileWorks Dental earned a badge
      Week One Done
    • Community Regular
      vZeroG went up a rank
      Community Regular
    • Collaborator
      Snake Doc earned a badge
      Collaborator
    • Week One Done
      Snake Doc earned a badge
      Week One Done
    • One Month Later
      Johnny Mrkvička earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      593
    2. 2
      Michael Scrip
      197
    3. 3
      ATLien_0
      193
    4. 4
      +FloatingFatMan
      131
    5. 5
      Xenon
      122
  • Tell a friend

    Love Neowin? Tell a friend!