VB.net battery life percentage - vb.net

I tried many methods trying to make a small programs which shows the battery percentage (the value is displayed on a progressbar).Can anyone help me?

Dim power As SystemInformation.PowerStatus = SystemInformation.PowerStatus
Dim percent As Single = power.BatteryLifePercent
' Display the ProgressBar control.
pBar1.Visible = true;
' Set min and max
pBar1.Minimum = 0
pBar1.Maximum = 100
' Set the current value
pBar1.Value = percent * 100
Then you just need to refresh with a timer or something else.

Related

Word VBA Formfield number format

I am creating a form where the input of one field is used for calculations in VBA to return a value that is inserted in another text field. I am fine up to here.
Problem is, dealing with Power numbers tend to get quite high, so my code divides Watts into Kilowatts or Megawatts depending on the value.
If the number is divided by 1,000 into kW, I would like the format of the field to be "#.##0,00 kW". If it is divided by 1,000,000 into Megawatts, the format should be "#.##0,00 MW".
Any suggestions on how to do this?
My code looks something like this (previous part is irrelevant):
Dim Module As String, Power As Integer, Technology As String, TechLong As String, NumberOfCells As Integer
Module = ActiveDocument.FormFields("ModType").Result
Power = Mid(Module, 4, 3)
Technology = Mid(Module, 9, 1)
NumberOfCells = Mid(Module, 10, 2)
' Project information
Dim NumModules As Long, ProjectSize As Long, PowerField As FormField
NumModules = ActiveDocument.FormFields("ModNum").Result
ProjectSize = NumModules * Power
Set PowerField = ActiveDocument.FormFields("TotalPower")
If ProjectSize < 1000000 Then
'Change W to kW
PowerField.Result = ProjectSize / 1000
Else
'Change W to MW
PowerField.Result = ProjectSize / 1000000
End If
Thanks!
My suggestion is to break the number and power label into two separate form fields. Deactivate the "Fillin enabled" property of the power label so that the user can't change it.
Even though the user can't change the content of the "label", your code can.
This approach allows you to leave the number formatting of the power amount field alone. Changing the number format setting using your code would mean unprotecting then re-protecting the form document. It's always better to avoid that step if at all possible.
So assuming the form field for the power label is named PowerLabel your code could look like this:
PowerLabel = ActiveDocument.Formfields("PowerLabel")
If ProjectSize < 1000000 Then
'Change W to kW
PowerField.Result = ProjectSize / 1000
PowerLabel.Result = "Kw"
Else
'Change W to MW
PowerField.Result = ProjectSize / 1000000
PowerLabel.Result = "Mw"
End If

Progress bar and percent label

First and foremost: Yes I have searched and checked these:
* VB - progress bar not incrementing correct amounts?
* Progress bar and percents
Question: What is the best way to do a progress bar and a label for "percent" complete?
My Scenario: I have a check list box that I am looping thru and processing each item.
What I have tried: The first search result says to use the count as the max, as seen here:
ProgressBar1.Maximum = ListBox1.Items.Count
ProgressBar1.Increment(1)
This will work for the progress bar BUT not the text label, also if the items exceed 100, you will obviously get something like
231% Complete
etc..
My Idea was to use an IF statement and check to see if the total items is greater/less than 100 but thats where I have some issues.
This is my code, but please advise on what would be the best solution.
My Code:
Sub PercentComplete(ByVal Total As Long)
Dim Notch As Integer = 100 / Total
If Total > 100 Then
'If the total is greater than 100
Dim intPctText As Integer = Math.Round(Total / Notch, 10)
lblPct.Text = intPctText & "%"
Else
'If the total is less than 100
lblPct.Text = Notch.ToString & "%"
End If
End Sub

Populating reports with calculated values

I hope this is a simple question and you don't have to waste too much of you time on it.
I have an report (called repRAD78) which contains a textbox (called txtRAD8). I would like to populate txtRAD8 with a calculated value based on numbers pulled from a query called qryrRAD78.
Looking through the forums it looks like recordsets would be the solution but this is my first foray into recordsets and it's not going well. :(
The code I have pasted in below I have pulled together from a number of places and it doesn't produce any errors but puts the same value into txtRAD8 for all the records.
I'm sorry if this is a stupid question but it's been driving me potty.
Many thanks for your time.
Al.
Public Sub Calc()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("qryrRAD78")
rst.MoveFirst
Do Until rst.EOF = True
Dim lngMean As Long
Dim lngRAD78max As Long
Dim lngRAD78_1 As Long
Dim lngRAD78_2 As Long
Dim lngRAD78_3 As Long
Dim lngRAD7 As Long
Dim lngRAD8 As Long
lngRAD78_1 = rst![RAD78_1]
lngRAD78_2 = rst![RAD78_2]
lngRAD78_3 = rst![RAD78_3]
lngRAD8b_c = rst![RAD8b_c]
lngMean = (lngRAD78_1 + lngRAD78_2 + lngRAD78_3) / 3
lngRAD78max = Maximum(Abs(lngRAD78_1), Abs(lngRAD78_2), Abs(lngRAD78_3))
lngRAD7 = ((lngRAD78max - lngMean) / lngMean) * 100
lngRAD8 = ((lngMean - lngRAD8b_c) / lngRAD8b_c) * 100
txtRAD8.Value = lngRAD8
rst.MoveNext
Loop
rst.Close
dbs.Close
End Sub
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Calc
End Sub
Here's a second approach to this. Rather than using a function in the code, take the calculations from your Calc() routine and put them in another query.
SELECT idrRAD78,
(RAD78_1 + RAD78_2 + RAD78_3) AS Mean,
(IIf(Abs(RAD78_1) > Abs(RAD78_2),
IIf(Abs(RAD78_1) > Abs(RAD78_3), RAD78_1, RAD78_3),
IIf(Abs(RAD78_2) > Abs(RAD78_3), RAD78_2, RAD78_3))) AS RAD78Max,
(((RAD78max - Mean) / Mean) * 100) AS RAD7,
(((Mean - RAD8b_c) / RAD8b_c) * 100) AS RAD8
FROM qryrRAD78
This will give you a query that performs the same calculations as your existing function. Then just edit the report query to join to this new query (just like joining a table) using something like:
FROM ReportQuery INNER JOIN NewQuery ON ReportQuery.idrRAD78 = NewQuery.idrRAD78
Change the query names to match the real names. Add the fields from the new query in the SELECT part of your report query:
SELECT <existing field list>, RAD7, RAD8
Then set txtRAD8 to the RAD8 field.
I'm just doing this from memory as I'm not in front of my own computer, but hopefully that makes sense and is close enough to the correct code.
The problem with this function is that every row on the report is going to have a textbox called txtRAD8. So what you are really doing is updating every textbox on the report with the same value (once for every loop through the recordset). You are not actually setting the value for each individual row.
What you need to do is make the value of the textbox = Calc(RowID). Then your query uses the passed-in parameter to get the value for that one record instead of looping through the whole recordset, and updates just that one row on the report.
So your Sub becomes a Function, and returns the calculated value.

VIsual Basic - Scan same item

How to check if textbox contains the same barcode of the previous item scaned so that if it does, i will automatically increase quantity something like this:
Dim nextItemBarcode As String = Me.txtBarkodi.Text
Dim quantity As Integer = 1
If Me.txtBarkodi.Text = nextItemBarcode Then
quantity += 1
Else
quantity = 1
End If
Do you think I am missing sth or could there be a better algorithm for this scenario?
You're missing something. :-)
You need to store the value of the last bar code, not the next bar code. If the new one is the same as the last, you increment the quantity. If it's not the same, you reset quantity to one and store the new bar code as the last bar code.
Dim lastItemBarcode As String = ""
Dim quantity As Integer
' Scan now. If this is the first bar code,
' quantity will be set to 1 in the Else branch below
' Your scan should work in a loop starting here, so
' it keeps going and doesn't reset lastItemBarcode
' or quantity
If Me.txtBarkodi.Text = lastItemBarcode Then
' bar code same as last. Just increase quantity
quantity += 1
Else
' Either the first item scanned, or a different
' item. Save this bar code as the current one,
' and start our queantity at 1
lastItemBarcode = me.txtBarkodi.Text
quantity = 1
End If

Graphical representation of data

I am totally new to the graphical representation of the data. I want to make a progress report of students, based on marks achieved each year.
For example, in year 2005 marks were 750. in 2006 780 in 2007 800
I want to show it graphically. Could any body give me code example?
Thanks a lot.
Use a label and set the Label.Width = intValue where intValue is the value you want to show (you can divide it by a factor to make sure it stays within a certain bound). They you an play around with lable colors or backgrounds for different value ranges. E.g.
assume valuelist is the list of values
Dim graphicallabels(n) As Label
For i As Integer = 0 To valuelist.length
graphicallabels(i) = New Label
With graphicallabels(i)
.Location = New Point(0, i * 2 * graphicallabels(i).Height)
.Width = valuelist(i)
End With
If valuelist(i)> 50 Then
graphicallabels(i).BackColor = Color.Green
Else
graphicallabels(i).BackColor = Color.Red
End If
Next
Note: untested code
HTH