Basic Newbie question

A

Anil Gupte

I am learning to use C#, having decided to upgrade myself from VB.Net (to
which I also recently upgraded a few months ago from VB6).

I created a Windows Application and in it a form. I added a label and a
button on the form. Then I double-clicked on the button and added the
following:
private void button1_Click(object sender, System.EventArgs e)

{

lblTest.Text="Hello World!";

}

As I usually do in VB, I changed the button name from button1 to btnTest in
the Properties window. Now I have an error saying:

C:\Projects\vbtut\WindowsApplication1\Form1.cs(74):
'WindowsApplication1.Form1' does not contain a definition for
'button1_Click'

Why should it still be looking for button1, when I have gone in and changed
it. In VB I don't have this problem, what gives?

Thanx,
 
S

Scott M.

You will find that the editing environment for VB.NET differs in many ways
from C#. In VB.NET, your event handlers are not invoked by the name of the
procedure (as was the case in VB 6.0), but instead by the details following
the "Handles" keyword. Additionally, if you were to change the name of one
of your controls that has a pre-existing event handler in VB.NET, the editor
will change the details of the Handles clause in your code (but it won't
change the name of the procedure).

C# does not make use of the Handles clause, and as such, it works just like
it did in VB 6.0. If you have pre-existing code and your change the name of
a control, you must go and change the name of the event handler manually.

You will find many other examples of how the C# editor is not as
user-friendly as in VB.NET. VB.NET is dynamically compiled as you type it,
so syntax and build errors can be known as you type your code. C# doesn't
have this feature, so you will need to build your code frequently to see if
you have errors and build again after fixing them to be sure they are fixed.
 
L

Laurent Bugnion

Hi,

Anil said:
I am learning to use C#, having decided to upgrade myself from VB.Net (to
which I also recently upgraded a few months ago from VB6).

I created a Windows Application and in it a form. I added a label and a
button on the form. Then I double-clicked on the button and added the
following:
private void button1_Click(object sender, System.EventArgs e)

{

lblTest.Text="Hello World!";

}

As I usually do in VB, I changed the button name from button1 to btnTest in
the Properties window. Now I have an error saying:

C:\Projects\vbtut\WindowsApplication1\Form1.cs(74):
'WindowsApplication1.Form1' does not contain a definition for
'button1_Click'

Why should it still be looking for button1, when I have gone in and changed
it. In VB I don't have this problem, what gives?

Thanx,

When you create a button, you must rename it first and then only
double-click it to add event handlers.

When you double-click the button, it adds an entry in the
"InitializeComponent" method, something like:

button1.Click += new EventHandler( button1_Click );

This is called a delegate, it means that you add a reference to your
method "button1_Click" (as if it was an object) to the list of methods
which will be executed when the "Click" event fires.

If you rename the "button1" in "btnTest", and especially if you rename
the method "button1_Click" to "btnTest_Click", then you must also modify
the delegate definition:

btnTest.Click += new EventHandler( btnTest_Click );

It's a bit tricky to understand how delegate work, but I recommend you
to study that carefully, because they are an invaluable help, for
example for multithreading.

HTH,
Laurent
 
G

Gino

Right click on the method and then left click on Reference.
You will be taken to the Designer supplied code that
still has the original name.
Then you can edit the line so the names match.
 
A

Anil Gupte

Interestingly, I found no Reference to button1 anywhere. I used the
Reference in the right-click menu, then tried doing a find (told it to
search the entire project). Also, more interesting, I found the following.
this.btnTest.Click += new System.EventHandler(this.btnTest_Click_1);

So, I double clicked on the button again, and it created:
private void btnTest_Click_1(object sender, System.EventArgs e)

This is nuts! The thing is totally munged. I don't want to have these
weird names in there because no one will understand the code - if it becomes
a real project more people will be on it. Someone, please shoot the person
at Microsoft responsible for this! :) Just kidding ok? But seriously, what
is the way out of this mess?

Thanx,
 
M

Morten Wennevik

You can always just rename the btnTest_Click_1 to whatever name you want,
and then reattach it to the button.

In the designer, select the Button whos click event you want to define.

Click the lightning icon above the button properties to get the list of
events for the control.

In the Click event, click the dropdown button to the right and select the
method you renamed. You can also just write the name of the method in the
method name field and click enter, or just doubleclick on it with no name
and the default name will be used.

If there is no existing method matching the name a new one will be created.
 
L

Lionel Pinkhard

The event handler is probably still pointing to the function
button1_Click, the function name has nothing to do with the name of the
button, you can call it whatever you want. Click on button, go to Events
tab and change the handler, simple as that, I think it should be same as
in VB.Net, although I'm not expert at VB.Net. If that doesn't work use
"Find in Files" and search for "button1_Click".

Anil said:
I am learning to use C#, having decided to upgrade myself from VB.Net (to
which I also recently upgraded a few months ago from VB6).

I created a Windows Application and in it a form. I added a label and a
button on the form. Then I double-clicked on the button and added the
following:
private void button1_Click(object sender, System.EventArgs e)

{

lblTest.Text="Hello World!";

}

As I usually do in VB, I changed the button name from button1 to btnTest in
the Properties window. Now I have an error saying:

C:\Projects\vbtut\WindowsApplication1\Form1.cs(74):
'WindowsApplication1.Form1' does not contain a definition for
'button1_Click'

Why should it still be looking for button1, when I have gone in and changed
it. In VB I don't have this problem, what gives?

Thanx,


---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0643-0, 2006/10/22
Tested on: 2006/10/23 11:35:49 AM
avast! - copyright (c) 1988-2006 ALWIL Software.
http://www.avast.com
 
S

Scott M.

The best thing to do is simply adopt the same coding strategey you had in VB
6.0. Name your controls before you add any code for their events and don't
rename a control after that.
 
A

Anil Gupte

OK, I got it licked - but it was a helluva lot of busy work that did not
need to be there. After all what is an IDE for? Maybe I will go back to
learning C++ on Linux with my trusty pico editor. :-0 Thanx all who
responded.
 

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