|
Luc Pattyn wrote: I don't think so. The first-level reply to a question is an answer by default (like this one).
I see you are correct. Although at times the "General Comment" is the default like in this current reply. Ok I guess the default changes based on the type of the post you are replying to, I never knew that. Thanks for pointing all this out to me, although I imagine it will take me some time to start remembering to select a type, I never used to worry about it aside from occasional use of the joke type.
led mike
|
|
|
|
|
Hi guys,
I have a problem with event on key press enter.
I have a TextBox1, then I am a typing to him and I need to if KeyPress was ENTER call my function Result().
I think I can not do something function like that:
<br />
void textBox1_KeyPress( Object^ , System::Windows::Forms::KeyPressEventArgs^ e )<br />
{<br />
if(e->KeyCode == Key::Enter)
Result();
}<br />
I am looking for some pieces of code in msdn, but I still have a problem somewhere.
KeyPreview in Properties Form1 is true.
I am so sorry for this stupid question
|
|
|
|
|
From the docs:
"The KeyPress event is not raised by noncharacter keys;
however, the noncharacter keys do raise the KeyDown and
KeyUp events."
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks, but I still not work. I have this:
void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
{
if ( e->KeyCode == Keys::Enter)
textBox2->Text = "Enter was pressed";
}
|
|
|
|
|
Polar_Sheep wrote: I still not work
Maybe try setting the TextBox's AcceptsReturn property to true.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
AcceptsReturn True... Still is not work. Shall I have some code in Initialize component?
I mean something like this:
this->textBox1->KeyDown = gcnew... something?
|
|
|
|
|
Yes you need to create the delegate and add it to the event.
Can you show that code?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
#pragma once
namespace PoHavarii {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
protected:
private: System::Windows::Forms::TextBox^ textBox2;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->AcceptsReturn = true;
this->textBox1->Location = System::Drawing::Point(108, 55);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(181, 20);
this->textBox1->TabIndex = 0;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(108, 94);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(181, 20);
this->textBox2->TabIndex = 1;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(391, 264);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->KeyPreview = true;
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
{
if ( e->KeyCode == Keys::Enter)
textBox2->Text = L"Enter was pressed";
}
};
}
|
|
|
|
|
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++
|
|
|
|