Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you add innitial text from a text document into an edit box?

Status
Not open for further replies.

Hazor

Programmer
May 23, 2004
7
0
0
NZ
How on earth would you go about adding innital text into an edit box from a text file (file.txt)...

This text file is constantly updating

i know how to add text into edit box using variable (e.g. m_strEditBox = ("TEXT GOES HERE");


but how to have innital text from an updating text document file..................................... and how can i make it so updates every x ms




and

will i go about the same process for a "Listbox" thanks!! you guyz rock!

 
To have it update periodically, you could use a timer. Use SetTimer to set up either a callback function or a WM_TIMER message to be issued every x ms.

In the callback function or WM_TIMER message handler, you could scan the text file, find the text you want, and update the edit box (also works with labels, buttons, and other types of controls too) by using SetWindowText (or send message WM_SETTEXT to the control).

For a listbox it is a little more complicated because it holds more than one string. You can add strings, change strings, or remove strings. You can also change the currently selected string (for single selection listbox) or iterate through the listbox strings to see which is selected (for multiple selection listbox). All these things you can do by sending the listbox control window certain messages (list of them is here in MSDN).
 
What would be the syntax for the edit box to update every 60,000 ms for a text file located in say... C:\\folder\\file.txt
 
Add a message handler for WM_TIMER, and in it do this:

Code:
ifstream f ("C:\\folder\\file.txt");
char string [101];
f.getline (string, 100);
m_edit.SetWindowText (string);

To start updating, do this:

Code:
SetTimer (1, 60000, NULL);

It will not start updating until the 60,000ms. If you want it to run once before the timer starts, call the WM_TIMER message handler directly:

Code:
OnTimer (1);
SetTimer (1, 60000, NULL);

In my example here, I'm assuming you are using MFC, the code is added to a dialog box class, and you've made a control variable for your edit box, called m_edit. Also I didn't add any error checking, which you might need in case the file doesn't exist when the program tries to open it.
 
What file do i need to #include?
 
Probably one of more of the following:

#include <afxwin.h>
#include <afxext.h>
#include <afxdisp.h>
#include <afxdtctl.h>
#include <afxcmn.h>
#include <fstream>
using namespace std;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top