-
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.
-
-
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.
-
To save accuracy, it's best to premultiply by 100 before dividing -- that way you get the fractional part as well :)
-
THANKS!
It worked! Thanks loads!!
-
1 Attachment(s)
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...)