CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2011
    Posts
    73

    [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);
    }
    }
    }

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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:
    1. get rid of .NET's DataGridView and replace it with an unmanaged component, e.g. "Microsoft Data Grid" ActiveX control, or
    2. 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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Aug 2005
    Posts
    198

    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);
    		}
    	}
    };
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  4. #4
    Join Date
    Dec 2011
    Posts
    73

    Thumbs up 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

  5. #5
    Join Date
    Aug 2005
    Posts
    198

    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);
    	}
    }
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  6. #6
    Join Date
    Dec 2011
    Posts
    73

    Smile 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...

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Convert from C# to VC++2010 -dataGridView?

    Quote Originally Posted by MAHEY View Post
    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

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Convert from C# to VC++2010 -dataGridView?

    Right, from this moment it becomes a "pure" managed issue.

    [ Moved thread ]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Convert from C# to VC++2010 -dataGridView?

    Quote Originally Posted by David Anton View Post
    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 View Post
    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.

  10. #10
    Join Date
    Dec 2011
    Posts
    73

    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);
    }
    }

  11. #11
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Convert from C# to VC++2010 -dataGridView?

    Quote Originally Posted by MAHEY View Post
    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.

  12. #12
    Join Date
    Aug 2005
    Posts
    198

    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.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  13. #13
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: [RESOLVED] Convert from C# to VC++2010 -dataGridView?

    Mostly out of curiosity...

    Quote Originally Posted by David Anton View Post
    [...] 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.

  14. #14
    Join Date
    Aug 2005
    Posts
    198

    Re: [RESOLVED] Convert from C# to VC++2010 -dataGridView?

    It's one of our converters (you can check out the link in my signature).
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

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
  •  





Click Here to Expand Forum to Full Width

Featured