Results 1 to 9 of 9

Thread: [2.0] UserControlLibrary Default Event Handler

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    [2.0] UserControlLibrary Default Event Handler

    How can you choose which event comes up by default when a control is double clicked? My user control contains one TextBox. When I import the user control into another project and double click it, the Load event comes up by default. Would it be possible to make it TextChanged by default instead?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] UserControlLibrary Default Event Handler

    You need to apply the DefaultEvent attribute to your class, e.g.
    Code:
    [System.ComponentModel.DefaultEvent("TextChanged")]
    public partial class UserControl1 : UserControl
    {
    	public UserControl1()
    	{
    		InitializeComponent();
    	}
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] UserControlLibrary Default Event Handler

    Gracias. I shall give it a try. And while we're at it, how do I create default properties? I have a public property in public user control and I set it to false in the designers file, or in the declaration. Either way, when I import the control and create an instance of it, the public property is bold. For other properties, their default value is always just normal text and when you change them, THEN the text is bold, but for some reason, even when I first create the instance of it, the font is already bold as if it was changed.

    Does this even matter? Honestly? No. I just want my control to look and feel like other controls made by Microsoft and other components and such. Also, the way it is shown bold before it is changed leads me to believe that quite possibly I'm doing something wrong.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2.0] UserControlLibrary Default Event Handler

    Use the attributes on the property.
    If you specify the default value and of correct type it will not be bolded unless its changed from that value.


    Code:
    Option Explicit On
    Option Strict On
    
    Imports System.Runtime.InteropServices
    Imports System.ComponentModel
    Imports System.ComponentModel.Design
    
    Public Class UserControl1
    
    	Private _ShowMenuBar As Boolean
    
    	<Browsable(True), Category("SomeCategory"), DefaultValue(True)> _
    	Public Property ShowMenuBar() As Boolean
    		Get
    			Return _ShowMenuBar
    		End Get
    		Set(ByVal Value As Boolean)
    			_ShowMenuBar = Value
    		End Set
    	End Property
    End Class
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] UserControlLibrary Default Event Handler

    Apply the DefaultValue attribute to your property. That also gives you the ability to right-click it in the designer and select Reset.

    Remember, if you want to control the design-time behaviour of a type or member then you're almost certainly talking about attributes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] UserControlLibrary Default Event Handler

    Alright. I don't really know much at all about attributes. Got any good tutorials for learning about them?

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2.0] UserControlLibrary Default Event Handler

    Just check out the intellisense and see whats available. There isnt too much too them.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2.0] UserControlLibrary Default Event Handler

    Ok, here is the C# for you.
    Code:
    using System.Runtime.InteropServices;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    
    public class UserControl1
    {
    
    	private bool _ShowMenuBar;
    
    	[Browsable(true), Category("SomeCategory"), DefaultValue(true)]
    	public bool ShowMenuBar
    	{
    		get
    		{
    			return _ShowMenuBar;
    		}
    		set
    		{
    			_ShowMenuBar = value;
    		}
    	}
    }
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] UserControlLibrary Default Event Handler

    Go to the MSDN Library documentation for the Attribute class, read, go to the Inheritance Hierarchy section, click on the Derived Classes link, read about each one. It's all those in the System.ComponentModel namespace that control design-time behaviour.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width