button event handler

aronmatthew

Active member
Joined
Aug 5, 2019
Messages
41
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.
 
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.

Agreed, but until we get the answers to what I asked, we will never know. OP needs to come forward and explain in more detail so that we can give the best possible answers to their problem. My suspicions are clear, and I feel my questions will unearth the ugly side of what the OP is trying to do.

OP : As for statics and instantiated objects, and the correct way of doing things; there are only specific cases where I use statics as they can also leave terrible code smells and can sometimes be more troublesome than they're worth, typically when troubleshooting. Yea, sure they have their occasional uses but another reason why I try to avoid statics is because they're bad for testing, unit testing particularly and they are impossible to use with interfaces. Statics don't work to well with instantiated objects, and without getting into a full blown debate on this. As I said somewhere before : there are ways you can use delegates on classes to derive a certain functionality from static classes and also to use delegates in-place of interfaces. But all this is impracticable if you want to build a good model for your program, and you really should consider going about it in the correct way, and instantiate where an instantiation is needed, rather than using statics to slip under-the-hood for quicker easier access; so to speak. Which my guess is that is what you're trying to do quite likely because you do not know how to work with instantiated objects.

I will post this pseudo code to demonstrate for our OP the different uses of using static and instantiated methods, classes and properties. And perhaps it will help also help future readers understand working with instances and statics and make it a little easier to understand what you can and cannot do...
Pseudo for testing and experimentation:
    public static class Test : InterFaceA //Inheritance can't be done, as static classes can't inherit interfaces
    {
        public object foo; //has no status here because instance members do not belong in static member classes
        public static object doo; //Has status here because static objects can reside in static classes
        public void MethodA() //Has no status here because it is not a static method
        {
            //This is a non functional method because its not declared static, nor should an instantiable member be in a static class
        }
        public static void MethodB() //Has status here because static methods can reside in a static class
        {
            NewTest nTest = new NewTest();
            nTest.MethodA(); //Here we instantiate a new instance of the non static class NewTest and call Method A
            nTest.MethodB(); //Method B however can not be called by an instance reference, and must be called by its static type.
            NewTest.MethodB(); //This is a non instantiated call directly to the static method
        }
    }
    public class NewTest : InterFaceA //Inheritance can be done, as non-static classes can inherit interfaces
    {
        public object foo; //has status here because instance members belong in non-static member classes
        public static object doo; //Has status here because static objects can reside in non-static classes
        public void MethodA() //Has status here because it is a non-static method in a instantiable class
        {
            Test.MethodB(); //You can call Method B, but not Method A because Method A does not have any business being an instantiable member in a static class
        }
        public static void MethodB() //Has status here because static methods can reside in a instantiable class
        {
            Test newTest = new Test(); //This won't work because you can not create an instance of a static class, nor can you declare a variable of a static type
        }
    }
    public interface InterFaceA
    {

    }
Now you have this little "cheat sheet", you can go about finding your controls as outlined in its documentation or here (by James Curran) and invoke them from other classes the way it is meant to be done. And I would agree with ditching the above code and starting over. As I can see no possible reason for doing what you're trying to do, certainly not without more clarified information.
 
Wait until somebody writes some poorly architected spaghetti code like you are creating now, and then leaves it for you to maintain. Let's see what kind of language you'll be spouting then.

A lot of volunteers here inherently want to train the people coming here to write good code because there is a chance that we may end up working with them, or inheriting, or maintaining their code. We try to nip bad habits at the bud, as well as direct people to learning best practices.

There is adage in the arts: in order to break the rules, you need to know the rules well. Maybe you are trying to create something groundbreaking, but right now from our experienced eyes, you have not yet learned enough of the rules to be able to break them in a new and insightful way.
 
I hope that one line of insults that the staff have removed results in your ban.

If that's the attitude you take towards people who offer their time and experience to help you for absolutely free, you deserve nothing else. Since you haven't a clue what you're doing based on the code you presented, I decided I would explain to you how to use static and instance members and demonstrate their usage. Had you taken the advice wrote down and followed the links, you would be ahead of where you're still stuck. What you had wrote was absolutely fit for the bin.

I refuse to help people like you. What a horrible reply to receive.
 
calling a click event from within the button event does not do anything either. One should be able to understand what I am trying to do fromt his photo. When a talent is selected its photo and label need to be added to the build. This problem is preventing me from finishing this project.

Screenshot (60).png
 
If you were following a proper MVC or MVP (or even MVVM) architecture, then clicking on the picture would result in your Model of the build being updated with the selected talent being added, and the View would be updated with a picture and label on the current talents in the Model.

Alas, you are using your UI as both View and Model. Back in the 80's and 90's that was the way to build "programs" on Windows because of the expense of memory, and the speed of CPUs. But that is 20 years ago. Memory, processors, and programming techniques have evolved since then. Using your UI as your data model is now discouraged. The only thing the UI is supposed to do is to reflect the current state of your data.

Setting that aside, it is still unclear what benefits you get from having your button click event handlers in a separate static class. If the goal is code re-use, then you should be using aggregation, not delegation. Or you could also choose to use inheritance, but all my past experiences were negative when using inheritance for WinForm forms. The next best thing to inheritance for code reuse in WinForms is to use UserControls -- basically a form of visual aggregation.
 
Last edited:
I use one event handler for all my button clicks. I use statics because its convenient not only for events but for creating controls. But whatever your saying. Its a simple problem. How to access a sperate control from within an event handler.
 
calling a click event from within the button event does not do anything either.
That is a nonsense statement. You don't call events. You still haven't haven't posted the code of your event handler either, despite being asked to do so several times.
One should be able to understand what I am trying to do fromt his photo.
So you're just flat-out refusing to provide an actual explanation here then?
When a talent is selected its photo and label need to be added to the build.
What has that got to do with Buttons and accessing a second Button when one is clicked?
This problem is preventing me from finishing this project.
The provide the actual explanation that you should have provided in the first place and maybe we can help you solve it. It's never a surprise that someone can't solve a problem that they can't even explain because it is a string indication that they don't even understand the problem they're trying to solve. Either that or they just can't be bothered to take the time to explain it, in which case I can't be bothered to help. I'm done here.
 
I use one event handler for all my button clicks. I use statics because its convenient not only for events but for creating controls. But whatever your saying. Its a simple problem. How to access a sperate control from within an event handler.
What event handler? You've never shown us any event handler.
 
To me, this look like another case of an XY Problem.
The XY problem is asking about your attempted solution rather than your actual problem. This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

  • User wants to do X.
  • User doesn't know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.
  • User doesn't know how to do Y either.
  • User asks for help with Y.
  • Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve.
  • After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn't even a suitable solution for X.
The problem occurs when people get stuck on what they believe is the solution and are unable step back and explain the issue in full.
 
I am already out, and won't be contributing anything after this post because of your hostile attitude. But lets summarise over the details, shall we ? I think you're a troll.
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 asked you a number of questions, both myself and John wanted you to answer. In order for us to help you, you need to help us by providing us with answers for what we asked you for. Which you did not. You ignored us all, and then you come back a day later and call us gay fags and then you have the cheek to expect further help without apologising.
calling a click event from within the button event does not do anything either.
Well that's nice to know. And what do you think we can do about that when we don't know how you wrote your code inside your button event? And they are not called button events, they are button methods with event arguments. My theory is that you are likely needing to invoke click on the button you want to programmatically click. I think this because you appear to be throwing forms around in your get controls method. I could simply provide you with the answer to that problem, but I feel an apology is in order after your little outburst.
One should be able to understand what I am trying to do fromt his photo.
Well, you know they say a picture tells a thousand words. Hmm, in this case, it doesn't tell me anything about what you are trying to do, because you haven't explained that yet again, nor have you showed your code for this button method which you keep talking of.
When a talent is selected its photo and label need to be added to the build.
Build, what build? You're talking as if we are sitting beside you looking at your screen. We are each thousands of miles away from you with no clue what build you are referring to. Is this where we play guessing games?
I use one event handler for all my button clicks. I use statics because its convenient not only for events but for creating controls. But whatever your saying. Its a simple problem. How to access a sperate control from within an event handler.
Well that shows how much you know. And if its a simple problem, why don't you fix it? If that control is on another form, you need to either disable cross thread operations or invoke the control with a delegate. If this is something you've not done before, its actually not simple for the later option. And for the previous, I wouldn't advocate as its terrible practice and will likely cause more issues. Actually we don't know, because you are not showing the relevant code.
This problem is preventing me from finishing this project.
We could never have guessed as much. I thought you just came here for a quick trolling session and fling a few insults. Also on post 15, John was summarising to me about the questions I had asked you to update us on, so that we had more info to work with and provide you with an answer. If this is something you are unfamiliar with, this is how we build communication, and speculate to formulate ideas on the forums while we conjure up fresh thoughts on how we could best answer your question and wait for you to reply.

The only person here holding you back; is yourself. If you won't provide clear descriptive answers to what you were asked, you clearly are a troll.
 
Last edited:
Back
Top Bottom