如何填充ComboBox数组?

时间:2015-05-28 06:18:55

标签: vb.net combobox

在我的项目中,我已经定义了八个ComboBox,现在我想为所有这些选择索引。

以下是代码:

Private Sub btnRead_Set1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead_Set1.Click
        Dim lbCount As Byte
        Dim lunDO_Setting(40) As UInt16
        Dim lunRes As UInt16
        Dim lunAddress As UInt16
        Dim llgResponse As Long
        Dim lbIndex As UInt16
        'array of ComboBox
        Dim cmbDOMode = New ComboBox() {cmbDO0, cmbDO1, cmbDO2, cmbDO3, cmbDO4, cmbDO5, cmbDO6, cmbDO7}
        Dim txtDO_X = New TextBox() {txtDO0_1, txtDO1_1, txtDO2_1, txtDO3_1, txtDO4_1, txtDO5_1, txtDO6_1}
        lbIndex = 0
        lunAddress = 100
        For lbCount = 0 To 5
            If lunAddress = 200 Then    'Read Resolution for 1st SET
                llgResponse = MT_Read_Holding_Reg(Convert.ToByte(Trim(txtDIN.Text)), lunAddress, 1, lunRes)
            Else
                'Read Mode, Blink rate, Mono Shot , etc 
                '---------------------------------Device ID-------------------------Add---------Qty---Buffer
                llgResponse = MT_Read_Holding_Reg(Convert.ToByte(Trim(txtDIN.Text)), lunAddress, 48, lunDO_Setting(lbIndex))
                lbIndex += 8
            End If
            lunAddress += 100   'At every 100th interval new config is mapped
        Next

        For lbCount = 0 To 5
            'select the index for each combox depending open buffer value
            cmbDOMode(lbCount).SelectedIndex = lunDO_Setting(lbCount)
            txtDO_X(lbCount).Text = lunDO_Setting(lbCount + 8).ToString
        Next
    End Sub

然而,行cmbDOMode(lbCount).SelectedIndex = lunDO_Setting(lbCount)抛出异常:

  

尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

     

对象引用未设置为对象的实例。

StackTrace:

at DIO_48.frmMain.btnRead_Set1_Click(Object sender, EventArgs e) in D:\Project\DIO-48 V1\VB Utility\Source Code\V1\Lastone\DIO_48_Utility\DIO_48\frmMain.vb:line 134
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at DIO_48.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

1 个答案:

答案 0 :(得分:1)

改变这一行......

  

Dim lbCount As Byte

为...

  

Dim lbCount As Integer

然后告诉我们任何新的错误消息(如果有的话)。

相关问题