Error; Why is VS C++ deleting my events from the form?

  • Thread starter Thread starter _Christopher\(M2M\)
  • Start date Start date
C

_Christopher\(M2M\)

Why is VS 2003 C++ moving my events from my programme, it run OK. :-)

But soon as I change to the Design form some of the events have been
deleted!

The only thing I can find that sound like my prob. is the link:

http://msdn.microsoft.com/vstudio/downloads/updates/datalossifx.aspx

But that no good!! ;-(

Is there a work around for the prob.

Thanks.

--
From _Christopher (M2M).
RefCode:22CdcdTCDfdc V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for the
help."
 
Why is VS 2003 C++ moving my events from my programme, it run
OK. :-)

But soon as I change to the Design form some of the events have
been deleted!

Are you using MFC? Windows Forms?

Can you post a small code sample that exhibits this bug?

Thanks,
 
Tarek Madkour said:
Are you using MFC? Windows Forms?
I am using .NET
Can you post a small code sample that exhibits this bug?
I'l try...


--
From _Christopher (M2M).
RefCode:22CdccTCDsdF V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for the
help."
 
Tarek Madkour said:
Are you using MFC? Windows Forms? ..NET

Can you post a small code sample that exhibits this bug?

File -> New -> Project -> Visual C++ Projects-> .NET-> Windows Forms
Application (.NET)
Name: = TestEvent <ENTER>
Switch to 'Form1.H' (View Code)
Edit -> Select All

Copy and paste the folling:-

------8<-------8<-----

#pragma once

namespace TestEvent
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change
the
/// 'Resource File Name' property for the managed resource
compiler tool
/// associated with all .resx files this class depends on.
Otherwise,
/// the designers will not be able to interact properly with
localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
System::Void Button_MouseDown(System::Object * sender, MouseEventArgs *
e)
{
MessageBox::Show(S"",S"");
}

public: System::Windows::Forms::Button* button1;
Form1(void)
{
InitializeComponent();
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(24, 16);
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->MouseDown += new MouseEventHandler(this,
Button_MouseDown);

//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 270);
this->Controls->Add(this->button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);
}
};
}

------>8------->8-----

Now run the programme, and it should work OK
E.G. Clicking the blank button will display the OK MessageBox in
'Button_MouseDown'.

Now switch to the Form1.h [Design] mode and move the button now view the
code.

I'm finding that
this->button1->MouseDown += new MouseEventHandler(this, Button_MouseDown);
Is not there any more. ;-(

What am I doing wrong?


BIG thank you if you (or anyone else) can help me.
--
From _Christopher (M2M).
RefCode:333CdcTZCDdc V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for the
help."
 
_Christopher(M2M) said:
Tarek Madkour said:
Are you using MFC? Windows Forms? .NET

Can you post a small code sample that exhibits this bug?

File -> New -> Project -> Visual C++ Projects-> .NET-> Windows Forms
Application (.NET)
Name: = TestEvent <ENTER>
Switch to 'Form1.H' (View Code)
Edit -> Select All

Copy and paste the folling:-

------8<-------8<-----

#pragma once

namespace TestEvent
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change
the
/// 'Resource File Name' property for the managed resource
compiler tool
/// associated with all .resx files this class depends on.
Otherwise,
/// the designers will not be able to interact properly with
localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
System::Void Button_MouseDown(System::Object * sender, MouseEventArgs *
e)
{
MessageBox::Show(S"",S"");
}

public: System::Windows::Forms::Button* button1;
Form1(void)
{
InitializeComponent();
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(24, 16);
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->MouseDown += new MouseEventHandler(this,
Button_MouseDown);

//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 270);
this->Controls->Add(this->button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);
}
};
}

------>8------->8-----

Now run the programme, and it should work OK
E.G. Clicking the blank button will display the OK MessageBox in
'Button_MouseDown'.

Now switch to the Form1.h [Design] mode and move the button now view the
code.

I'm finding that
this->button1->MouseDown += new MouseEventHandler(this, Button_MouseDown);
Is not there any more. ;-(

What am I doing wrong?


BIG thank you if you (or anyone else) can help me.

I think I have found a work around:-
It work OK if I included the 'System::Windows::Forms::'

WAS:
this->button1->MouseDown += new MouseEventHandler(this, Button_MouseDown);

NOW:

this->button1->MouseDown += new
System::Windows::Forms::MouseEventHandler(this, Button_MouseDown);

So is there a problem with the 'using namespace'?

--
From _Christopher (M2M).
RefCode:333CdcTCS4kz V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for the
help."
 
message [big snip]
I think I have found a work around:-
It work OK if I included the 'System::Windows::Forms::'

WAS:
this->button1->MouseDown += new MouseEventHandler(this, Button_MouseDown);

NOW:

this->button1->MouseDown += new System::Windows::Forms::MouseEventHandler(this, Button_MouseDown);

So is there a problem with the 'using namespace'?


Such problems generally show up at compile time.
(The exception is when you don't know what you are
doing and use the wrong name.)

If changing that scope qualification changed the behavior
of your code, than that fact establishes that a different
object was bound to the resulting name. I would expect
that, in the absence of some other change you have not
mentioned, the result of the above change would be to
go from "will not compile" to "does compile". So, if
there was also a "using namespace ..." change, and if
that construct allowed you to use a name from a
different namespace than you intended, then I would
agree that there was indeed a specific problem with the
"using namespace ..." construct. In general, namespace
merging is a hazard, so I avoid use of the 'using' construct
to get at multiple namespaces in the same scope. In my
opinion, it is asking for trouble.
 
Larry Brasfield said:
message [big snip]
I think I have found a work around:-
It work OK if I included the 'System::Windows::Forms::'

WAS:
this->button1->MouseDown += new MouseEventHandler(this,
Button_MouseDown);

NOW:

this->button1->MouseDown += new
System::Windows::Forms::MouseEventHandler(this, Button_MouseDown);

So is there a problem with the 'using namespace'?
Such problems generally show up at compile time.
(The exception is when you don't know what you are
doing and use the wrong name.)

If changing that scope qualification changed the behavior
of your code, than that fact establishes that a different
object was bound to the resulting name. I would expect
that, in the absence of some other change you have not
mentioned, the result of the above change would be to
go from "will not compile" to "does compile". So, if
there was also a "using namespace ..." change, and if
that construct allowed you to use a name from a
different namespace than you intended, then I would
agree that there was indeed a specific problem with the
"using namespace ..." construct. In general, namespace
merging is a hazard, so I avoid use of the 'using' construct
to get at multiple namespaces in the same scope. In my
opinion, it is asking for trouble.

Can I ask you, did you try to compile the programme? If so did it compile
and run OK.
It compiles OK here with the namespaces as thay are.

Thanks.

--
From _Christopher (M2M).
RefCode:3434vfTVFTTe V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for the
help."
 
_Christopher(M2M) said:
Larry Brasfield said:
message [big snip]
I think I have found a work around:-
It work OK if I included the 'System::Windows::Forms::'

WAS:
this->button1->MouseDown += new MouseEventHandler(this, Button_MouseDown);

NOW:

this->button1->MouseDown += new System::Windows::Forms::MouseEventHandler(this, Button_MouseDown);

So is there a problem with the 'using namespace'?
Such problems generally show up at compile time.
(The exception is when you don't know what you are
doing and use the wrong name.)

If changing that scope qualification changed the behavior
of your code, than that fact establishes that a different
object was bound to the resulting name. I would expect
that, in the absence of some other change you have not
mentioned, the result of the above change would be to
go from "will not compile" to "does compile". So, if
there was also a "using namespace ..." change, and if
that construct allowed you to use a name from a
different namespace than you intended, then I would
agree that there was indeed a specific problem with the
"using namespace ..." construct. In general, namespace
merging is a hazard, so I avoid use of the 'using' construct
to get at multiple namespaces in the same scope. In my
opinion, it is asking for trouble.
Can I ask you, did you try to compile the programme? If so did
it compile and run OK.

I saw no reason to create a project, paste code, etc.
My comments result from a study of your posts and
knowledge of C++.
It compiles OK here with the namespaces as thay are.

I think you miss my point. Consider the following code:
namespace something {
using namespace ::whatever;
somename var1;
::whatever::somename var2;
}
If, for the var1 definition, 'somename' can be pulled out of the
combination of { whatever was naturally in namespace something }
and { the ::whatever namespace }, and if '::whatever::somename' binds
to something different than plain 'somename', then it must be the case
that plain 'whatever' is ambiguous in that scope. That should fail to
compile if the compiler is working right. There would be no ambiguity
only if '::whatever::somename' and 'somename' refer to the same object.

Since you claim a behavioral change by using a different name, it must
be because either { you are mistaken about the behavior change being
due to that code change } or { the change of name caused a change of
referent }. If the latter, then I cannot see how you avoided an ambiguous
name error if you had the 'using System::Windows::Forms;' statement
present all along as you claimed in your post yesterday.

I guess you're welcome, but I do not imagine this is useful, yet.
There is something strange going on here which, once it is
unearthed, may prove useful to know.
 
Hi !

I did a copypaste of the code into a project, like you instructed. After
repeating the steps, I noticed the same behaviour as you described. The
actual function remained, but the delegate instance creation from
InitializeComponent gets removed.

Reasons why this occurs are currently unknown. I would presume it truly is a
bug, and if there are any members of the Visual Studio team reading these
groups, I would suggest they try it out. The only way I was able to remedy
this was to copy the contents of the handler away, then deleting both the
handler and the delegate addition, and using the Designer mode (from
button's event list) to manually type the name Button_MouseDown to the Click
field and pressing Enter. Then I pasted the code back. Now, when moving the
button around, the delegate is no longer removed.

The reason why I believe this to be a bug is in the way InitializeComponent
belongs to the Form Designer's property. Afterall, you're not supposed to
manually add event handler code inside the class. It seems that there's some
variable or other in the Designer that, when missing, indicates that the
button has no event handler registered, and thus it removes the delegate
addition to prevent the code from failing in compilation. This, clearly, is
a bug, and seems like a close relative to the ones we saw when ClassWizard
was still operational in VS 6.

-Antti Keskinen


_Christopher(M2M) said:
Tarek Madkour said:
Are you using MFC? Windows Forms? .NET

Can you post a small code sample that exhibits this bug?

File -> New -> Project -> Visual C++ Projects-> .NET-> Windows Forms
Application (.NET)
Name: = TestEvent <ENTER>
Switch to 'Form1.H' (View Code)
Edit -> Select All

Copy and paste the folling:-

------8<-------8<-----

#pragma once

namespace TestEvent
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change
the
/// 'Resource File Name' property for the managed resource
compiler tool
/// associated with all .resx files this class depends on.
Otherwise,
/// the designers will not be able to interact properly with
localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
System::Void Button_MouseDown(System::Object * sender, MouseEventArgs *
e)
{
MessageBox::Show(S"",S"");
}

public: System::Windows::Forms::Button* button1;
Form1(void)
{
InitializeComponent();
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(24, 16);
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->MouseDown += new MouseEventHandler(this,
Button_MouseDown);

//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 270);
this->Controls->Add(this->button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);
}
};
}

------>8------->8-----

Now run the programme, and it should work OK
E.G. Clicking the blank button will display the OK MessageBox in
'Button_MouseDown'.

Now switch to the Form1.h [Design] mode and move the button now view the
code.

I'm finding that
this->button1->MouseDown += new MouseEventHandler(this, Button_MouseDown);
Is not there any more. ;-(

What am I doing wrong?


BIG thank you if you (or anyone else) can help me.
--
From _Christopher (M2M).
RefCode:333CdcTZCDdc V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for
the help."
 
File -> New -> Project -> Visual C++ Projects-> .NET-> Windows
Forms Application (.NET)
Name: = TestEvent <ENTER>
Switch to 'Form1.H' (View Code)
Edit -> Select All

Copy and paste the folling:-
[...]

Now switch to the Form1.h [Design] mode and move the button now
view the code.

I'm finding that
this->button1->MouseDown += new MouseEventHandler(this,
Button_MouseDown); Is not there any more. ;-(

I think I have found a work around:-
It work OK if I included the 'System::Windows::Forms::'

WAS:
this->button1->MouseDown += new MouseEventHandler(this,
Button_MouseDown);

NOW:

this->button1->MouseDown += new
System::Windows::Forms::MouseEventHandler(this,
Button_MouseDown);

So is there a problem with the 'using namespace'?

This is a bug in the C++ CodeDOM which parses and generates code
for the Windows Forms designer.

Thanks for reporting it. We will fix it in our upcoming VS2005
release.

One question I have is whether this code was generated using the
designer or was hand-edited?

Thanks,
 
I think I have found a work around:-
It work OK if I included the 'System::Windows::Forms::'

WAS:
this->button1->MouseDown += new MouseEventHandler(this,
Button_MouseDown);

NOW:

this->button1->MouseDown += new
System::Windows::Forms::MouseEventHandler(this, Button_MouseDown);

--
From _Christopher (M2M).
RefCode:22CDcdTCDfds V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for the
help."
 
Tarek Madkour said:
File -> New -> Project -> Visual C++ Projects-> .NET-> Windows
Forms Application (.NET)
Name: = TestEvent <ENTER>
Switch to 'Form1.H' (View Code)
Edit -> Select All

Copy and paste the folling:-
[...]

Now switch to the Form1.h [Design] mode and move the button now
view the code.

I'm finding that
this->button1->MouseDown += new MouseEventHandler(this,
Button_MouseDown); Is not there any more. ;-(

I think I have found a work around:-
It work OK if I included the 'System::Windows::Forms::'

WAS:
this->button1->MouseDown += new MouseEventHandler(this,
Button_MouseDown);

NOW:

this->button1->MouseDown += new
System::Windows::Forms::MouseEventHandler(this,
Button_MouseDown);

So is there a problem with the 'using namespace'?

This is a bug in the C++ CodeDOM which parses and generates code
for the Windows Forms designer.

Thanks for reporting it. We will fix it in our upcoming VS2005
release.
That OK.
Did you try my 'My work around'?
One question I have is whether this code was generated using the
designer or was hand-edited?
By hand.

In the main programme, the code was created by the editer, but then I added
some other events by hand without adding System::Windows::Forms::
The 'TestEvent' programme was all done by hand.


--
From _Christopher (M2M).
RefCode:33CdcdTCDdfd V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for the
help."
 

That explains it. We generate code in InitializeComponent and read
it to regenerate the design-view of the form. Hand-editing our
generated code confuses our parser/generator. The result is what
you are experiencing.

I strongly suggest that you always use the designer to modify
Windows Forms code if you want the designer to work on this code
at a later point in time. That's why we have a comment on
InitializeComponent() asking you not to hand-edit it :)

Thanks,
 
Tarek Madkour said:
That explains it. We generate code in InitializeComponent and read
it to regenerate the design-view of the form. Hand-editing our
generated code confuses our parser/generator. The result is what
you are experiencing.

I strongly suggest that you always use the designer to modify
Windows Forms code if you want the designer to work on this code
at a later point in time. That's why we have a comment on
InitializeComponent() asking you not to hand-edit it :)

Thanks,

I thing you're right. I only fould this problem becouse I was converting a
C# programme to C++.

Sorry about that.
TC4N.

--
From _Christopher (M2M).
RefCode:33CdcdTCDDff V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for the
help."
 
Back
Top