2015-05-28 57 views
1

在我的项目中我定义了八个组合框,现在我想为它们选择索引。如何填充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)抛出异常:

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

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

堆栈跟踪:

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() 
+0

我很担心这个异常与该行无关。你能提供一个异常的StackTrace吗? – Fabio

+0

@Fabio在问题 – Sushant

+0

中添加组合框初始化的位置? ('cmbDO0' ...)它们是在同一个线程上初始化的吗?尝试谷歌你的例外,有很多案件和解决方案。可能是你找到你的 – Fabio

回答

1

改变这一行...

昏暗lbCount为字节

到...

昏暗lbCount作为整数

再向上日期我们如果有任何新的错误消息。