Solved

Printing output to edit control  in MFC

Posted on 2007-03-23
23
1,908 Views
Last Modified: 2013-11-26
Hello,
I want to be able to print out certain integer and float values to a textbox with some text before the values so that the user knows whats being printed. I need to print the values in a loop. However I have some problems. This is the background information:

I dont know if i should be adding a CEdit control variable or a Cstring value variable! So I added both and i tried stuff out with both. The Cedit Control variable is called tEdit_output and the CString value variable is called tEdit_Result. Please provide me an example with whichever variable you think  Ineed to use!
I need the values of some integers (say the value of j here for simplicity ) to be printed out in the format "
"And the value is ", j

in the following loop:

for(j =0;j<num;j++)
{
...print here to window

}//j
I
Its a multiline edit control.

THanks in advance,
-D

0
Comment
Question by:indacrypt
  • 11
  • 6
  • 6
23 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 150 total points
ID: 18779387
Point of clarification for you - there is a section specific to MFC questions - this is really meant for .NET questions.  

Have code like the following in the loop

CString s;
tEditOutput.GetWindowText(s);  //get current text
s += _T("\r\n");  //add a new line
s += INSERT YOUR TEXT HERE //put your new text here
tEditOutput.SetWindowText(s);  //now put back into the edit


---------------
CString ss;
ss.Format(_T("Hello %d"), 54);  // ss now is "Hello 54"
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 100 total points
ID: 18779449
You can use both tEdit_output and tEdit_Result.

1) First prepare the big string to print...

CString str1, str2;

for(j =0;j<num;j++)
{
   str2.Format( "Some text  %d\r\n", yourvalue[j];
   str1 += str2;
}

2) Then print it...

if you want to use tEdit_output you can write:
 
    tEdit_output.SetWindowText( str1 );

if you want to use tEdit_Result you can write:

    tEdit_Result = str1;
    UpdateData( false );


Remember that UpdataData() is called by the framework in OnInitDialog() and OnOK() to perform tha data transfer from each variable member to each window control.

Personally I prefer to use control variabile to menage controls look, while I use value variable to menage controls content.

0
 

Author Comment

by:indacrypt
ID: 18779452
thanks andy....i guess i just checked .Net coz im using that programming environment. But thanks for clarifying that. What is the GetWindowText function for  in the above snippet of code?I mean im not getting anything from the window...im just printing to it.THanks!
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 150 total points
ID: 18779582
I thought you would be adding each new line onto the existing text.  IF you aren't then:

CString s;
s = INSERT YOUR TEXT HERE //put your new text here
tEditOutput.SetWindowText(s);  //now put into the edit



I prefer using the control rather than the variable the UpdateData will perform an update on EVERY control you have a variable mapped to.  This way you only update the control whose contents has actually changed.
0
 

Author Comment

by:indacrypt
ID: 18779668
alb66...what does updateData(false) do?
0
 

Author Comment

by:indacrypt
ID: 18779680
ANd Andy..one more thing with your s and ss exmaples...did u mean it can be done either way....
0
 
LVL 19

Expert Comment

by:alb66
ID: 18779736
UpdateData( false ) trasfer data from the value member variables ( tEdit_Result ) to the window control (the edit box).
As Andy said, this occurs for every control in the dialog with an associated  varable.
0
 

Author Comment

by:indacrypt
ID: 18779771
So if say I want to print 4 different things with an explanation for each of them I need 4 string variables??.....
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 18779782
s and ss - one was showing you how to put a string into the edit control, the other was how to put an integer into a string with text.
0
 

Author Comment

by:indacrypt
ID: 18779875
sorry ..my questions must seem retarded. Its just that im  really tired though.  Ok..heres what I tried:

for(j =0;j<form2_ldPtr[i].get_tot_num_of_legs();j++)\
{
 temp_ord_num = form2_ldPtr[i].get_tract_num(j);
CStr_Output.Format("\n Tractor for leg %d",j, " =%d \n " , temp_ord_num);
CStr_temp += CStr_Output;
}//j
tEdit_output.SetWindowText(CStr_temp);

Now in debug mode I've checked that temp_ord_num has the correct values. However when i print it out to the window it prints onl "Tractor for leg 0"...Tractor for leg 1 etc. It does not print temp_ord_num. Instead theres a weird character like a little rectangle thts printed after tractor for leg j, and also it doesnt insert the new line character....
0
 
LVL 19

Expert Comment

by:alb66
ID: 18779898
Try this:

CStr_Output.Format("\r\n Tractor for leg %d =%d \r\n" , j ,  temp_ord_num);
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 18779911
new line - \r\n

SetWindowText as I said earlier will replace any text existing in the control.  If you want to append to the existing text you need my first code snippet.




ps
retarded questions - everyone must start learning somewhere
0
 

Author Comment

by:indacrypt
ID: 18779970
okie...the code from both of u works thank  u VERY MUCH....only problem NOW is....the scroll button is not being enabled:| a wee bit of the last line is getting cut off !! It is multiline and all the atuoscroll options are enabled for the output text window
0
 

Author Comment

by:indacrypt
ID: 18779976
"s += _T("\r\n");  " ..andy in that line of code what is the _T for?
0
 
LVL 19

Expert Comment

by:alb66
ID: 18780030
Try to set "vertical scroll" property to true.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 18780034
UNICODE - crudely said if UNICODE is defined then it will wide chars (2 byte) else ASCII chars (1 byte)
0
 

Author Comment

by:indacrypt
ID: 18780083
no unicode and veritcal scroll property is true:(
0
 
LVL 19

Expert Comment

by:alb66
ID: 18780099
You must set "Vertical scroll" to true...
0
 

Author Comment

by:indacrypt
ID: 18780140
Vertical Scroll from the properties for the text window, is set to TRUE. Do you mean I have to do something else to set it to true?...
0
 
LVL 19

Expert Comment

by:alb66
ID: 18780173
sorry, i misunderstood your previous comment...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 18780203
UNICODE or not - doesn't make any difference to what you are doing.
Win 95, 98, Me are ASCII based, WIn NT, 2000, XP, Vista are UNICODE based.  They can run ASCII programs (albeit with slight performance hit).
0
 

Author Comment

by:indacrypt
ID: 18780259
darn..what else can it be
0
 

Author Comment

by:indacrypt
ID: 18780265
wierd..i reduced the size of the text window a little bit and now it scrolls!! thanks so much for your help
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now