Call click event via code? C#

T

Tony

private void cmdTryMe_Click(object sender, System.EventArgs e)
{

MessageBox.Show("Something");
}


How would I programatically fire the button's click event via code
instead of via a user physically mouse-clicking the button?


(C# Newbie alert... Sorry if this is a pretty basic question)


Tony!
 
C

Cor Ligthert [MVP]

Tony,

Just call the method, in the worst way as

TheMethodFromTheClickEvent(null, null);

I hope this helps,

Cor
 
T

Tony

Why would you do that? It seams amazing they have such a command in the
framework.

Because I'm kind of cheating.

I've got a task I'm needing to run on an automated scheduler we have
running. I'm using a secure ftp component we got from IPWORKS. I took
the demo windows app that came with it and pieced together a windows
app of my own that does what I need it to with a button click.

I'm trying to take this and convert it into a console app where I
would then call with arguments with various site info from a father
app which creates a report which would then need to be ftpd out
somewhere.

However, being newbish in C#, I'm having issues getting the component
and it's events coded correctly in a console app.

Sooo... I figured I would click my button via code in the form's load
event and that would get me by until I can get things more the way
they really need to be..

As a stop-gap measure, it should work.

-Tony!-
 
P

PS

Tony said:
Because I'm kind of cheating.

I've got a task I'm needing to run on an automated scheduler we have
running. I'm using a secure ftp component we got from IPWORKS. I took
the demo windows app that came with it and pieced together a windows
app of my own that does what I need it to with a button click.

I'm trying to take this and convert it into a console app where I
would then call with arguments with various site info from a father
app which creates a report which would then need to be ftpd out
somewhere.

However, being newbish in C#, I'm having issues getting the component
and it's events coded correctly in a console app.

Sooo... I figured I would click my button via code in the form's load
event and that would get me by until I can get things more the way
they really need to be..

As a stop-gap measure, it should work.

When I have this type I pay some illiegal alien a couple of dollars an hour
to run the task and click the buttons. Saves me having to figure out how to
use the task scheduler in Windows.

PS
 
M

Michael C

Tony said:
Sooo... I figured I would click my button via code in the form's load
event and that would get me by until I can get things more the way
they really need to be..

As a stop-gap measure, it should work.

The point is you don't need to click the button, you need to run the code
that clicking the button runs. eg

void Button1_Click(...)
{
DownloadFiles();
}

You're thinking you need to click the button but clicking the button is NOT
what you want to do. What you want to do is download the files, so call
DownloadFiles directly. This is a very common newbie question and there's
always some "expert" who goes into details about how to click the button.
You *never* need to write code to click a button in your own app.

Michael
 
T

Tony

The point is you don't need to click the button, you need to run the code
that clicking the button runs. eg

void Button1_Click(...)
{
DownloadFiles();
}

You're thinking you need to click the button but clicking the button is NOT
what you want to do. What you want to do is download the files, so call
DownloadFiles directly. This is a very common newbie question and there's
always some "expert" who goes into details about how to click the button.
You *never* need to write code to click a button in your own app.

Michael

You make a valid point.

Tony!
 
M

Marc Gravell

Cor, Michael; I agree that calling the underlying code directly is
almost always the better option. I also maintain it answers the
question, but also meets a few other criteria:

1: it may (or may not; I haven't investigated) check UI-level checks
akin to simulating an actual click (enabled, visible, etc)
2: if the button has been inherited, then any appropriate OnClick code
will get fired
3: if multiple methods are subscribing to the event, then they will get
fired
4: it works even if you don't "own" the button (and so can't access the
referenced code directly)

OK, in reality it is very rare that these are actual issues, so I
almost always code with the handler pointing to a simple method that I
can call directly, but! it is a good idea for people to understand the
reasons for doing things a certain way. The backing method invoke
approach had already been mentioned, so I threw this in as an
"actually, you may wish to consider" (even if only in a "it isn't
likely you'll need this, but it would be good for you to know that it
is there" kind of way).

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top