button event handler

aronmatthew

Active member
Joined
Aug 5, 2019
Messages
25
Programming Experience
Beginner
I have created a dynamic button and a general event handler in a sperate class that catches the click event
public static void buttonClick(object sender, EventArgs e)
However in this handler I am unable to do things like change the button text and image although the debugger hits the lines that do these tasks there is no effect on the button.
 
How are we supposed to know what's wrong with your code if you don't show us your code? Generally speaking, you use the sender parameter to access the object that raised the event, so you can use the same event handler for as many objects as you like.
 
yeah that works for the button that called the event handler but what im actually trying to do is access a different button using a get control method.
 
Perhaps I am missing something... every WebControl has a Page property. Why can't you find the other control on the page?
 
Perhaps I am missing something... every WebControl has a Page property. Why can't you find the other control on the page?
Given that the OP has posted in an inappropriate forum, we don;t even know whether it is a web app or a Windows app. This is not a general question about Visual Studio.
 
yeah that works for the button that called the event handler but what im actually trying to do is access a different button using a get control method.
You didn't say anything about another Button in post #1 and you still haven't provided the relevant code. We are here because we want to help but you are making all but impossible for us to do that. Instead of making us guess and/or drag every little bit of information out of you, please provide a FULL and CLEAR description of the problem, which includes EXACTLY what you're trying to achieve, EXACTLY how you're trying to achieve it (including relevant code) and EXACTLY what happens when you try (including error messages and where they occur).
 
Hmm, I am rather perplexed why you have a static button at all. Your access modifier makes me assume you are using winforms, because most commonly buttons inside of lets say a web app like asp.net would be using protected over public/private. So I assume this is a winforms question.

Show us your code, all of it that involves how you've gone about creating this new button.
 
Last edited:
its a windows forms c# app. calling get control to get a button other than the one that was clicked. returns a control that when modified has no effect.
here is my get control method
C#:
public static void GetAllControl(Control c, List<Control> list)
{
foreach (Control control in c.Controls)
                {
                    list.Add(control);
                    if (control.GetType() == typeof(Panel))
GetAllControl(control, list);
}
            }
            public static Control getControl(Form f, string name, Type type)
{
                List<Control> list = new List<Control>();
                GetAllControl(f, list);
                foreach (Control control in list)
{
                    if (control.GetType() == type && control.Name.Equals(name))
                        return control;
                }
                return null;
            }
 
I have formatted the code you posted as you posted it and it's barely more readable. That seemingly random indentation is ridiculous. I feel like we're being punked. Again, we want to help but you need to help us do that, so posting code that is as readable as possible is an important part of that. It's easy too, because the IDE will format it for you and you just have to copy it. If you don;t do that, take the time to format it by hand because you want us to volunteer our time to help you. Here's what that code should have looked like:
C#:
public static void GetAllControl(Control c, List<Control> list)
{
    foreach (Control control in c.Controls)
    {
        list.Add(control);

        if (control.GetType() == typeof(Panel))
            GetAllControl(control, list);
    }
}

public static Control getControl(Form f, string name, Type type)
{
    List<Control> list = new List<Control>();
    GetAllControl(f, list);

    foreach (Control control in list)
    {
        if (control.GetType() == type && control.Name.Equals(name))
            return control;
    }

    return null;
}
Obviously much easier to read.
 
Now I feel even more like I'm being punked. You STILL have not posted your event handler code! That is most likely where the actual issue is. My guess is that you're passing the wrong form when you call it but I should NOT have to guess.
 
I'd suggest that you get rid of all that code anyway. I can't think of anything for which what you're doing would be the best way but you are still yet to actually provide an explanation of what it is that you're trying to achieve. It's very frustrating when you want to help someone but they refuse to make the effort to provide the information required to help. People will only persevere so long and will eventually just leave you without the help you want. I suggest that you make that effort.

We know that you're trying to affect a Button other than the one that was clicked but we don;t know how or why. The why is important because that will often dictate the how. For instance, if you have pairs of Buttons where clicking one should affect the other then it would make sense to assign one Button to the Tag property of the other, so you can then access it directly via the sender parameter in the event handler.

If getting a control of a particular type by name is the best way to go, do it in the form that contains the control and do it like this:
C#:
var btn = this.Controls.OfType<Button>().FirstOrDefault(b => b.Name == name);

if (btn != null)
{
    // Use btn here.
}
 
Being honest, I wish you had grabbed a book on C# or done some research before writing this.
  • What is meant to be passed into getControl ?
  • What should be passed in for f, name and type ?
  • What does this have to do with a button ?
  • What is all this about a handler for a button ?
  • What are you trying to achieve, (not in code) but in general?
  • Lastly, why are your objects static?
Surely if you're passing around forms and such, you'd be better working with instances?

If you can answer some of the questions, maybe we can help you build what you need. As it stands, what you wrote doesn't make a lot of sense for something of much use.
 
It almost feels like the OP is trying to create a poor man's version of one of the early concepts for WPF where the end user or designer creates the UI or skin, and the developer provides the logic to drive the UI. When WPF was originally conceived it was thought that UI designers would party on the UI using Blend, while developers would write all the code in the ViewModel and Models.

Of course, that is all speculation since our OP doesn't seem to be very forthcoming with what he is trying to do.
 
Being honest, I wish you had grabbed a book on C# or done some research before writing this.
  • What is meant to be passed into getControl ?
  • What should be passed in for f, name and type ?
  • What does this have to do with a button ?
  • What is all this about a handler for a button ?
  • What are you trying to achieve, (not in code) but in general?
  • Lastly, why are your objects static?
Surely if you're passing around forms and such, you'd be better working with instances?

If you can answer some of the questions, maybe we can help you build what you need. As it stands, what you wrote doesn't make a lot of sense for something of much use.
I've seen a lot of VB beginners and even intermediate developers think that they are doing the right thing by creating a module and then putting functionality there that relates to forms so that they can access it from multiple forms. The C# equivalent of a VB module is a static class so I'm guessing that the OP is putting functionality in a static class that can be accessed from any form. If you were going to write all this code to access a child control of a particular type by name then you wouldn't want to write it multiple times in multiple forms, so the motivation is good from that perspective. The thing is, you don't need that much code, as I have demonstrated. If you write succinct code to do this, there's nothing to be gained by putting it in a common location. If you did need to write enough code to make a common location worthwhile, I'd still suggest that a static class is not the best idea, as you could just put instance members in a base class and then derive all your forms from that.

So, I think I can guess the answers to some of your questions, although, as I've said, we really shouldn't have to guess this stuff. I think that the OP has created a static class with methods that they can call from any form to find a control on that form or any of its child controls. I have little doubt that those methods could be written better and may well not be needed at all, if the app was designed better in the first place. Again, we can't know what constitutes good design without more information about the actual aim here. This is a case of someone asking us how to implement a means to an end without explaining the end, so we can't actually explain what the better means there would be.
 
Back
Top Bottom