我对编程很陌生,但昨天我设法在VBA for Excel中编写了一个简单的转换计算器。由于代码并不真正需要Excel,我发现我想让它成为一个独立的应用程序并下载Visual Basic。
工作一晚后,将VBA for Excel代码迁移到Visual Basic,我终于设法让主代码功能正常工作。
但是,如果数字位数超过5,我想以科学记数法输出textbox1结果。我试图直接使用VBA代码,虽然它没有导致错误,但它没有&#39 ; t改变任何东西。
这是我的VBA代码:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rates(0 To 5, 0 To 5) As Double, i As Integer, j As Integer
rates(0, 0) = 1
rates(0, 1) = 86400
rates(0, 2) = 100
rates(0, 3) = 8640000
rates(0, 4) = 283460
rates(0, 5) = 2120428.8
rates(1, 0) = 0.000011574
rates(1, 1) = 1
rates(1, 2) = 0.0011574
rates(1, 3) = 100
rates(1, 4) = 3.2808
rates(1, 5) = 24.542
rates(2, 0) = 0.01
rates(2, 1) = 864
rates(2, 2) = 1
rates(2, 3) = 86400
rates(2, 4) = 2834.6
rates(2, 5) = 21204.288
rates(3, 0) = 0.00000011574
rates(3, 1) = 0.01
rates(3, 2) = 0.000011574
rates(3, 3) = 1
rates(3, 4) = 0.032808
rates(3, 5) = 0.24542
rates(4, 0) = 0.0000035278
rates(4, 1) = 0.3048
rates(4, 2) = 0.00035278
rates(4, 3) = 30.4805
rates(4, 4) = 1
rates(4, 5) = 7.4333
rates(5, 0) = 0.000000474600000000001
rates(5, 1) = 0.0410054400000001
rates(5, 2) = 0.0000474600000000001
rates(5, 3) = 4.10054400000001
rates(5, 4) = 0.134530116
rates(5, 5) = 1
For i = 0 To 5
For j = 0 To 5
If FraEnhet.SelectedIndex = i And TilEnhet.SelectedIndex = j Then
TextBox2.Text = TextBox1.Text * rates(i, j)
End If
Next j
Next i
End Sub
Private Sub TextBox2_Change()
If Len(TextBox2) > 5 Then
TextBox2.Text = FormatNumber("0.00E+00")
End If
End Sub
End Class
这是我的Visual Basic代码:
enum
如果有人也可以告诉我如何设置双击并输入关键事件,我们将不胜感激。
由于
答案 0 :(得分:0)
您可以使用.ToString()
方法输出科学概念。例如
TextBox1.Text = (0.000000474600000000001).ToString("0.00E+00")
' OR
TextBox1.Text = value.ToString("0.00E+00")
您可以阅读有关自定义数字格式here
的更多信息至于活动,您可以查看this(不是VB),但会指出正确的方向使用闪电'用于选择要挂接的事件的图标。或者你可以创建一个handles
像这样的事件的子
Private sub Textbox_changed() Handles TextBox1.TextChanged
'Textbox change event
End sub
答案 1 :(得分:0)
这里有一些建议:
在Button1_Click()
中,您不需要任何循环,只需转到:
TextBox2.Text = TextBox1.Text * rates(FraEnhet.SelectedIndex, TilEnhet.SelectedIndex)
至于TextBox2格式化问题我会选择:
TextBox2.Text = Convert.ToDouble(TextBox2.Text).ToString("0.00E+00")
但我宁愿将其保留在Button1_Click()
而不是其他事件处理程序(TextBox2_TextChanged()
)中:
TextBox2.Text = TextBox1.Text * rates(FraEnhet.SelectedIndex, TilEnhet.SelectedIndex)
If Len(TextBox2.Text) > 5 Then TextBox2.Text = Convert.ToDouble(TextBox2.Text).ToString("0.00E+00")
最后,VBA UserForm_Initialize()
转换为VB.NET Form1_Load()
以上所有内容都提供了以下Form1.vb
代码:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rates(0 To 5, 0 To 5) As Double, i As Integer, j As Integer
rates(0, 0) = 1
rates(0, 1) = 86400
rates(0, 2) = 100
rates(0, 3) = 8640000
rates(0, 4) = 283460
rates(0, 5) = 2120428.8
rates(1, 0) = 0.000011574
rates(1, 1) = 1
rates(1, 2) = 0.0011574
rates(1, 3) = 100
rates(1, 4) = 3.2808
rates(1, 5) = 24.542
rates(2, 0) = 0.01
rates(2, 1) = 864
rates(2, 2) = 1
rates(2, 3) = 86400
rates(2, 4) = 2834.6
rates(2, 5) = 21204.288
rates(3, 0) = 0.00000011574
rates(3, 1) = 0.01
rates(3, 2) = 0.000011574
rates(3, 3) = 1
rates(3, 4) = 0.032808
rates(3, 5) = 0.24542
rates(4, 0) = 0.0000035278
rates(4, 1) = 0.3048
rates(4, 2) = 0.00035278
rates(4, 3) = 30.4805
rates(4, 4) = 1
rates(4, 5) = 7.4333
rates(5, 0) = 0.000000474600000000001
rates(5, 1) = 0.0410054400000001
rates(5, 2) = 0.0000474600000000001
rates(5, 3) = 4.10054400000001
rates(5, 4) = 0.134530116
rates(5, 5) = 1
TextBox2.Text = TextBox1.Text * rates(FraEnhet.SelectedIndex, TilEnhet.SelectedIndex)
If Len(TextBox2.Text) > 5 Then TextBox2.Text = Convert.ToDouble(TextBox2.Text).ToString("0.00E+00")
End Sub
'Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
' If Len(TextBox2.Text) > 5 Then
' TextBox2.Text = Convert.ToDouble(TextBox2.Text).ToString("0.00E+00")
' End If
'End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With FraEnhet.Items
.Add("m/s")
.Add("m/dag")
.Add("cm/s")
.Add("cm/dag")
.Add("ft/day")
.Add("gpd/ft2")
End With
With TilEnhet.Items
.Add("m/s")
.Add("m/dag")
.Add("cm/s")
.Add("cm/dag")
.Add("ft/day")
.Add("gpd/ft2")
End With
FraEnhet.SelectedIndex = 0
TilEnhet.SelectedIndex = 2
TextBox1.Text = 1
TextBox2.Text = 100
End Sub
End Class