I created a webAddressTextBox with a SingleButtonRight EditorButton. When the button is pressed opens up IE and displays website.... This works fine at run time.
I am having problems with design time. I added my EditorButton to the buttons right collection using a key ("Hyperlink"). I can add the control at design time and everything looks fine, I then resize the control as needed. If I close the design view of the form and reopen I get an error stating that the "Hyperlink" key already exists. (Before I added code for the key, every time I editied the form in the designer and then closed and reopend the form it would add a new editor button to the ButtonsRight collection.)
Code:
Imports Infragistics.Win.UltraWinEditors
Imports System.ComponentModel
Public Class CsiHyperlinkEditor
Inherits CsiTextBox
Private WithEvents hyperlinkButton As EditorButton
Private Sub OnHyperlinkEditorButtonClick(ByVal sender As Object, ByVal e As EditorButtonEventArgs)
Try
Process.Start(Me.Text)
Catch ex As Exception
MessageBox.Show("Unable to find " & Me.Text, "Unable to find", MessageBoxButtons.OK)
End Try
End Sub
Public Sub New()
MyBase.New()
' This call is required by the Windows Form Designer.
InitializeComponent()
hyperlinkButton = CType(MyBase.ButtonsRight(0), EditorButton)
If hyperlinkButton IsNot Nothing Then
AddHandler hyperlinkButton.Click, AddressOf Me.OnHyperlinkEditorButtonClick
End If
'UserControl overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
Finally
MyBase.Dispose(disposing)
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
CType(Me, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
If Me.DesignMode = False Then
Me.ButtonsRight.Clear()
If Me.ButtonsRight.Exists("Hyperlink") = False Then
Dim hyperlinkEditorButton As EditorButton = New EditorButton("Hyperlink")
Dim hyperlinkAppearance As Infragistics.Win.Appearance = New Infragistics.Win.Appearance
hyperlinkAppearance.Image = Global.Csi.CsiBaseForms.My.Resources.Resources.world
hyperlinkEditorButton.Appearance = hyperlinkAppearance
Me.ButtonsRight.Add(hyperlinkEditorButton)
CType(Me, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout()
End Class
Using suggestion of using OnCreateControl and checking for design mode didn't work for me (I wanted to see the control laid out properly in the designer). However, just marking the fields that contained my subcontrols and properties with the attribute
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
did.
Thanks Matt!
That did the trick!
You can't add the button through the constructor; when you do this at design-time, the Visual Studio designer will see that the ButtonsRight collection has changed and so it will serialize the button. The next time that Visual Studio needs to deserialize the control, the button will be created in the constructor and then the designer code will try to add the same button again. A better approach would be to override OnCreateControl, as outline in this thread.
-Matt