Results 1 to 7 of 7

Thread: [RESOLVED] Change form control properties in different class with method ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Resolved [RESOLVED] Change form control properties in different class with method ?

    hi,

    how could I write a method in class which I could then use for control events in all forms ? I have labels with same images and same Mouse_Enter, Mouse_Leave code and I want to write a method which I could simply subcribe to all labels in different forms.

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

    Re: Change form control properties in different class with method ?

    E.g.
    vb.net Code:
    1. Module LabelEventHandlers
    2.  
    3.     Public Sub MouseEnterHandler(sender As Object, e As EventArgs)
    4.         Dim lbl = DirectCast(sender, Label)
    5.  
    6.         '...
    7.     End Sub
    8.  
    9.     Public Sub MouseLeaveHandler(sender As Object, e As EventArgs)
    10.         Dim lbl = DirectCast(sender, Label)
    11.  
    12.         '...
    13.     End Sub
    14.  
    15. End Module
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         AddHandler Label1.MouseEnter, AddressOf LabelEventHandlers.MouseEnterHandler
    5.         AddHandler Label1.MouseLeave, AddressOf LabelEventHandlers.MouseLeaveHandler
    6.     End Sub
    7.  
    8.     Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    9.         RemoveHandler Label1.MouseEnter, AddressOf LabelEventHandlers.MouseEnterHandler
    10.         RemoveHandler Label1.MouseLeave, AddressOf LabelEventHandlers.MouseLeaveHandler
    11.     End Sub
    12.  
    13. End Class

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Change form control properties in different class with method ?

    So there isn't any shorter way to do this ? I was hoping for something without AddHandler method in C#.

    Is RemoveHandler allways neccesary, what can happen If not included in code ? And I've seen some C# code where they register event in constructor, right after Initialize component() - what's the difference from registering in Load ?

    Beside that - is this a valid code in C# or should It be better to use something else when you need to reference form or other controls too from same method :

    Code:
     
            public static void Enter(object sender, EventArgs e)
            {
                dynamic lbl = (Label)sender;
    
                Form frm = lbl.Parent;
                frm.Cursor=Cursors.Hand;
    
                frm.Controls["Textbox1"].Text = "Test";
    
                lbl.Image=Properties.Resources.Smile;
            }
    
            public static void Leave(object sender, EventArgs e)
            {
                dynamic lbl = (Label)sender;
    
                Form frm = lbl.Parent;
                frm.Cursor = Cursors.Default;
                frm.Controls["Textbox1"].Text = "";
    
                lbl.Image = Properties.Resources.Cry;
            }
    Last edited by LuckyLuke82; Aug 10th, 2017 at 12:46 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Change form control properties in different class with method ?

    Sorry, I answer most of my questions in the VB.NET forum and I must have forgotten that this one was in C# when I answered it. The C# code would be basically the same though.
    csharp Code:
    1. static class LabelEventHandlers
    2. {
    3.     public static void MouseEnterHandler(object sender, EventArgs e)
    4.     {
    5.         var lbl = (Label)sender;
    6.  
    7.         // ...
    8.     }
    9.  
    10.     public static void MouseLeaveHandler(object sender, EventArgs e)
    11.     {
    12.         var lbl = (Label)sender;
    13.  
    14.         // ...
    15.     }
    16. }
    csharp Code:
    1. public partial class Form1 : Form
    2. {
    3.     public Form1()
    4.     {
    5.         InitializeComponent();
    6.     }
    7.  
    8.     private void Form1_Load(object sender, EventArgs e)
    9.     {
    10.         label1.MouseEnter += LabelEventHandlers.MouseEnterHandler;
    11.         label1.MouseLeave += LabelEventHandlers.MouseLeaveHandler;
    12.     }
    13.  
    14.     private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    15.     {
    16.         label1.MouseEnter -= LabelEventHandlers.MouseEnterHandler;
    17.         label1.MouseLeave -= LabelEventHandlers.MouseLeaveHandler;
    18.     }
    19. }
    Quote Originally Posted by LuckyLuke82 View Post
    So there isn't any shorter way to do this ? I was hoping for something without AddHandler method in C#.
    C# uses the '+=' operator to do the same job as AddHandler but you need to use it in order to register an event handler. I'm not sure how much shorter you were hoping for but I don't really see that using AddHandler in VB is long. If what you actually mean is that you don't want to have to write a statement for each event of each control then an option might be to add a method to the same class as the event handling methods themselves are members of, so you can just call one method and pass in a control and it will register all the event handlers:
    csharp Code:
    1. static class LabelEventHandlers
    2. {
    3.     public static void RegisterEventHandlers(Label lbl)
    4.     {
    5.         lbl.MouseEnter += MouseEnterHandler;
    6.         lbl.MouseLeave += MouseLeaveHandler;
    7.     }
    8.  
    9.     public static void UnregisterEventHandlers(Label lbl)
    10.     {
    11.         lbl.MouseEnter -= MouseEnterHandler;
    12.         lbl.MouseLeave -= MouseLeaveHandler;
    13.     }
    14.  
    15.     public static void MouseEnterHandler(object sender, EventArgs e)
    16.     {
    17.         var lbl = (Label)sender;
    18.  
    19.         // ...
    20.     }
    21.  
    22.     public static void MouseLeaveHandler(object sender, EventArgs e)
    23.     {
    24.         var lbl = (Label)sender;
    25.  
    26.         // ...
    27.     }
    28. }
    You still need one line per event but you only have to write those lines once, instead of once per control.
    Quote Originally Posted by LuckyLuke82 View Post
    Is RemoveHandler allways neccesary, what can happen If not included in code ?
    You generally should use RemoveHandler in VB and '-=' in C# whenever you use AddHandler and '+='. The reason is that the object whose event is being handled holds a reference to the object handling the event via the event handler delegate and that effects garbage collection. In this case though, if the class handling the events is static then there is no object to clean up while the app is running, so it might not be an issue. I would tend to do it anyway though, for consistency and the fact that there might be some other issue that I wasn't aware of.
    Quote Originally Posted by LuckyLuke82 View Post
    And I've seen some C# code where they register event in constructor, right after Initialize component() - what's the difference from registering in Load ?
    Certainly nothing wrong with doing it in the constructor. I rarely do things in a form constructor in VB unless I want to add parameters but, given that event handlers are registered in the constructor (or at least in the InitializeComponent method that is called from the constructor) when done in the designer, I can't really think of an argument against doing it. That applies even more in C#, where the constructor is in the user code file by default. If you are removing event handlers in the FormClosed event handler then adding them in the Load event handler offers some sort of symmetry, but that's a rather flimsy reason.
    Quote Originally Posted by LuckyLuke82 View Post
    Beside that - is this a valid code in C# or should It be better to use something else when you need to reference form or other controls too from same method :

    Code:
     
            public static void Enter(object sender, EventArgs e)
            {
                dynamic lbl = (Label)sender;
    
                Form frm = lbl.Parent;
                frm.Cursor=Cursors.Hand;
    
                frm.Controls["Textbox1"].Text = "Test";
    
                lbl.Image=Properties.Resources.Smile;
            }
    
            public static void Leave(object sender, EventArgs e)
            {
                dynamic lbl = (Label)sender;
    
                Form frm = lbl.Parent;
                frm.Cursor = Cursors.Default;
                frm.Controls["Textbox1"].Text = "";
    
                lbl.Image = Properties.Resources.Cry;
            }
    I would say two things about that. Firstly, I would tend to call FindForm on the Label rather than using the Parent property. That allows for Labels that are added to child containers like Panels.

    Secondly, don't reuse properties of Properties.Resources like that. Remember that resources are actually embedded in your EXE and every time you access a property like that, the embedded data is extracted and an object created. If you were to mouse over your Label 10 times, that code would create 20 Image objects and not dispose any of them. What you should do is access each property once and assign the result to a variable, then reuse that variable so that you are reusing the same object.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Change form control properties in different class with method ?

    You meant like this right ? :

    registering events:
    Code:
            private void Form2_Load(object sender, EventArgs e)
            {
                LabelEventHandlers.RegisterEventHandlers(label1);
            }
    images:

    Code:
        static Bitmap Cry = Properties.Resources.Cry;
          static Bitmap Smile = Properties.Resources.Smile;
    
            public static void Enter(object sender, EventArgs e)
            {
                dynamic lbl = (Label)sender;
    
                Form frm = lbl.FindForm();
                frm.Cursor = Cursors.Hand;
    
                frm.Controls["Textbox1"].Text = "Test";
    
                lbl.Image = Smile;
            }
    
            public static void Leave(object sender, EventArgs e)
            {
                dynamic lbl = (Label)sender;
    
                Form frm = lbl.FindForm();
    
                frm.Cursor = Cursors.Default;
                frm.Controls["Textbox1"].Text = "";
    
                lbl.Image = Cry;
            }
    If that's the case then I'm satisfied, exactly something I was looking for. Thanks, again.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Change form control properties in different class with method ?

    Yep, that's pretty much it. It feels a little dirty to me to be accessing a TextBox on the form like that but I guess I can live with it if it's internal.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Change form control properties in different class with method ?

    Yep, that's pretty much it. It feels a little dirty to me to be accessing a TextBox on the form like that but I guess I can live with it if it's internal.
    Basically I don't need accessing other controls right now, but I included that in post to make sure If I can do It. What I need is reference to form only, to change cursor on label Mouse_Enter.

    Thanks again!

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