Form with UserControl opens in VS2013 but not in VS2015 - vb.net

I have a Windows Form application that was originally created in VS2010. I have since migrated it to VS2013 and VS2015. The Application compiles fine and runs in VS2015, but if I try to open a particular form, the designer crashes giving the following error:
Error HRESULT E_FAIL has been returned from a call to a COM component.
It doesn't give the line of code that caused the error, but it does give a call stack as follows:
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at Microsoft.VisualStudio.LanguageServices.Implementation.Utilities.Exceptions.ThrowEFail()
at Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel.CodeTypeRef.LookupTypeSymbol()
at Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel.CodeTypeRef.get_TypeKind()
at EnvDTE.CodeTypeRef.get_TypeKind()
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.GetUrtTypeFromVsType(CodeTypeRef vsType)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnTypePopulateMembers(Object sender, EventArgs e)
at System.CodeDom.CodeTypeDeclaration.get_Members()
at Microsoft.VisualStudio.Design.Serialization.CodeDom.MergedCodeDomParser.CodeTypeDeclarationPopulator.OnPopulateMembers(Object sender, EventArgs e)
at System.CodeDom.CodeTypeDeclaration.get_Members()
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)
I am pretty sure it has to do with the fact that I use custom controls on the form. The code for the custom controls is as follows:
Public Class ctlServiceItem
Implements IComponent
Private _SelectedItem As AP_Data.AP_InvoiceService.SelectedItemEnum = AP_Data.AP_InvoiceService.SelectedItemEnum.NA
Public Event SelectedItemChanged As EventHandler
Public Property SelectedItem As AP_Data.AP_InvoiceService.SelectedItemEnum
Get
Return _SelectedItem
End Get
Set(value As AP_Data.AP_InvoiceService.SelectedItemEnum)
_SelectedItem = value
Select Case SelectedItem
Case AP_Data.AP_InvoiceService.SelectedItemEnum.NA
rbNA.Checked = True
Case AP_Data.AP_InvoiceService.SelectedItemEnum.OK
rbOK.Checked = True
Case AP_Data.AP_InvoiceService.SelectedItemEnum.Replaced
rbReplaced.Checked = True
Case AP_Data.AP_InvoiceService.SelectedItemEnum.Required
rbRequired.Checked = True
Case AP_Data.AP_InvoiceService.SelectedItemEnum.Suggested
rbSuggested.Checked = True
End Select
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Set
End Property
Public Property HeaderText As String
Get
Return GroupBox1.Text
End Get
Set(value As String)
GroupBox1.Text = value
End Set
End Property
Private _Added As Boolean
Public Property Added As Boolean
Get
Return _Added
End Get
Set(value As Boolean)
_Added = value
If _Added Then
rbReplaced.Text = "Added"
Else
rbReplaced.Text = "Replaced"
End If
End Set
End Property
Private Sub rbOK_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbOK.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.OK
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
Private Sub rbSuggested_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbSuggested.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.Suggested
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
Private Sub rbRequired_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbRequired.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.Required
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
Private Sub rbReplaced_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbReplaced.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.Replaced
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
Private Sub rbNA_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbNA.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.NA
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
End Class
The other control is
Public Class ctlServiceTireItem
Implements IComponent
Private _SelectedItem As AP_Data.AP_InvoiceService.SelectedItemEnum = AP_Data.AP_InvoiceService.SelectedItemEnum.NA
Public Event SelectedItemChanged As EventHandler
Public Property SelectedItem As AP_Data.AP_InvoiceService.SelectedItemEnum
Get
Return _SelectedItem
End Get
Set(value As AP_Data.AP_InvoiceService.SelectedItemEnum)
_SelectedItem = value
Select Case SelectedItem
Case AP_Data.AP_InvoiceService.SelectedItemEnum.NA
rbNA.Checked = True
Case AP_Data.AP_InvoiceService.SelectedItemEnum.OK
rbOK.Checked = True
Case AP_Data.AP_InvoiceService.SelectedItemEnum.Replaced
rbReplaced.Checked = True
Case AP_Data.AP_InvoiceService.SelectedItemEnum.Required
rbRequired.Checked = True
Case AP_Data.AP_InvoiceService.SelectedItemEnum.Suggested
rbSuggested.Checked = True
End Select
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Set
End Property
Public Property HeaderText As String
Get
Return GroupBox1.Text
End Get
Set(value As String)
GroupBox1.Text = value
End Set
End Property
Private Sub rbOK_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbOK.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.OK
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
Private Sub rbSuggested_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbSuggested.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.Suggested
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
Private Sub rbRequired_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbRequired.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.Required
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
Private Sub rbReplaced_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbReplaced.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.Replaced
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
Private Sub rbNA_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbNA.CheckedChanged
_SelectedItem = AP_Data.AP_InvoiceService.SelectedItemEnum.NA
RaiseEvent SelectedItemChanged(Me, EventArgs.Empty)
End Sub
End Class
I can provide the designer code for the form if needed, but it is voluminous. The controls show up in the toolbox as Project controls just like they should. It just doesn't make sense that it will open in the designer fine in one version of VS but not the newer version. Since it does work in an earlier version I was really hoping it is just an obscure setting or something like that.
Update: I tried creating a brand new blank form. I was able to drag the controls over to the new form and they showed up fine. When I closed the form and re-opened it in the designer, I got the same error as above. The problem is definitely with the user controls.

I found the answer at the bottom of
https://social.msdn.microsoft.com/Forums/en-US/97bfbff4-651c-47e4-8aaa-25fa2273b1b5/designer-crash-in-vs2015-in-windows-forms?forum=winformsdesigner
For some reason, I had a referenced to my own project in the project. I think this was a trick to make projects recognize their own controls in earlier versions of Visual Studio. Looks like this was fixed in VS 2015. Once the project didn't have a reference to itself, the Forms with the User Controls showed up fine.

Related

Adding an event handler to custom control

I have created a custom control (Check Box) with a custom EventHandler
Public Event CheckedChanged As EventHandler
Private Sub setCheckStateUI(sender As Object, e As EventArgs)
...
RaiseEvent CheckedChanged(sender, e)
End Sub
It works fine without any errors if I added this control directly to a form. But when I add this to another custom control (a page of settings window) and that second custom control add to a form (settings window) the 'settings window' freeze and visual studio auto restart.
If I removed this event handler in the code the problem is gone.
What can be the problem here?
Thanks in advance
Update: (Complete code of the Custom Control)
Public Class cusCheckBox
Private mystring As String
Private CheckButtonState As Integer = 0
Public Event CheckedChanged As EventHandler
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
mystring = Me.Name
setSizes()
End Sub
Public Property CheckState() As Integer
Get
CheckState = CheckButtonState
End Get
Set(ByVal value As Integer)
CheckButtonState = value
chkButton.CheckState = CheckButtonState
End Set
End Property
Public Property LabelText() As String
Get
LabelText = mystring
End Get
Set(ByVal value As String)
mystring = value
lblText.Text = mystring
setSizes()
End Set
End Property
Public Overrides Property Font As Font
Get
Return lblText.Font
End Get
Set(value As Font)
lblText.Font = value
End Set
End Property
Private Sub chkButton_CheckedChanged(sender As Object, e As EventArgs) Handles chkButton.CheckedChanged
If chkButton.CheckState = 1 Then
chkButton.Image = Global.MYLogs.My.Resources.Resources.btnToggleOn
CheckButtonState = 1
Else
chkButton.Image = Global.MYLogs.My.Resources.Resources.btnToggleOff
CheckButtonState = 0
End If
End Sub
Private Sub lblText_Click(sender As Object, e As EventArgs) Handles lblText.Click
setCheckStateUI(sender, e)
End Sub
Private Sub cusCheckBox_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
setCheckStateUI(sender, e)
End Sub
Private Sub cusCheckBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
setCheckStateUI(sender, e)
setSizes()
End Sub
Private Sub cusCheckBox_Resize(sender As Object, e As EventArgs) Handles Me.Resize
setSizes()
End Sub
Private Sub setSizes()
Me.Size = New Size(chkButton.Width + lblText.Width + 4, chkButton.Height)
End Sub
Private Sub setCheckStateUI(sender As Object, e As EventArgs)
If chkButton.CheckState = 0 Then
chkButton.Image = Global.MYLogs.My.Resources.Resources.btnToggleOn
chkButton.CheckState = 1
CheckButtonState = 1
Else
chkButton.Image = Global.MYLogs.My.Resources.Resources.btnToggleOff
chkButton.CheckState = 0
CheckButtonState = 0
End If
RaiseEvent CheckedChanged(Me, EventArgs.Empty)
chkButton.Select()
End Sub
End Class

Overriding a derived Event

I want to "override" an Event from a derived class - for example from a Forms-Control.
My actual state is, that the overriding (performed by the Command "Shadows") is working when I use the Handler of this Control directly.
Is the Control a member of a Collection it is only working with such Events which I have created by myself - if I try to use the overridden Event it isn't working. I suppose that the Collection uses the Event from the Base-Class.
Is that possible ?
And if "Yes" - what could I do ?
Code-Snippets from the described "Problem" :
This part collects the Event-Handler inside the Custom Control :
Private KalenderElemente As New Collection
Private Sub CreateElements()
KalenderElemente.Clear()
For i As Integer = 1 To 42
Dim myKalenderTag As New PP_Monatskalender_Tag
myKalenderTag.Name = "Tag_" + i.ToString("00")
myKalenderTag.ForeColor = my_ForeColor_Days
myKalenderTag.BackColor = my_BackColor_Days
myKalenderTag.Parent = Me
AddHandler myKalenderTag.Click, AddressOf KalenderTag_Click
AddHandler myKalenderTag.MouseMove, AddressOf KalenderTag_MouseMove
AddHandler myKalenderTag.MouseEnter, AddressOf KalenderTag_MouseEnter
AddHandler myKalenderTag.MouseLeave, AddressOf KalenderTag_MouseLeave
KalenderElemente.Add(myKalenderTag)
Next
End Sub
Private Sub Kalender_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
If Not KalenderElemente.Item(0).Visible Then
KalenderElemente.Item(0).DatumsTag = 0
RaiseEvent MouseMove(KalenderElemente.Item(0), e)
Else
KalenderElemente.Item(41).DatumsTag = 0
RaiseEvent MouseMove(KalenderElemente.Item(41), e)
End If
End Sub
Private Sub KalenderTag_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
RaiseEvent MouseMove(sender, e)
End Sub
Shadows Event MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
Private Sub KalenderTag_MouseEnter(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent MouseEnter(sender, e)
End Sub
Shadows Event MouseEnter(ByVal sender As Object, ByVal e As EventArgs)
Private Sub KalenderTag_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent MouseLeave(sender, e)
End Sub
Shadows Event MouseLeave(ByVal sender As Object, ByVal e As EventArgs)
Now each of the internal Controls deliver it's Mouse-Event to outside.
If I put it on a Form and write a script which takes the Event I could see that all works fine (and as expected).
In the following you see the part of the collection which should manage this Control (and others) :
Public Class MessageDefinition
Public WithEvents Control As Control
Public HeaderText As String
Public MessageText As String
Public DisplayShadow As Boolean
Public ToolTipLocation As ToolTipLocationDefintion
Public Location As Point
End Class
Public Class Message_Collection
Inherits CollectionBase
Public Shadows Sub Clear()
Dim myItem As MessageDefinition
For i As Integer = 1 To List.Count
myItem = List.Item(i - 1)
RemoveHandler myItem.Control.MouseEnter, AddressOf Item_MouseEnter
RemoveHandler myItem.Control.MouseMove, AddressOf Item_MouseMove
RemoveHandler myItem.Control.MouseLeave, AddressOf Item_MouseLeave
Next
List.Clear()
End Sub
Overrides Function ToString() As String
Return "[...]"
End Function
Public Sub Dispose()
Clear()
End Sub
' ================================
Public Sub SetMessage(item As MessageDefinition)
Dim myItem As MessageDefinition
For i As Integer = 1 To List.Count
myItem = List.Item(i - 1)
If myItem.Control.GetType Is item.Control.GetType _
AndAlso myItem.Control.Name = item.Control.Name Then
'List.Item(i - 1) = item
'RaiseEvent MouseEnter(item, System.EventArgs.Empty)
Exit Sub
End If
Next
AddHandler item.Control.MouseEnter, AddressOf Item_MouseEnter
AddHandler item.Control.MouseMove, AddressOf Item_MouseMove
AddHandler item.Control.MouseLeave, AddressOf Item_MouseLeave
List.Add(item)
RaiseEvent MouseEnter(item, System.EventArgs.Empty)
End Sub
Private Sub Item_MouseEnter(sender As Object, e As System.EventArgs)
Dim myItem As MessageDefinition
Dim mySender As Control = sender
For i As Integer = 1 To List.Count
myItem = List.Item(i - 1)
If myItem.Control Is mySender Then
RaiseEvent MouseEnter(myItem, e)
Exit Sub
End If
Next
End Sub
Private Sub Item_MouseMove(sender As Object, e As System.EventArgs)
Dim myItem As MessageDefinition
Dim mySender As Control = sender
For i As Integer = 1 To List.Count
myItem = List.Item(i - 1)
If myItem.Control Is mySender Then
RaiseEvent MouseMove(myItem, e)
Exit Sub
End If
Next
End Sub
Private Sub Item_MouseLeave(sender As Object, e As System.EventArgs)
Dim myItem As MessageDefinition
Dim mySender As Control = sender
For i As Integer = 1 To List.Count
myItem = List.Item(i - 1)
If myItem.Control Is mySender Then
RaiseEvent MouseLeave(myItem, e)
Exit Sub
End If
Next
End Sub
Public Event MouseEnter(sender As Object, e As System.EventArgs)
Public Event MouseMove(sender As Object, e As System.EventArgs)
Public Event MouseLeave(sender As Object, e As System.EventArgs)
End Class
As described (on Top) the catched Events are fired with "Standard"-Controls but not with the "Customized" Control.
If I change it and build up my own Events (with different names), which are not shadowing the Events from the derived control, it is also working as expected.
You don't override events in .NET - you can only override inherited event handlers, if any.
The event pattern in .NET is to create a public event in the base class and a protected virtual (VB Protected Overridable) method that raises that event and that can be overridden by a derived class. This method should be named OnEventName.
The Windows Forms controls follow this pattern, so to e.g. override when the Click event happens, you override the OnClick method:
Public Class MyTextBox
Inherits TextBox
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
If SomeCondition() Then
MyBase.OnClick(e)
Else
Return 'Do not click
End If
End Sub
End Class
Obviously you can fiddle with the e argument as well.

Cancel Datetimepicker ValueChanged Event

Is there a way to cancel an event through a condition? I have tried e.cancel but it does not work. After cancelling the event the dtpAudit_From.Value must revert back to its original value.
Private Sub dtpAudit_From_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpAudit_From.ValueChanged
'check if two DTPs (Date time pickers) are valid
If dtpAudit_From.Value > dtpAudit_To.Value Then
MsgBox("cancel the event")
End If
End Sub
One way is to subclass the DateTimePicker and add a ValueChanging event. Here's an example:
Public Class UIDateTimePicker
Inherits DateTimePicker
Public Sub New()
Me.cachedValue = Me.Value
End Sub
Public Event ValueChanging As CancelEventHandler
Protected Overrides Sub OnValueChanged(e As EventArgs)
If (Not Me.reverting) Then
Dim evargs As New CancelEventArgs(False)
Me.OnValueChanging(evargs)
If ((Not evargs Is Nothing) AndAlso evargs.Cancel) Then
Dim value As Date = Me.Value
Me.reverting = True
Me.Value = Me.cachedValue
Else
Me.cachedValue = Value
MyBase.OnValueChanged(e)
End If
Me.reverting = False
End If
End Sub
Protected Overridable Sub OnValueChanging(e As CancelEventArgs)
RaiseEvent ValueChanging(Me, e)
End Sub
Private cachedValue As DateTime
Private reverting As Boolean
End Class
Usage
Private Sub dtpAudit_From_ValueChanging(sender As Object, e As CancelEventArgs) Handles dtpAudit_From.ValueChanging
e.Cancel = {Condition}
End Sub

Making combobox visible when it is disabled

I am disabling combobox in VB.net.
But in disable mode it not visible properly.
I tried changing both BackColor and ForeColor but it is not working.
Code :
cmbbox.BackColor = Color.FromName("Window")
or
cmbbox.ForeColor = Color.FromName("Window")
Please help
Dear Adam:
I am making my component enable false.But I want to make it viewable.You can reffer the link.This is what exacly I want but in VB.Net : A combobox that looks decent when it is disabled
To achieve disabling combobox without fading it, first change the dropdown style of the combobox to DropDownList, Then tweak with the events to achieve the goal.
Here is a piece of code by which you can achieve the same:
Public Class Form1
Dim selectindex As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("1")
ComboBox1.Items.Add("2")
ComboBox1.Items.Add("3")
ComboBox1.Items.Add("4")
selectindex = 3
ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
ComboBox1.SelectedIndex = selectindex
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
ComboBox1.SelectedIndex = selectindex
End Sub
End Class
Create anew form Form1 and add a combobox to the form, then add the above code to get a readonly combobox.
Have a look at this thread which has a solution for a readonly combobox and the code is all VB.NET.
A version of their code is as follows. You'll need to but it inside a class of your own which inherits System.Windows.Forms.ComboBox
Private _ReadOnly As Boolean = False
Public Property [ReadOnly]() As Boolean
Get
Return _ReadOnly
End Get
Set(ByVal Value As Boolean)
_ReadOnly = Value
End Set
End Property
Public Overrides Function PreProcessMessage(ByRef msg As Message) As Boolean
'Prevent keyboard entry if control is ReadOnly
If _ReadOnly = True Then
'Check if its a keydown message
If msg.Msg = &H100 Then
'Get the key that was pressed
Dim key As Int32 = msg.WParam.ToInt32
'Ignore navigation keys
If key = Keys.Tab Or key = Keys.Left Or key = Keys.Right Then
'Do nothing
Else
Return True
End If
End If
End If
'Call base method so delegates receive event
Return MyBase.PreProcessMessage(msg)
End Function
Protected Overrides Sub WndProc(ByRef m As Message)
'Prevent list displaying if ReadOnly
If _ReadOnly = True Then
If m.Msg = &H201 OrElse m.Msg = &H203 Then
Return
End If
End If
'Call base method so delegates receive event
MyBase.WndProc(m)
End Sub
I've been looking for the same not long ago and ended up doing the following. You may not like it, but i'll share it in case. I am using TableLayoutPanel to arrange my controls on the form and then i am swapping the positions of the desired controls.
For example I've created the following Items:
Form1 Design
TableLayoutPanel1 (two columns, three rows)
TextBox1 Read-only = True, BackColor = White
ComboBox1 Visible = False, DropDownStyle = DropDownList, FlatStyle = Popup
Button1 (named it to Change)
Button2 (named it to Done) -> Visible = False
Runtime - Screenshots
Here is my code:
Public Class Form1
Private Sub SwapControls(tlp As TableLayoutPanel, ctr1 As Control, ctr2 As Control)
Dim ctl1pos As TableLayoutPanelCellPosition = tlp.GetPositionFromControl(ctr1)
ctr1.Visible = False
tlp.SetCellPosition(ctr1, tlp.GetPositionFromControl(ctr2))
ctr2.Visible = True
tlp.SetCellPosition(ctr2, ctl1pos)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SwapControls(TableLayoutPanel1, TextBox1, ComboBox1)
SwapControls(TableLayoutPanel1, Button1, Button2)
Label1.Select()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SwapControls(TableLayoutPanel1, ComboBox1, TextBox1)
SwapControls(TableLayoutPanel1, Button2, Button1)
Label1.Select()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Select()
ComboBox1.SelectedIndex = 0
TextBox1.Text = ComboBox1.SelectedItem
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox1.Text = ComboBox1.SelectedItem
End Sub
End Class

How to expand combo box on focus event?

I want to automatically expand Combo box on focus event.
I have set the Droppeddown = True in gotfocus event, but this has a side effect. When click event gets fired, it expands dropdown and closes immediately. How can I avoid it?
Here is Code:
Private Sub cmbElectLoadPS_gotfocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbElectLoadPS.GotFocus
cmbElectLoadPS.DroppedDown = True
End Sub
What about check if already DroppedDown ?
Private Sub cmbElectLoadPS_gotfocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbElectLoadPS.GotFocus
if Not cmbElectLoadPS.DroppedDown Then
cmbElectLoadPS.DroppedDown = True
End If
End Sub
If u need this behavior for all your combo controls is better to create your own implementation
Pulic Class CustomComboBox
Inherits ComboBox
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
if Not DroppedDown Then
DroppedDown = True
End If
End Sub
End Class
Oh .. add the same value to ComboBox on Mouseup event..
it would do the trick for u :)
sthing like :
private void comboBox1_Enter(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
private void comboBox1_MouseUp(object sender, MouseEventArgs e)
{
comboBox1.DroppedDown = true;
}
Not your best solution.. but it would do the trick :)
Create a timer called tmrDropDown (you should create a timer for each ComboBox) and leave its default properties.
Add this code:
Private Sub cmbBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbBox.GotFocus
tmrDropDown.Enabled = True
End Sub
And
Private Sub tmrDropDown_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDropDown.Tick
cmbBox.DroppedDown = True
tmrDropDown.Enabled = False
End Sub
To implement this features for multiple combobox controls, rather than inheriting the combobox as a new custom control, I'd propose this simple solution:
Private Sub AutoDropDownCombobox_Enter(sender As Object, e As EventArgs) Handles _
cboControl1.Enter, cboControl2.Enter ' register additional events here
If Not CType(sender, ComboBox).DroppedDown Then
CType(sender, ComboBox).DroppedDown = True
End If
End Sub