[RESOLVED] Convert from C# to VC++2010 -dataGridView?
Hi,
I like to convert the following codes from c# to VC++2010. Any help for me?
Actually the codes for dataGridView - Enter Key process for cursor movement from one cell to another cell [like a tab key...]
public class MyDataGrid : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
Keys key = keyData & Keys.KeyCode;
if (key == Keys.Enter)
{
base.OnKeyDown(new KeyEventArgs(keyData));
return true;
}
else
{
return base.ProcessDialogKey(keyData);
}
}
}
Re: Convert from C# to VC++2010 -dataGridView?
Just thinking about "translating" C# into (Visual) C++ code is a "piece of cake".
However, DataGridView Class is .NET stuff, then you cannot simply derive from it "like a walking in the park", let's say, in an MFC application.
Basically, I see two choices:
- get rid of .NET's DataGridView and replace it with an unmanaged component, e.g. "Microsoft Data Grid" ActiveX control, or
- make a separate .NET Class Libray project to implement the grid, then "import" it in the MFC application; not sure about this one, but may try this article as a "start digging point".
Re: Convert from C# to VC++2010 -dataGridView?
As Ovidiu said, it's no picnic to convert to native C++, but if you are one of the handful of people in the world coding in C++/CLI, then you can use:
Code:
public ref class MyDataGrid : DataGridView
{
protected:
virtual bool ProcessDialogKey(Keys ^keyData) override
{
Keys ^key = keyData & Keys::KeyCode;
if (key == Keys::Enter)
{
DataGridView::OnKeyDown(gcnew KeyEventArgs(keyData));
return true;
}
else
{
return DataGridView::ProcessDialogKey(keyData);
}
}
};
Re: Convert from C# to VC++2010 -dataGridView?
Hi David,
The above code will be declared in Header File..Then, how this can be initiated in cpp File?
Can you explain me please?
Thanks
Re: Convert from C# to VC++2010 -dataGridView?
Code:
//.h file code:
public ref class MyDataGrid : DataGridView
{
protected:
virtual bool ProcessDialogKey(Keys ^keyData) override;
};
//.cpp file code:
bool MyDataGrid::ProcessDialogKey(Keys ^keyData)
{
Keys ^key = keyData & Keys::KeyCode;
if (key == Keys::Enter)
{
DataGridView::OnKeyDown(gcnew KeyEventArgs(keyData));
return true;
}
else
{
return DataGridView::ProcessDialogKey(keyData);
}
}
Re: Convert from C# to VC++2010 -dataGridView?
Hi David,
I Checked...Its giving the following errors. Further is it possible to learn.....?
Errors:
01. warning C4490: 'override' : incorrect use of override specifier; 'MyDGV::ProcessDialogKey' does not match a base ref class method
02.error C3063: operator '&': all operands must have the same enumeration type
03.error C2275: 'System::Windows::Forms::Keys' : illegal use of this type as an expression
04.error C2275: 'System::Windows::Forms::Keys' : illegal use of this type as an expression
Thanks For the helps...
Re: Convert from C# to VC++2010 -dataGridView?
Quote:
Originally Posted by
MAHEY
Hi David,
I Checked...Its giving the following errors. Further is it possible to learn.....?
Please note that your code and questions are off-topic for this forum. The code you're attempting to compile is not "traditional" C++, with emphasis on MFC and ATL (that is what this forum is about).
You should post any follow-ups in the Managed C++ forum.
Regards,
Paul McKenzie
Re: Convert from C# to VC++2010 -dataGridView?
Right, from this moment it becomes a "pure" managed issue.
[ Moved thread ]
Re: Convert from C# to VC++2010 -dataGridView?
Quote:
Originally Posted by
David Anton
Code:
//.h file code:
public ref class MyDataGrid : DataGridView
{
protected:
virtual bool ProcessDialogKey(Keys ^keyData) override;
};
//.cpp file code:
bool MyDataGrid::ProcessDialogKey(Keys ^keyData)
{
Keys ^key = keyData & Keys::KeyCode;
if (key == Keys::Enter)
{
DataGridView::OnKeyDown(gcnew KeyEventArgs(keyData));
return true;
}
else
{
return DataGridView::ProcessDialogKey(keyData);
}
}
Quote:
Originally Posted by
MAHEY
Errors:
01. warning C4490: 'override' : incorrect use of override specifier; 'MyDGV::ProcessDialogKey' does not match a base ref class method
02.error C3063: operator '&': all operands must have the same enumeration type
03.error C2275: 'System::Windows::Forms::Keys' : illegal use of this type as an expression
04.error C2275: 'System::Windows::Forms::Keys' : illegal use of this type as an expression
Keys is a managed enum, not a reference type. (As such it's much closer to a value type than a reference type, yet managed enums and value types are different animals.) Removing all the ^s, which are mostly used with reference types in C++/CLI, from David Anton's snippet should get you some steps further.
Though they're caused by the bogus ^s as well, some specific notes on the first two errors/warnings:
C4490: The DGV does have a virtual method of that name, but that takes a parameter of type Keys, not Keys ^. These two different signatures are considered two distinct overloads.
C3063: This is due to the fact that the left-hand side of the & is of type Keys ^ while the right-hand side Keys::KeyCode is of the correct type Keys.
Re: Convert from C# to VC++2010 -dataGridView?
First, I would like to thank to our David Anton & Eric....
My heartiest thanks to those kindness...
Because of the above 2-greaters, I get clear by the following way...Let it be useful to the searchers...
//MyDataGrid.h
using namespace System;
using namespace System::Windows::Forms;
#pragma once
public ref class MyDataGrid : DataGridView
{
protected:
virtual bool ProcessDialogKey(Keys keyData) override;
};
//MyDataGrid.cpp
#include "StdAfx.h"
#include "MyDataGrid.h"
using namespace System;
using namespace System::Windows::Forms;
bool MyDataGrid::ProcessDialogKey(Keys keyData) {
Keys ^ key = keyData & (System::Windows::Forms::Keys::KeyCode);
//if (key == Keys::Enter)
if (keyData.ToString()=="Return") {
DataGridView::OnKeyDown(gcnew KeyEventArgs(keyData));
return true;
}
else {
return DataGridView::ProcessDialogKey(keyData);
}
}
Re: Convert from C# to VC++2010 -dataGridView?
Quote:
Originally Posted by
MAHEY
First, I would like to thank to our David Anton & Eric....
You're welcome! :)
However, though your code works now, some additional comments:
Code:
Keys ^ key = keyData & (System::Windows::Forms::Keys::KeyCode);
//if (key == Keys::Enter)
if (keyData.ToString()=="Return") {
The last line of that snippet works but is terribly inefficient. The commented-out line would work as well (after adding a { at the end of course, to get the syntax right).
However, key is still declared with an extraneous ^. That way it's a tracking handle to a boxed Keys instance instead of simply being a Keys instance itself. This is a rarely needed construct that would work here as well but also is unnecessarily inefficient. Simply removig the ^ makes it what it should be.
So this is how I would probably write that snippet:
Code:
Keys key = keyData & System::Windows::Forms::Keys::KeyCode;
if (key == Keys::Enter) {
Please use code tags when posting code.
Also, though rarely done, it is good practice and an aid to other readers to mark a thread [RESOLVED] using the Thread Tools menu at the top of the thread display once your question has been answered.
Re: [RESOLVED] Convert from C# to VC++2010 -dataGridView?
I agree about the "Keys" enum - the problem was that without a "using System.Windows.Forms", the converter can't confirm that "Keys" refers to the enum and not some other class of that name.
Re: [RESOLVED] Convert from C# to VC++2010 -dataGridView?
Mostly out of curiosity...
Quote:
Originally Posted by
David Anton
[...] the problem was that without a "using System.Windows.Forms", the converter can't confirm that "Keys" refers to the enum and not some other class of that name.
What converter? Is there actually a converter from C# to C++/CLI? (And if there is, is it from MS? I'd rather expect them to publish a converter for the opposite direction. ;))
Re: [RESOLVED] Convert from C# to VC++2010 -dataGridView?
It's one of our converters (you can check out the link in my signature).