[英]VB.NET PictureBox showed the wrong image
我正在尝试使用 PictureBox 上显示的图像集来显示设备的电池百分比。 这是我的代码(我把它放在计时器上)
`
Dim psBattery As PowerStatus = SystemInformation.PowerStatus
Dim perFull As Single = psBattery.BatteryLifePercent
Dim perFull2 As Single = perFull * 100
If psBattery.PowerLineStatus = PowerLineStatus.Online Then
If perFull2 > 96 Then
PBBatt.Image = My.Resources._100p
ElseIf 76 < perFull2 < 95 Then
PBBatt.Image = My.Resources._87p
ElseIf 51 < perFull2 < 76 Then
PBBatt.Image = My.Resources._75p
ElseIf 38 < perFull2 < 51 Then
PBBatt.Image = My.Resources._50p
ElseIf 27 < perFull2 < 38 Then
PBBatt.Image = My.Resources._37p
ElseIf perFull2 < 26 Then
PBBatt.Image = My.Resources._25p
End If
ElseIf psBattery.PowerLineStatus = PowerLineStatus.Offline Then
If perFull2 > 96 Then
PBBatt.Image = My.Resources._100up
ElseIf 76 < perFull2 < 95 Then
PBBatt.Image = My.Resources._87up
ElseIf 51 < perFull2 < 76 Then
PBBatt.Image = My.Resources._75up
ElseIf 38 < perFull2 < 51 Then
PBBatt.Image = My.Resources._50up
ElseIf 27 < perFull2 < 38 Then
PBBatt.Image = My.Resources._37up
ElseIf perFull2 < 26 Then
PBBatt.Image = My.Resources._25up
End If
End If
`
我遇到的问题是 PictureBox 仅在我的电池百分比 > 96 且 < 26 时显示正确的图像,而不是显示 _87up/_87p
我尝试使用OR
运算符(因此,我将其更改为perFull2 > 76 OR perFull2 < 95
而不是76 < perFull2 < 95
并将perFull
和perFull2
的类型更改为Integer
,但它没有用,结果仍然是与之前的代码相同。
那么,我该如何缓解这个问题呢?
您需要使用 CSng 将数字转换为单个数字,也不需要为您的条件提供范围,请参见以下代码。
Dim x As Single = 88
If psBattery.PowerLineStatus = PowerLineStatus.Online Then
If x > CSng(96) Then
MessageBox.Show("96")
ElseIf perFull2 > CSng(76) Then
MessageBox.Show("76")
ElseIf perFull2 > CSng(51) Then
MessageBox.Show("51")
ElseIf perFull2 > CSng(38) Then
MessageBox.Show("38")
ElseIf perFull2 > CSng(27) Then
MessageBox.Show("27")
Else
MessageBox.Show("26")
End If
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.