Quick question in Visual C#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 1
Reputation: Joka527 is an unknown quantity at this point 
Solved Threads: 0
Joka527 Joka527 is offline Offline
Newbie Poster

Quick question in Visual C#

 
0
  #1
Mar 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Quick question in Visual C#

 
2
  #2
Mar 9th, 2009
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.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 47
Reputation: ctrl-alt-del is an unknown quantity at this point 
Solved Threads: 0
ctrl-alt-del ctrl-alt-del is offline Offline
Light Poster

Re: Quick question in Visual C#

 
0
  #3
Mar 24th, 2009
  1. this.button1.Click += new System.EventHandler(this.button1_Click);

this will do the trick for you
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: bcasp is an unknown quantity at this point 
Solved Threads: 10
bcasp bcasp is offline Offline
Light Poster

Re: Quick question in Visual C#

 
0
  #4
Mar 24th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC