-
April 9th, 2012, 03:06 AM
#1
[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);
}
}
}
-
April 9th, 2012, 04:46 AM
#2
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".
Last edited by ovidiucucu; April 9th, 2012 at 04:54 AM.
-
April 10th, 2012, 03:41 PM
#3
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);
}
}
};
-
April 11th, 2012, 10:30 AM
#4
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
-
April 11th, 2012, 04:03 PM
#5
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);
}
}
-
April 12th, 2012, 06:24 AM
#6
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...
-
April 12th, 2012, 10:26 AM
#7
Re: Convert from C# to VC++2010 -dataGridView?
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
-
April 12th, 2012, 11:59 AM
#8
Re: Convert from C# to VC++2010 -dataGridView?
Right, from this moment it becomes a "pure" managed issue.
[ Moved thread ]
-
April 12th, 2012, 06:20 PM
#9
Re: Convert from C# to VC++2010 -dataGridView?
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);
}
}
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.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
April 14th, 2012, 02:09 AM
#10
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);
}
}
-
April 14th, 2012, 07:23 AM
#11
Re: Convert from C# to VC++2010 -dataGridView?
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.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
April 14th, 2012, 03:58 PM
#12
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.
-
April 14th, 2012, 04:16 PM
#13
Re: [RESOLVED] Convert from C# to VC++2010 -dataGridView?
Mostly out of curiosity...
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. )
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
April 14th, 2012, 05:20 PM
#14
Re: [RESOLVED] Convert from C# to VC++2010 -dataGridView?
It's one of our converters (you can check out the link in my signature).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|