|
In initializeComponent(), create a delegate and add it to textBox1's KeyDown event:
this->textBox1->KeyDown += gcnew System::KeyEventHandler(this, &Form1::textBox1_KeyDown);
Mark
*edit* Fixed code in bold
Mark Salsbery
Microsoft MVP - Visual C++
modified on Saturday, July 26, 2008 4:35 PM
|
|
|
|
|
'void PoHavarii::Form1::textBox1_KeyDown(System::Object ^,System::Windows::Forms::KeyEventArgs ^)' : the specified function does not match the delegate type 'void (System::Object ^,System::EventArgs ^)'
It needs other function, but we are on the right way Many thanks
|
|
|
|
|
this->textBox1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::textBox1_KeyDown); Thank you very much and sorry for your time
|
|
|
|
|
Oops! You got it
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I assume you added the TextBox to your form using the designer. An easy way to add events to a control is to view the Properties window for the control (just right-click on the TextBox and select Properties if it's not already showing). The default view shows most of the public properties you can access for that control (things like Text, Enabled, Visible, Height, etc.). There's a button with a lightning bolt at the top of this window. Clicking this button will show all the events this control has. Double-clicking on an event will automatically insert the function and link it to your control in InitializeComponent().
Slightly longer explanation than I wanted, but once you walk through it, it's really easy. I just with it would place the function definitions in Form1.cpp instead of Form1.h
- dybs
|
|
|
|
|
This is a problem I've had since I started using C++/CLI
take for example this simple ref class
ref class CFoo
{
public:
String^ text;
CFoo(void)
{
this->text = "";
}
CFoo(const CFoo^ _cpy)
{
this->text = _cpy->text;
}
void operator=(const CFoo^ _rhs)
{
this->text = _rhs->text;
}
};
And now a main that has a couple CFoo variables
int main(void)
{
CFoo^ foo1 = gcnew CFoo();
CFoo^ foo2 = gcnew CFoo();
foo1->text = "Bill";
foo2 = foo1;
foo1->text = "Bob";
return 0;
}
When I run this when I run foo2 = foo1; " my assignment operator is never called and all that happens is the handle from foo1 is given to foo2. Then when I change the text of foo1 it "changes" foo2 which is just at this point a reference to foo1;
Now if I swap out
foo2 = foo1;
with
foo2->operator =(foo1);
then everything works as I expect it.
The way I have always gotten around this was to call the copy constructor like so
foo2 = gcnew CFoo(foo1);
Although this works in my opinion it doesn't look as nice as a simple = operator.
So what I'm looking for is to get foo1 = foo2; to call my assignment operator or is it just not possible? I've googled it to death and although its not stopping my program from working it's been something thats been bugging me to death.
modified on Thursday, July 24, 2008 12:05 AM
|
|
|
|
|
If you assign a handle to a managed object to another
handle to a managed object of the same type, only the
handle is copied.
To get a distinct new copy you can do one of the following:
1) Call your assignment operator explicitly (wasteful - why do a construct then an assignment?)
2) Use a copy constructor
3) Implement ICloneable in your class and use its Clone() method
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
What mark said. If you want to read more about why this is the case you might read some of Lippmans comments on C++/CLI like this article[^]
led mike
|
|
|
|
|
|
I am new to .NET interoperability. I need to write some C++ code which would access the methods of a .NET component. Any help is greatly appreciated.
Regards,
|
|
|
|
|
|
hi,
I wanted to know how how safe it is to use directory::move!! i have tried it in my code it works fine some times but some times gives IO exception. if also check if the file is in use!! Can anyone guide me!!
Thanks
Samir
|
|
|
|
|
VCsamir wrote: I wanted to know how how safe it is to use directory::move
It's perfectly "safe" as long as you own the files and folders being moved.
Otherwise, it's unsafe.
You shouldn't be moving things that you don't have control over.
If you must, then you must be prepared to handle the exceptions,
because there's no reliable way to find out if a file is in use.
For example, a file can become in use right after you check and find
it's not in use.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
sir,
i have also found that the move function to work properly the root folder should be same. so i tried copying the folder first recursively and then deleting it recursively
it works fine but if the file is in use it gives ioexception. can u suggest me a way to handle this?
Regards
Samir
|
|
|
|
|
You need to handle it however is appropriate to your application.
Moving a file that's in use could break the application that has the
file open - that's why it's not allowed.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I have a C++ application that I would like to use the managed DateTime class. However, I would like to add an extension method to the class, call it Foo(), that returns a double. I know that in C#, you can write the class as follows:
using System;
namespace ManagedCode {
public static class DateTimeExtension {
public static double Foo(this DateTime dt) {
return 83.5;
}
}
} The problem is that I cannot figure out how to call the extension method defined above from C++. I have attempted the following, but get the error "error C2039: 'Foo' : is not a member of 'System::DateTime'":
#using "ManagedCode.dll"
#using <mscorlib.dll>
#using <system.dll>
using namespace ManagedCode;
using ManagedCode::DateTimeExtension;
using System::DateTime;
public int main(void) {
DateTime foo;
double temp = foo.Foo();
return 0;
} I also know the way I would have done this in C++ 8 years ago would be as follows:
#using <mscorlib.dll>
#using <system.dll>
class DateTime : public System::DateTime {
public:
DateTime();
double Foo();
}; But here, I get the error that I cannot create an unmanaged class that inherits from a managed class. OK, fine. Next I tried the following:
#using <mscorlib.dll>
#using <system.dll>
ref class DateTime : public System::DateTime {
protected:
~DateTime();
public:
DateTime();
!DateTime();
double Foo();
}; But here, I get the error that I cannot inherit from a sealed class. Does anyone know how I can accomplish the goal of getting a DateTime class into C++ that exports an additional method, Foo()? Thanks in advance,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
You could wrap a System::DateTime in another class.
Here's an example of a ref class wrapper:
public ref class MyDateTime
{
System::DateTime dateTime;
public:
static operator System::DateTime(MyDateTime ^val)
{
return val->dateTime;
}
MyDateTime()
{
dateTime = System::DateTime::UtcNow;
}
MyDateTime(System::DateTime datetime)
{
dateTime = datetime;
}
void Foo()
{
}
};
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I considered that, but decided that it was no different than just adding a classless function unassociated with DateTime. Another solution would be to re-implement the entirety of the DateTime class, where I publicly expose the underlying DateTime object's methods and properties with the same signatures, but my DateTime return type. This is a lot of work that should be able to be done in about 10 lines of code using inheritance or extension methods. Is there some way to incorporate the class from C# AFTER I add the extension method "Foo"? Or, is there some way to define an extension method in C++? Thanks for the reply,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Whatever works for you
If I come up with an elegant solution I'll let you know - right now, I've got nothing.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Jeff,
I am afraid it can't be done. I quote:
"Extension methods are a really nice syntactic sugar. They're not really added to the class, as we can see, but the compiler makes it feel like they are."
from here.[^]
So it is a new C# thingy, nothing C++/CLI currently is capable of.
|
|
|
|
|
I have three forms A,B and C. C has a webbrowser contorl
Upon a certain event form A opens a url in webbrowser of C.Upon some other event FormB should open a url in same webbrowser of C.. How do I achieve the desired behaviour.
Perhaps, by creating a global instance of form C. If so, where should I declare the global instance, in form A,B or C?
My project is a CLR winForms project.
Any pointers would be appreciated.
|
|
|
|
|
I want to write a windows service (myService) by using C++/CLI (Visual Studio 2005). This myService can use MFC function for OLE Automation.
Just for a first try, I did the following project configuration and coding:
• Creating a Windows Service under C++/CRL
• Adding ServiceInstaller with start type automatic and sereviceprocessInstaller with Account Local System
• Setting MFC in a Shared DLL
• Setting Common Language Runtime Support (/clr)
• Setting Multithreaded-Debug-DLL (/MDd)
• Adding #include <afxdisp.h> in stdafx.h
Compile is always successful. But when I use myServie.exe –Install to install my windows service in Visual Studio 2005 Command Prompt, I always get the following exceptions
When debugging:
Unhandled exception at 0x78374588 (mfc80ud.dll) in myService.exe: 0xC0000005: Access Violation reading location 0x00000000.
In Visual Studio 2005 Command Prompt:
Unhandled exception: System.AccessViolationException
Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.
at _wWinMainCRTStartup()
Does anyone know the reason? Where am I wrong?
Many thanks for any help!
|
|
|
|
|
Try adding the following lines to your _tmain before the call to ServiceBase::Run()...
AfxInitialize();
AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
Beyond that, there's no guarantee the MFC classes you want to use are going
to work in your service. Some, like the database classes, do work. Most don't -
like anything involving windows (CWnds).
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark!
Thank you for you reply!
I added such code like that you said.
AfxInitialize();
AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
ServiceBase::Run(gcnew ccs_clientWinService());
But the result is same as without that.
Anyway thank you so much! hope you still have futher ideas for me.
|
|
|
|
|
I'd need to see the call stack at the time of the exception...
Without it I could only guess.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|