943,884 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 494
  • C# RSS
Mar 9th, 2009
0

Quick question in Visual C#

Expand Post »
Hi, a noob programmer just starting in C# with Visual Studio here....

I came across this while coding in Visual C# and I couldn't figure out anything about it so a little explanation would be greatly appreciated....

In Microsoft's Visual C#...when using Windows Forms for GUI.....

You guys know how there's a Design view and a Code view?,
and let's say you create a button1 on the Design view, and if you double click that on the Design view, Visual C# automatically generates an event handler linked to that button on the Code view like....

private void button1_Click(object sender, EventArgs e){
}

this might be a dumb question but I tried copying that code above and just pasted it into the Code view INSTEAD of letting it generate from the doubl-clicking and when I ran the program, the button didn't work.........Why is that???
The button's function only works if I generate the code by double-clicking it....
I'm guessing when you double click the button there's more to it than just the code automatically being generated?

Any explanation would be great.

Thank you guys.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Joka527 is offline Offline
1 posts
since Mar 2009
Mar 9th, 2009
2

Re: Quick question in Visual C#

There's some other code generated that refers to the button1_Click method -- look around for it, it's probably folded up somewhere. I can guarantee that simply adding that method is unsufficient to put an event handler in place.

Edit:
Right click the method when it's auto-generated, and click "Find All References" -- that'll show you where the method is getting used.
Last edited by Rashakil Fol; Mar 9th, 2009 at 9:45 pm.
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 24th, 2009
0

Re: Quick question in Visual C#

C# Syntax (Toggle Plain Text)
  1. this.button1.Click += new System.EventHandler(this.button1_Click);

this will do the trick for you
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
ctrl-alt-del is offline Offline
65 posts
since Jul 2008
Mar 24th, 2009
0

Re: Quick question in Visual C#

If you've ever expanded the file for a form in the list view in your solution explorer pane, you should see two files below the main Form source file. There should be a [form_name].Designer.cs (or whatever language). Inside that file is where it handles setting all of the properties for your controls. This is also where the event handlers get wired up as mentioned in the other posts.
Reputation Points: 23
Solved Threads: 10
Light Poster
bcasp is offline Offline
45 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: hi : removing the zeroes
Next Thread in C# Forum Timeline: Regexp whitespace problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC