我试图让变量输入来自组合框。我原以为这将是一个更简单的任务,但我坚持这一点,并会感谢一些帮助。
我正在使用连接到 API、连接到服务器并执行操作的预打包代码。我正在对其进行自定义以进行一些额外的计算,所有这些现在都可以正常工作,但是我用来执行这些计算的变量在其中一个子例程中是硬编码的,我希望能够读取它们而是使用组合框。我曾多次使用 VB6 和 VBA 完成此操作,但我是 vb.net (2010) 的新手,即使我的表单上有组合框,对组合框中所选数字的每次引用最终都为空结果
在一个更简单的应用程序中,例如下面的应用程序,我可以从组合框中获取数据:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim divisor As Integer
Dim res1 As Integer
If Int32.TryParse(ComboBox1.Text, divisor) Then
MsgBox(ComboBox1.Text)
Else
MsgBox("error" & ComboBox1.Text)
End If
res1 = divisor - 9
MsgBox(res1)
End Sub
End Class
不幸的是,我正在使用的代码与上面的代码不合作。在不发布所有代码的情况下,这是基本结构,也许这将帮助您找出在哪里编写上面的代码,这样它将获取组合框中的值,其中 sub13 中的“除数”(见下文)将是组合框中的数字,而不是硬编码
您在下面看到的对组合框的每个引用都是“自动生成的”。也就是说,一旦我将组合框放在表单上,所有代码都会出现。
Imports X.API
Public Class frmMain
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Sub Dispose(ByVal disposing As Boolean)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents Panel1 As System.Windows.Forms.Panel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.ComboBox()
Me.ComboBox1.FormattingEnabled = True
Me.ComboBox1.Location = New System.Drawing.Point(710, 117)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
Me.ComboBox1.TabIndex = 3
End Sub
#End Region
#Region " Member Variables "
Private mTable As DataTable
#End Region
#Region " Form and Control Events "
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
End Sub
Private Sub s1()
End Sub
Private Sub s2()
End Sub
Private Sub s3()
End Sub
Private Sub s4()
End Sub
#End Region
#Region " Operations "
Private Sub s5()
End Sub
Private Sub s6()
End Sub
Private Sub s7()
End Sub
Private Sub s8()
End Sub
#End Region
#Region " API Events "
Private Sub s8()
End Sub
Private Sub s9()
End Sub
Private Sub s10()
End Sub
Private Sub s11()
End Sub
Private Sub s12()
End Sub
#End Region
Private Sub s13()
Dim divisor As Integer = 1
'[this is where i want the divisor to draw from the combobox]
'so instead of "Dim divisor As Integer = 1"
i want "Dim divisor As Integer = contents of combobox
End Sub
' i have no idea why this code appears here
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Private Class Item1
Public Sub s14()
End Sub
End Class
Private Class Item2
Public Sub s15()
End Sub
End Class
Private Class Item3
Public Sub s16()
End Sub
End Class
End Class
我正在处理的子是“s13()”,但是当我尝试从组合框中读取时,我得到一个空白。
我不知道将代码的确切位置放置在我上面显示的结构中的第一个示例中所示的位置。我原以为从组合框中阅读会容易得多,但我很难过。