我有一个Case语句,根据所选的情况返回不同数量的零。我需要一种方法,如果零的数量大于3,则修剪掉字符串中的最后3个零。我目前有返回字符串的情况,如" 0"或" 000000"。在那种情况下,代码将转为" 000000"进入" 000"。
我目前有:
If NumberOfZeros.Length > 3 Then
NumberOfZeros.Remove(0, NumberOfZeros.Length - 3)
End If
但显然,这不起作用。任何帮助将不胜感激!
编辑:如果有人可以改进这篇文章,请做。 应评论的要求,这是有问题的案例。
Dim Colour As String = " "
Colour = Cmbl3.SelectedItem
Select Case Colour
Case "Black"
NumberOfZeros = "0"
lblcolour3.BackColor = Color.Black
Cmbr.Enabled = True
Case "Brown"
NumberOfZeros = "00"
lblcolour3.BackColor = Color.SaddleBrown
Cmbr.Enabled = True
Case "Red"
ValueOf0s = "000"
lblcolour3.BackColor = Color.Red
Cmbr.Enabled = True
Case "Orange"
NumberOfZeros = "0000"
lblcolour3.BackColor = Color.Orange
Cmbr.Enabled = True
Case "Yellow"
NumberOfZeros = "00000"
lblcolour3.BackColor = Color.Yellow
Cmbr.Enabled = True
Case "Green"
ValueOf0s = "000000"
lblcolour3.BackColor = Color.Green
Cmbr.Enabled = True
Case "Blue"
NumberOfZeros = "0000000"
lblcolour3.BackColor = Color.Blue
Cmbr.Enabled = True
Case "Violet"
NumberOfZeros = "00000000"
lblcolour3.BackColor = Color.DarkViolet
Cmbr.Enabled = True
Case "Grey"
NumberOfZeros = "000000000"
lblcolour3.BackColor = Color.Gray
Cmbr.Enabled = True
Case "White"
NumberOfZeros = "0000000000"
lblcolour3.BackColor = Color.White
Cmbr.Enabled = True
Case Else
MsgBox("You must enter a colour choice from the third option box!")
End Select
If NumberOfZeros.length > 3 Then
NumberOfZeros.Remove(0, NumberOfZeros.Length - 3)
End If
End Sub
答案 0 :(得分:1)
需要考虑的一些事项:
表示条带的类将大大缩短代码:
Public Class Stripe
Public colour As Color
Public value As Int64
Public name As String
Sub New(_colour As Color, _value As Int64, _name As String)
colour = _colour
value = _value
name = _name
End Sub
Public Overrides Function ToString() As String
Return name
End Function
End Class
创建此类的列表并将其分配给组合框的数据源属性将填充它。
Public stripes As New List(Of Stripe)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
stripes.AddRange(
{
New Stripe(Color.Black, 1, "Black"),
New Stripe(Color.Brown, 10, "Brown"),
New Stripe(Color.Red, 100, "Red"),
New Stripe(Color.Orange, 1000, "Orange"),
New Stripe(Color.Yellow, 10000, "Yellow"),
New Stripe(Color.Green, 100000, "Green"),
New Stripe(Color.Blue, 1000000, "Blue"),
New Stripe(Color.DarkViolet, 10000000, "Violet"),
New Stripe(Color.Gray, 100000000, "Grey"),
New Stripe(Color.White, 1000000000, "White")
})
cmbl3.DataSource = stripes
End Sub
现在从selecteditem获取值很简单,只需将其转换回条带对象即可:
Private Sub cmbl3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbl3.SelectedIndexChanged
Dim selectedStripe As Stripe = DirectCast(cmbl3.SelectedItem, Stripe)
lblcolour3.BackColor = selectedStripe.colour
'This assumes you have a label named lblvalue3 to display the value
If selectedStripe.value < 1000 Then
lblvalue3.Text = selectedStripe.value.ToString
Else
lblvalue3.Text = (selectedStripe.value / 1000).ToString & "K"
End If
End Sub
由于您将需要代码用于电阻器上的不同频段,因此您的代码将更加简洁。
答案 1 :(得分:0)
首先,在Case结构中返回不同数量的零听起来有点奇怪,为什么不为不同的条件使用不同的数字或位标志呢?
NumberOfZeros.length
是SO-msg中的拼写错误,还是实际代码?不应该是NumberOfZeros.Length
吗?