Printing output to edit control in MFC

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

indacryptAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alb66Commented:
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
indacryptAuthor Commented:
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
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

AndyAinscowFreelance programmer / ConsultantCommented:
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
indacryptAuthor Commented:
alb66...what does updateData(false) do?
0
indacryptAuthor Commented:
ANd Andy..one more thing with your s and ss exmaples...did u mean it can be done either way....
0
alb66Commented:
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
indacryptAuthor Commented:
So if say I want to print 4 different things with an explanation for each of them I need 4 string variables??.....
0
AndyAinscowFreelance programmer / ConsultantCommented:
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
indacryptAuthor Commented:
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
alb66Commented:
Try this:

CStr_Output.Format("\r\n Tractor for leg %d =%d \r\n" , j ,  temp_ord_num);
0
AndyAinscowFreelance programmer / ConsultantCommented:
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
indacryptAuthor Commented:
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
indacryptAuthor Commented:
"s += _T("\r\n");  " ..andy in that line of code what is the _T for?
0
alb66Commented:
Try to set "vertical scroll" property to true.
0
AndyAinscowFreelance programmer / ConsultantCommented:
UNICODE - crudely said if UNICODE is defined then it will wide chars (2 byte) else ASCII chars (1 byte)
0
indacryptAuthor Commented:
no unicode and veritcal scroll property is true:(
0
alb66Commented:
You must set "Vertical scroll" to true...
0
indacryptAuthor Commented:
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
alb66Commented:
sorry, i misunderstood your previous comment...
0
AndyAinscowFreelance programmer / ConsultantCommented:
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
indacryptAuthor Commented:
darn..what else can it be
0
indacryptAuthor Commented:
wierd..i reduced the size of the text window a little bit and now it scrolls!! thanks so much for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.