Visual Studio Internal Compiler Error FIX

  • Thread starter Thread starter String
  • Start date Start date
Dim cb() As New ComboBox()

Protected Overrides Sub OnCreateControl()

AddHandler cb.SelectedIndexChanged, AddressOf ComboBox_TextChanged

End Sub



I am creating a custom datagrid with comboboxes in whatever columns the
designer wishes. The above code is what causes VS to crash. It works fine
with a single combobox(not an array) but as soon as I create a combo box
array the problem arises. There must be a different way to do this but I
need a dynamic-unknown number of comboboxes so an array of them would be the
best and easiest way of doing it. Any two cents appreciated. Thanks
 
String said:
Dim cb() As New ComboBox()

Protected Overrides Sub OnCreateControl()

AddHandler cb.SelectedIndexChanged, AddressOf ComboBox_TextChanged

End Sub

Keep in mind that an array is a Data Type, unlike VB 6 it is an object in
and of itself (with some neat tricks when you declare an Array of a specific
type). It makes no sense to put an event handler on it (at least not in
this case). What you are saying is this: "When the array's SelectedIndex
Property changes, fire this event", when in reality, the array has no such
event, the event is a member of the array contents, not the array itself. So
what you want to do is loop through each item in the list/array and process
them seperatly. I don't fully understand why the IDE even let you do this
in the first place - it should throw a syntax error.

Try this code:

Dim cb( ) As New ComboBox()

Protected Overrides Sub OnCreateControl( )
Dim cbThis as ComboBox

For Each cbThis in cb
AddHandler cb.SelectedIndexChanged, AddressOf ComboBox_TextChanged
Next
End Sub


HTH,
Jeremy
 
Sorry, there is a bug in the example i posted... see inline fix:

Try this code:

Dim cb( ) As New ComboBox()

Protected Overrides Sub OnCreateControl( )
Dim cbThis as ComboBox

For Each cbThis in cb


Replace this:
AddHandler cb.SelectedIndexChanged, AddressOf ComboBox_TextChanged



With this:

AddHandler cbThis.SelectedIndexChanged, AddressOf ComboBox_TextChanged
 
Thanks. I see what you're saying and it makes sense. Unfortunely Visual
Studio crashes in the exact same way with your code. Any other suggestions?
 
String said:
Thanks. I see what you're saying and it makes sense. Unfortunely Visual
Studio crashes in the exact same way with your code. Any other suggestions?

Did you use my bug fix (it will crash with out it)?
 
Your right that seemed to fix visual studio from crashing. Maybe I got a
little over my head here. Below is posted my entire control code. I hope you
don't mind taking a look and see how to make your suggestion work in my code
or my code work with yours. Most of this code is taken from Microsoft. I
relize its nowhere neat complete and some places might make no sense. The
reason for this is cause I made it work with 1 combo box and now I'm trying
to accomidate it for multiple. Thanks alot.



Public Class CustomDataGrid

Inherits System.Windows.Forms.DataGrid

Dim intCol As Integer

Dim cb() As ComboBox

Dim alComboCols As New Collection()

Public Sub FillCombo(ByVal column As Integer, ByVal items As Collection)

Dim i As Integer

Me.Controls.Add(cb(0))

alComboCols.Add(column)

If column = 3 Then

Me.Controls.Add(cb(1))

alComboCols.Add(column)

For i = 1 To items.Count

cb(0).Items.Add(items.Item(i))

Next i

End If

For i = 1 To items.Count

cb(0).Items.Add(items.Item(i))

Next i

intCol = column

End Sub

Private Sub CustomDG_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

Dim i As Integer

For i = 1 To alComboCols.Count

If Me.CurrentCell.ColumnNumber = alComboCols.Item(i) Then

cb(i - 1).Width = Me.GetCurrentCellBounds.Width

End If

Next i

End Sub

Private Sub CustomDG_CurrentCellChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.CurrentCellChanged

Dim i As Integer

For i = 1 To alComboCols.Count

If Me.CurrentCell.ColumnNumber = alComboCols.Item(i)
Then

cb(i - 1).Visible = False

cb(i - 1).Width = 0

cb(i - 1).Left =
Me.GetCurrentCellBounds.Left

cb(i - 1).Top = Me.GetCurrentCellBounds.Top

cb(i - 1).Text = Me.Item(Me.CurrentCell) &
""

cb(i - 1).Visible = True

Else

cb(i - 1).Visible = False

cb(i - 1).Width = 0

End If

Next i

End Sub

Private Sub CustomDG_Scroll(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Scroll

'cb.Visible = False

'cb.Width = 0

End Sub



Private Sub CustomDG_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click

'cb.Visible = False

'cb.Width = 0

End Sub



Private Sub Ctrls_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim i As Integer

For i = 1 To alComboCols.Count

If Me.CurrentCell.ColumnNumber = alComboCols.Item(i) Then

cb(i - 1).Visible = False

If Me.Item(Me.CurrentCell) & "" = "" Then

'SendKeys.Send("*")

End If

Me.Item(Me.CurrentCell) = cb(i - 1).Text

End If

Next i

End Sub

Protected Overrides Sub OnCreateControl()

Dim cbThis As New ComboBox()

For Each cbThis In cb

AddHandler cbThis.SelectedIndexChanged, AddressOf
Ctrls_TextChanged

Next

End Sub

End Class
 
Sorry I guess that was a bit too much to ask. I guess all that is confusing
me is: why do I need cbThis? When I display and use a combobox am I using
cb() or cbThis? Do I set cb(0) = cbThis, cb(1) = cbThis? Am I completely
mixed up? Thanks
 
String said:
Sorry I guess that was a bit too much to ask. I guess all that is confusing
me is: why do I need cbThis? When I display and use a combobox am I using
cb() or cbThis? Do I set cb(0) = cbThis, cb(1) = cbThis? Am I completely
mixed up? Thanks

Lookup the definition of For...Each, you will find that a for...each loop
iterates through each item in a collection, cbThis is a pointer moving to
each item in the list.

This:

Dim cbThis as ComboBox
For Each cbThis in cb( )
Debug.WriteLine cbThis.Name
Next

Is equivilent to this:

Dim i as Integer
For I = 0 to cbThis.GetUpperBound(0)
Debug.WriteLine cb( i ).Name
Next


I like the prior just because it is more concise (don't need to think about
an indicies, or the bounds of the array). I just don't have time to look
through your code, sorry, but from what I saw, it doesn't seem like you need
an array of combo boxes, just one would do. Are you trying to show a combo
box in the cell of a data grid? What exactly are you trying to accomplish?

Jeremy
 
Back
Top