Results 1 to 6 of 6

Thread: Performing Percentages In VB

  1. #1
    j2k
    Guest

    Unhappy

    How can you use VB to work out percentages?

    I want it to be able to give a percentage of RAM free, based on total amount of RAM and amount of RAM free. (for example, a system with 128MB RAM has say 20MB RAM free - how can you work out a percentage in VB to put in say, a progress bar?)

    Thanks for any info.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    (segment * 100) / total
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Addicted Member dmr's Avatar
    Join Date
    Jul 2000
    Location
    West-Germany :) Timezone: GMT +1 [DST] North.......: 52° 16’ 09” East...: 10° 31’ 16”
    Posts
    255
    When you already have these two values do it with Microsoft Common Controls 1.

    Add a ProgressBar and a Command Button to your form...

    Code:
    Private Sub Command1_Click()
       Dim memFree As Single
       Dim memTotal As Single
    
       '--> here you should get the amount of memory... ;-)
    
       Dim percent As Single
    
       percent = memFree / memTotal * 100
    
       ProgressBar1.Max = 100
       ProgressBar1.Value = percent
    
    End Sub
    Hope this helps,
    Dennis.
    Last edited by dmr; Apr 8th, 2001 at 03:30 PM.
    Code that I author is neither elegant nor efficient. It is, however, functional. Once I get something that works, I'm generally satisfied with myself - I mean, if it works, that's good enough, right?

    Originally posted by aknisely
    Sorry, but I feel uncomfortable on CC with clothes on
    __________________
    The truth ... is out there!

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    To save accuracy, it's best to premultiply by 100 before dividing -- that way you get the fractional part as well
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    j2k
    Guest

    THANKS!

    It worked! Thanks loads!!

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    why not use the GlobalmemoryStatus API?

    Code:
    Option Explicit
    '//Win32API Structure
    Private Type MEMORYSTATUS
        dwLength As Long
        dwMemoryLoad As Long
        dwTotalPhys As Long
        dwAvailPhys As Long
        dwTotalPageFile As Long
        dwAvailPageFile As Long
        dwTotalVirtual As Long
        dwAvailVirtual As Long
    End Type
    
    '//Win32API Declare
    Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)
    Private Const MegaByte = 1048576
    
    Private Sub Form_Load()
        Dim MemStat As MEMORYSTATUS
        Dim um As Single
        
        '//Get the Memory status
        GlobalMemoryStatus MemStat
        
        '//Totqal memory loaded
        lblMemory(0).Caption = "Total memory loaded : " & MemStat.dwMemoryLoad & "%"
        
        '//Setup the Physical memory section
        lblMemory(2).Caption = "Total physical memory : " & Round(MemStat.dwTotalPhys / MegaByte, 0) & " MB"
        lblMemory(3).Caption = "Total available physical memory : " & Round(MemStat.dwAvailPhys / MegaByte, 0) & " MB"
        um = Round((MemStat.dwTotalPhys - MemStat.dwAvailPhys) / MemStat.dwTotalPhys * 100, 1)
        lblMemory(4).Caption = "Used " & um & "%"
        ProgressBar1.Value = um
        
        '//Setup the Virtual memory section
        lblMemory(6).Caption = "Total virtual memory : " & Round(MemStat.dwTotalVirtual / MegaByte, 0) & " MB"
        lblMemory(7).Caption = "Total available virtual memory : " & Round(MemStat.dwAvailVirtual / MegaByte, 0) & " MB"
        um = Round((MemStat.dwTotalVirtual - MemStat.dwAvailVirtual) / MemStat.dwTotalVirtual * 100, 1)
        lblMemory(8).Caption = "Used " & um & "%"
        ProgressBar2.Value = um
        
        '//Setup the PageFile memory section
        lblMemory(10).Caption = "Total pagefile memory : " & Round(MemStat.dwTotalPageFile / MegaByte, 0) & " MB"
        lblMemory(11).Caption = "Total available pagefile memory : " & Round(MemStat.dwAvailPageFile / MegaByte, 0) & " MB"
        um = Round((MemStat.dwTotalPageFile - MemStat.dwAvailPageFile) / MemStat.dwTotalPageFile * 100, 1)
        lblMemory(12).Caption = "Used " & um & "%"
        ProgressBar3.Value = um
    End Sub
    
    
    'Code improved by vBulletin Tool (Save as...)
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width