|
|
A long time ago, I needed a way to put information into edit boxes etc. in my MFC dialog applications "on the fly". That is, I didn't want to wait till a function terminated and the particular window was updated/repainted. I wanted to see the data change real time as it was running. I didn't want to use a timer so I came up with the following function.
void DisplayNow(int nID, LPCTSTR lpszString)<br />
{<br />
CWnd* pDISPLAY_Wnd = GetDlgItem(nID);<br />
<br />
pDISPLAY_Wnd->SetWindowText(lpszString);<br />
pDISPLAY_Wnd->UpdateWindow();<br />
}
where nID is the ID of the edit box etc. that I want to display the data in
I overloaded more versions of the same functions for integers, floats etc. I am only showing the version that does strings here.
Anyways these functions have allowed me to display data on the fly and I have been quite happy with them for quite some time now.
But, anytime I want to use them, I have to copy/paste them into the application's dialog file. At one point, I tried to wrap them in a class and use them that way but the compiler burped hard at that because the handle to the window (pDISPLAY_Wnd) is not known at compile/link time. So I continued copy/pasting.
Recently I tried to put all the functions into a file called DisplayNow.cpp and DisplayNow.h hoping that, even though I couldn't make them into a class, this would prevent me from the copy/paste thing.
So DisplayNow.cpp looks like this:
#include "stdafx.h"<br />
#include "DisplayNow.h"<br />
<br />
void DisplayNow(int nID, LPCTSTR lpszString)<br />
{<br />
CWnd* pDISPLAY_Wnd = GetDlgItem(nID);<br />
<br />
pDISPLAY_Wnd->SetWindowText(lpszString);<br />
pDISPLAY_Wnd->UpdateWindow();<br />
}
and DisplayNow.h looks like this:
void DisplayNow(int nID, LPCTSTR lpszString);
Now, when I try to compile, I get this error:
DisplayNow.cpp(6) : error C2660: 'GetDlgItem' : function does not take 1 parameters
Am I destined to being forced to copy/paste all these functions into very new application that I create????
Thank you in advance
Pierre
|
|
|
|
|
What if you make a common base class and derive your dialog classes from it - something like:
class CMyDialog : public CDialog
{
public:
CMyDialog() : CDialog() {}
explicit CMyDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL) :
CDialog(lpszTemplateName, pParentWnd) {}
explicit CMyDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL) :
CDialog(nIDTemplate, pParentWnd) {}
void DisplayNow(int, LPCTSTR);
};
...
void CMyDialog::DisplayNow(int nID, LPCTSTR lpszString)
{
CWnd* pDISPLAY_Wnd = GetDlgItem(nID);
if (pDISPLAY_Wnd)
{
pDISPLAY_Wnd->SetWindowText(lpszString);
pDISPLAY_Wnd->UpdateWindow();
}
}
-- modified at 18:39 Friday 23rd February, 2007
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
As Mark said, or you could just add a third parameter to the DisplayNow function
void DisplayNow(CWnd *pParent, int nID, LPCTSTR lpszString)
{
CWnd *pDISPLAY_Wnd = pParent->GetDlgItem(nID);
...
}
And in dialog you call it like this:
DisplayNow(this, ID, _T("Text"));
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
pblais wrote: Tired of copy/pasting code
Here is some food for thought Avoiding GetDlgItem
By Joseph M. Newcomer. [^]
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
|
|
|
|
|
I know ActiveX methods can return complex data types like structures or classes,I just can't quite figure out how.... For example, I have a class containing 4 data elements (strings) and 4 functions acting as getters... I want to return the class through a method call in my activex ctrl.
From what I've read, I beleive I can do it via IDispatch and a definition in the IDL file, or by returning a VARIANT...
Can someone please point me to a tutorial or article that could help me figure this out?
Thanks in advance!
|
|
|
|
|
I have three questions?
ONE: It is said "using references is safer than using pointer."
Why?
TWO: If I want node objects in heap, how can I assign the pointers from heap memory?
example code)
Node& Nodes[100];
? = new Node();
THREE: Is using pointers faster than using references?
Thanks!
Yonggoo N
|
|
|
|
|
Yonggoo wrote: using references is safer than using pointer
Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.)
I don't understand what you are doing with two. Node* pNode = new Node; Don't know what the references line has to do with anything.
Yonggoo wrote: Is using pointers faster than using references?
With an optimizing compiler the performance is generally the same since internally references are pointers.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Is this code OK!
Node* Nodes[101];
refNodes& getNodes()
{
for(int i=1;i<=100; ++i)
{
Nodes[i] = new Node();
}
return Nodes;
}
Thanks!
Yonggoo
|
|
|
|
|
Yes, but you've failed to initialize Nodes[0] . The preferred style is:
for (int i = 0; i < 101; ++i)
...
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
No, since you can't convert Node*[101] to Node&.
You could return Nodes[101] as a Node**. I'm not sure how you would return it as a reference or why you would bother.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Joe Woodbury wrote: Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.)
That is a red herring...
struct A {
int a;
};
void func1(A& r)
{
printf("r.a = %d\n", r.a);
}
void func2(A* p)
{
func(*p);
}
...
A* p = 0;
func2(p); Guess where it'll blow up on you in virtually all C++ compilers? References in C++ are not safer than pointers. It's just an illusion...
|
|
|
|
|
Joergen Sigvardsson wrote: void func2(A* p)
{
func(*p);
}
The only reason it blows up is because you are dereferencing a NULL pointer, not because references are not safe. And one wonders why so many products have stupid warning labels; to prevent them from being used in ways they were not intended to be used.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
PJ Arends wrote: The only reason it blows up is because you are dereferencing a NULL pointer, not because references are not safe.
My point is that you can't trust a reference to be always valid, as many people suggest.
--
Fun for the whole family - except grandma and grandpa
|
|
|
|
|
Yonggoo wrote: Node& Nodes[100];
? = new Node();
I don't think you can have an array of references
Yonggoo wrote: Node* Nodes[101];
refNodes& getNodes()
{
for(int i=1;i<=100; ++i)
{
Nodes[i] = new Node();
}
return Nodes;
}
return Nodes; is returning a Node**. Is that what a refNodes& is?
Mark
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
Basically what I'm trying to do is open up a file, check to see if everything is ok, and then get a line from the file and pop up a box with the text from the getline(). Then I got all these errors upon compiling. The line which the errors are pointing to is marked with a smilie. I don't really know what to do with them so I'd appreciate any help. The code:
bool CRatiosNewSearchDialog::LoadExpressionsFile(CString Path)
{
bool Success = true;
CString FirstLineText = "";
ifstream expressionFInput(Path);
if(expressionFInput.fail())
{
MessageBox("Could not load expressions file: " + (CString)Path, "Error", MB_ICONWARNING);
Success = false;
}
else if(EOF == expressionFInput.peek())
{
MessageBox("Expressions file is empty: " + (CString)Path, "Error", MB_ICONWARNING);
Success = false;
}
else
{
:^)getline(expressionFInput, FirstLineText, '\n');
MessageBox(FirstLineText, "First Line", MB_OK);
}
return Success;
}
</code>
These are the errors:
\Work\Copy of Ratios_2006\Ratios\ratios_searchdialogs.cpp(58): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
\Work\Copy of Ratios_2006\Ratios\ratios_searchdialogs.cpp(58): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
\Work\Copy of Ratios_2006\Ratios\ratios_searchdialogs.cpp(58): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Ax> &' from 'CString'
I'd appreciate any help. -John
|
|
|
|
|
CoffeeAddict19 wrote: expects 2 arguments - 3 provided
This is the answer. Look at the first error message.
|
|
|
|
|
getline(expressionFInput, FirstLineText);
same error. :/ It doesn't care whether I have 3 arguments or two. Go figure.
|
|
|
|
|
You've provided 2 arguments. Does the compiler say that you have provided 3??? Strange thing. Have you rebuilt the project or just proper cpp?
|
|
|
|
|
Anyway if the error occurs after your change it means tha you have used wrong arguments check 2 another errors from your first post.
Try to use istream instead of ifstream and string instead of CString
|
|
|
|
|
I gave in and did it with a string...works fine. Thanks. Still annoys me the though. Has anyone written a defenitive guide on how all of these character and string formats work together? Has anyone done a performance analysis on the different types of strings and their functions to see what works fastest in what appication? Seems like someone could write a book on it.
|
|
|
|
|
Yes. I rebuilt it.
I hate Microsoft.
e:\Work\Ratios\ratios_searchdialogs.cpp(61): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
|
|
|
|
|
--------------------------------------------------------------------------------
I'll preface this by saying, I'm pretty new to C++ and Windows programming, I've mostly done embedded systems in C and assembler. This is probably a really simple thing, I just can't seem to get it done.
I'm trying to use the Static LED control http://www.codeguru.com/Cpp/controls/controls/article.php/c2351[^] in a Dialog based application.
On the main dialog, it's no problem, works just like a champ. However, I have a popup dialog that I want to put some detailed information on, and I can't seem to get it working there. I have some floating point values that I can simply update the class variables to set their displayed values, but the LED CLed class isn't so simple. How do I access this control on the popup dialog, what am I missing?
|
|
|
|
|
gunrunnerjohn wrote: How do I access this control on the popup dialog...
From where?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Well, I was trying to set the LED controls from the main dialog that launched the popup dialog itself. Is there a way to add the code to the popup dialog, I didn't see any obvious way. I don't want to have to click any buttons in the popup to make the values appear, I want them there when the dialog opens. The data I need to actually set the LED status is in some global variables, so I can get to them from anywhere.
-- modified at 21:30 Friday 23rd February, 2007
|
|
|
|