Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have problems with button PerformClick().
In my main form im loading another form 30 times:

C#
for (int i = 0; i < iloscIteracji; i++)
{
    pso _pso = new pso(czastki, punkty, 1, 3, 3,1);
    _pso.Show();
}

then in Form1_Load im checking settings of constructor

C#
if (ktoraMetoda == 1)
{
    button1.PerformClick();
    radioButton1.Checked = true;
    textBox1.Text = "100";
    button3.PerformClick();

}


but the problem is that, in button3_click i have 2 methods which i want to run:
znajdzNajlepszych();
zmienKolory();
and that doesnt work. I tried to put breakpoint on that 2 methods, app just leaving them.

Any suggestions?
Posted

First of all, this design of the code is the pure abuse of OOP. As I can understand a bit of some Slavic languages, I'll explain for others: most likely, the identifier "ktoraMetoda" means "whichMethod". By attempting to use an integer (or any other) descriptor for identification and selection of some method means throwing out all the OOP technology, replacing it with nothing robust or supportable.

Instead of trying to work with events and UI in general, you should stop any development and learn how OOP works in principle. Unfortunately, this Quick Questions & Answer forum is not a place to explain it all, which would take a pretty big article designed for people with some minimal background.

I will just briefly explain. The whole idea of using PerformClick is wrong in general case. It was designed on purpose, that an event cannot be invoked anywhere but the class where the event instance is declared, not even in the derived class. The method PerformClick is just a backdoor to such invocation. The event handling should be pure. You don't really need to invoke an event from elsewhere, you simply need to call the same method as your event handle. That said, you add a event handler to the invocation list of some event instance (using '+=', there is no any other way). Write a separate method to be called on handling of this event. Your event handler should do only one thing: call this method. It gives you additional freedom: you don't have to use the same argument list as the handler; some or all arguments usually are not used. Now, you can call this method from any other place, which effectively replaces PerformClick. This way, you can get rid of any side effects possibly caused by some other handler of the same event instance. And, finally, this method you call can call any number of methods, not just one.

—SA
 
Share this answer
 
Inadequate code to really see your problem. However, I would wrap the code you execute in the perform click in a method, then call the method when you need to execute that code. If you really want to execute the click method, execute it directly, not through the button. If the event handler for the button is button1_Click, then just execute the button1_Click:

button1_Click(this, new EventArgs())
.
or
button1_Click(button1, new EventArgs())


Doing this eliminates the need for the button to be in a state that it can be executed, and also eliminates most potential threading problems.
 
Share this answer
 
I got an error, thats why it doestn work.
I read that in 64b computers Visual isnt showing debug problems, app just finished work and closed. Thank u anyway ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900