|
I am getting confused in declaring a 2d array
I need a n*4 matrix (n rows with 4 cols in each row)
I wanted to do something like
__int8 stepArray[][]={{1,2,3,4},{5,6,7,8}};
corresponds to
1 2 3 4
5 6 7 8
where n is implicitly 2
I am getting an error . Please help
|
|
|
|
|
only last dimension can be undefined
try
__int8 stepArray[2][]={{1,2,3,4},{5,6,7,8}};
better yet use std::vector
|
|
|
|
|
Actually, it's the opposite. The first dimension can be undefined, as in:
__int8 stepArray[][4]={{1,2,3,4},{5,6,7,8}};
|
|
|
|
|
you can use pointers and memmory allocation to do the trick! try this, it should work, I had an assignment on this, a while ago... Let's see if I can dig up some code
__int8 *stepArray[4] = NULL;
stepArray = (__int8 **) malloc(n * sizeof(__int8*));
while(n-- > 0)
stepArray[n] = (__int8*) malloc(sizeof(__int8));
after this, you can use it as a normal 2d matrix (stepArray[n][m] =...)
It is far more easy to use a vector of some sort, but it's more fun to do it yourself (at least, I experience it that way )
hope this helps
ps.
I don't want te scare you, but beware of memory leaks, because they sneak in very easy...
A student knows little about a lot.
A professor knows a lot about little.
I know everything about nothing.
|
|
|
|
|
(note: italics will contain the text from the documentation of Chris' Grid control. I got it from here)
The underlying class of the grid control is CGridCtrl which is derived from CWnd.
So far, so good
To use it, either use the MS Visual C++ dialog editor to place a custom control on a dialog, and enter "MFCGridCtrl" (no quotes) as the Class name. To subclass the control using the DDX mechanism (this will be done by default by the ClassWizard) use the DDX_GridControl function instead of the DDX_Control function (just manually change the ClassWizard entry). This ensures your control is correctly registered as a drop target and avoids some weird win95 issues.
I am assuming that all of the above is the "either" half of the "either...or" and the below is the "or" half........
Alternatively you can use CGridCtrl::Create:
CGridCtrl grid;
grid.Create(rect, pParentWnd, nID);
where rect is the dimensions, pParentWnd is the parent window, and nID is the id.
Here is where I am stuck. Is nID the control ID of the Grid? Does this mean I still need to create it in the dialog editor? If so, do I still make it a custom control?
I'd really like to use this control. I looked at the demo app and it is really nice!
Any help would be appreciated.
|
|
|
|
|
I generally use the first method. However the second method also works.
Terry O`Nolley wrote:
Here is where I am stuck. Is nID the control ID of the Grid? Does this mean I still need to create it in the dialog editor? If so, do I still make it a custom control?
No, you do not have to use the dialog editor for this, however it is generally easier to do this. You assign it an ID that is not the same as any ID in its parent.
If you wanted to create it in the dialog editor you could make a static control with a specific ID and in your OnInitDialog get the rect. Then create the grid with this rect and give it the id of the static control. Then delete the static control.
John
|
|
|
|
|
Thanks! I got it to compile, but even after calling the SetColumnCount and SetRowCount it is invisible.
So I ripped out that code and went back to the dialog control:
To subclass the control using the DDX mechanism (this will be done by default by the ClassWizard) ......
Since it says it is done by default, does this mean I don't need to do this?
Or does he mean that I need to create a member variable for the control and only if I want to override it should I do the below?
use the DDX_GridControl function instead of the DDX_Control function (just manually change the ClassWizard entry).
Sorry for all the newbie questions, but I am only at this late time getting off of the SDK programming model and I get paranoid when I let something automatically generate weird code mappings for me
|
|
|
|
|
Terry O`Nolley wrote:
use the DDX_GridControl function instead of the DDX_Control function (just manually change the ClassWizard entry).
This is what you need to do.
John
|
|
|
|
|
John M. Drescher wrote:
This is what you need to do.
I am trying to do that, but the control ID for the grid control doesn't appear in class wizard.
The dialog box that contains the grid control fires up when it is supposed to and I included the GridCtrl.h in the source file for the dialog class that contains the grid control.
But when I go to Class Wizard, the only controls it sees are the 2 default buttons.
The grid control appears in the class wizard's first screen when I select the CDIalog derived class that holds the GridCtrl, but it disappears when you click on the Member Variables tab.
I realize the documentation is probably more than enough for most people but I guess I am just dense
|
|
|
|
|
Add this to your class definition:
CGridCtrl m_Grid;
Add this after CDialog::DoDataExchange(pDX); in your DoDataExchange(CDataExchange* pDX) member function
DDX_GridControl(pDX, IDC_GRID, m_Grid);
John
|
|
|
|
|
Grinding along. Thanks for putting up with me.
Well, I added the following:
At the top of my dialog class header file, I added #include "GridCtrl.h"
In my dialog class header file at the top of the public: section I added CGridCtrl m_Grid;
I added DDX_GridControl(pDX, IDC_GRID, m_Grid); per your instructions
When I compiled, I got this:
c:\dev\visualstudio\vc98\include\wtypes.h(369) : error C2632: 'char' followed by 'char' is illegal
c:\dev\visualstudio\vc98\include\wtypes.h(369) : warning C4091: 'typedef ' : ignored on left of 'unsigned char' when no variable is declared
c:\dev\visualstudio\vc98\include\wtypes.h(375) : error C2628: 'DWORD' followed by 'unsigned' is illegal (did you forget a ';'?)
c:\dev\visualstudio\vc98\include\wtypes.h(375) : error C2628: 'DWORD' followed by 'long' is illegal (did you forget a ';'?)
c:\dev\visualstudio\vc98\include\wtypes.h(375) : warning C4091: 'typedef ' : ignored on left of 'unsigned long' when no variable is declared
Is this maybe a #if defined block that is being left open somewhere?
|
|
|
|
|
Are you compiling for UNICODE??
[EDIT]
I am really not sure what is causing the problem is in this case.
[/EDIT]
John
|
|
|
|
|
No unicode.
I'm about ready to shell out the $500 bucks for HalloGram's Ultimate Grid.
Thanks for walking through this with me.
If I ever get this to work, I am going to submit an article that walks a newbie to MFC through getting the thing to function.
|
|
|
|
|
Don't be discouraged. I am not sure what your problem is but it should not be too hard to fix. I have used the grid in at least 5 of my applications...
John
|
|
|
|
|
Why don't you try creating a simple dialog application and place a grid in it to see how to get it working?
John
|
|
|
|
|
Do you have UCHAR or ULONG defined in any of your files?? That looks like it is the problem..
This is line 369 of wtypes.h
typedef unsigned char UCHAR;
To me the error says that UCHAR is defined as char already...
[EDIT]
I tested this theory by putting a
#define UCHAR char
in my stdafx.h file and I got the following errors:
StdAfx.cpp
d:\pf\microsoft sdk\include\windef.h(47): error C2632: 'char' followed by 'char' is illegal
d:\pf\microsoft sdk\include\windef.h(47): warning C4091: 'typedef ': ignored on left of 'unsigned char' when no variable is declared
d:\pf\microsoft sdk\include\wtypes.h(379): error C2632: 'char' followed by 'char' is illegal
d:\pf\microsoft sdk\include\wtypes.h(379): warning C4091: 'typedef ': ignored on left of 'unsigned char' when no variable is declared
d:\pf\microsoft sdk\include\sqltypes.h(109): error C2632: 'char' followed by 'char' is illegal
d:\pf\microsoft sdk\include\sqltypes.h(109): warning C4091: 'typedef ': ignored on left of 'unsigned char' when no variable is declared
[/EDIT]
John
|
|
|
|
|
Exactly!
I double clicked on the compile error and found that line in wintypes.h.
I commented it and the other typedef error out .
When I got a good compile, I guessed that it must have been defined in one of the GridCtrl files - or else there would have been an error.
So after I commented it out and built the executable, I uncommented it again and as of 2 minutes ago it was working.
Now I'm going to dig through GridCtrl files to see how it was defined. Hopefully it was defined the same way as in wintypes.h
I was just checking back in to thank you again and tell you what the problem was
|
|
|
|
|
Okay - I found the culprit. I was assuming the redefinition was in the GridCtrl files, but it is in the Victor Image Library files I am using. God help me if the Victor library definition is different that the Windows typedefs........
|
|
|
|
|
Oh man. Now that I got it to compile I am trying to use it.
In my OnInitDialog handler, I set the columns to 10 and rows to 10 and set the first row as a fixed row. no prob - looks great.
Also in OnInitDialog I called m_Grid.SetItemText(1, 1, "ID"); and it returned FALSE - failure.
Is there an entire bunch of initialization hoops I need to jump through?
I was hoping I wouldn't need to start carving out pieces of the example project. The docs looked easy enough for me to jump right in but it just isn't happening for me..........
|
|
|
|
|
Have you tried tracing the calls using the debugger?
cheers,
Chris Maunder
|
|
|
|
|
Have you declared IDC_GRID in your resource.h file?
cheers,
Chris Maunder
|
|
|
|
|
Hey there!
Thanks for making this tool available to us all. I was looking for a grid and couldn't find anything except for Active-X controls. Yours seemed to be just what I was looking for.
Chris Maunder wrote:
Have you declared IDC_GRID in your resource.h file?
It is declared and appears properly when I click the button to display the dialog box that contains it. The calls to m_Grid.SetColumnCount();, m_Grid.SetColumnCount(); & m_Grid.SetFixedRowCount(); work fine and the grid displays with the proper number of rows, columns and fixed rows.
But when I go the next step and call m_Grid.SetItemText(1, 1, "ID"); it returns FALSE.
Note that all of the above calls are in my OnInitDialog handler.
=============== EDIT =====================
Nevermind I discovered the problem. I was trying to set the text of the first (fixed) row but forgot to reference it as the zeroeth row. EXCELLENT work!!!!!!!!!!!!!!!!
|
|
|
|
|
In your resource file:
#define IDC_GRID 1004 In your header:
#include "GridCtrl_src/GridCtrl.h"
class CGridCtrlDemoDlg : public CDialog
{
public:
...
CGridCtrl m_Grid;
In your implementation file:
...
void CGridCtrlDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
...
DDX_Control(pDX, IDC_GRID, m_Grid);
}
...
BOOL CGridCtrlDemoDlg::OnInitDialog()
{
m_Grid.[SetSomeProperties...];
cheers,
Chris Maunder
|
|
|
|
|
Hi,
I have a doubt. How can I do to change the menu prompt at runtime ?
thanks.
|
|
|
|
|
menu prompt ?
the menu item text or the text that is displayed in the status bar when moving the mouse over a menu item ?
the first one is simple, use CMenu::ModifyMenu
the other things, I don't know .
Maximilien Lincourt
"Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )
|
|
|
|