Hi,

I was creating a Gui for one of my classes and I hit a major bump. I am trying to take words from a text file and just display it in the text box. I know how to read files, what I don't know is how to change the information in the text box. Where can I put the code so that when the gui isn't doing anything the program searches through the file, and sends a message to update the text box.

help would be greatly appreciated.
Chris

My code looks like this

//---------------------------------------------------------------------------
wchar_t *ComboBoxItems = L"Sri Lanka";  //holds the text
string Combo;
HWND Handle = NULL; // Specify a handle as null
HWND hWnd;  //specify another handle this will be used in the dialog box
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); //Function
//which handles requests for the dialog box
ifstream file;
int input_position = 0;

//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));



return FALSE;

}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{             
    file >> Combo;
    SendDlgItemMessage(hWnd, WM_COMMAND, IDC_EDIT1, NULL, NULL);

    HWND hWndPrincipal;
    switch(Msg)
    {
    case WM_INITDIALOG:
        hWndPrincipal = GetDlgItem(hWndDlg, IDC_EDIT1);

        SetWindowText(hWndPrincipal, ComboBoxItems);
        //ComboBoxItems = L"Hello";
        return TRUE;


    case WM_COMMAND:
        switch(wParam)
        {
        case IDQUIT:
            EndDialog(hWndDlg, 0);
            return TRUE;

                }
        break;
    }

    return FALSE;

Before we can solve that problem there are a few questions that you may need to answer.

1. What exactly to do mean by "when the gui isn't doing anything?" do you mean "when the user hasn't put input anything for 30 seconds" or "when there hasn't been any mouse activity with the currently active window?"

2. Is the gui going to be doing anything else besides just updating text? If so are you going to need threads?

3. Are you allowed to use threads? Would it be a good idea to do so?

Here are a few extra questions for you. It's not directly related to solving your problem, but it may help you build a faster application.
Is the file that you're reading from going to be updated regularly? At what interval? If not wouldn't it be easier to read everything to a static or global array?

Thank you for your reply:

1) When I say when the Gui isn't doing anything I realy mean that I would like this to be the background activity for the program. I would like the program to check this file for input, update the display.

2) the only other thing the gui does is it has 3 buttons, each button opens a different exe file.

3) There would be no problem to use threads except that I have never used them before, and would need time to learn how to implement threads.

4) The file will be updated periodically by another program. Essentially my goal is to take information from one exe file and display it on the gui, just so the user has a little idea of whats going on. I have looked at some networking tutorials, but due to my time constraints i think using the file method would be simpler.

Thank you for your help
chris

To fix your problem you can use the windows function SetTimer. You don't need to know how to use threads to use it and it will automatically run a function for you.

Take a look at how to use set timer through this example by looking at the InitInstance ( search for SetTimer in this function ) and the WndProc function ( you have to add the case WM_TIMER ).
You can use this to run your update function at periodic intervals.

http://www.daniweb.com/code/snippet1138.html

This is the reference to the MSDN explaination of the function
http://msdn.microsoft.com/en-us/library/ms644906.aspx

You can also use this to call the function that you going to use to read the file.

Ex.

WM_TIMER:
    CheckFile();
    UpdateGui();
    break;

If you have any other question feel free to ask.

Hey thank you for the advice the settimer works, but now whenever I use setwindowtext in the WM_TIMER the program crashes. IT gives me an undhandled exception whenever I run it.
Whevever I move the setwindowtext() function, the program executes just fine. Any ideas as to what I'm doing wrong, I've searched other forums with no luck.

my code now looks like this:

#include <windows.h>
#include "Resource.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//---------------------------------------------------------------------------
UINT TimmerID = 0;
string temp;
HWND Handle = NULL; // Specify a handle as null
HWND hTimer;
HWND hWnd;  //specify another handle this will be used in the dialog box
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); //Function
//which handles requests for the dialog box
ifstream file;
int input_position = 0;
HWND hWndPrincipal;
HWND hWndPrincipal2;
HWND hWndPrincipal3;
HWND hWndPrincipal4;
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    ofstream file2("Gui_display.txt");
    file2<<"No Class"<<endl;
    file2<<"No Class"<<endl;
    file2<<"No Message"<<endl;
    file2.close();


    DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST),
              hWnd, reinterpret_cast<DLGPROC>(DlgProc));



    return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{

    TimmerID = SetTimer(hWndDlg, IDC_CHECKTIME, 4000, NULL);
    hWndPrincipal = GetDlgItem(hWndDlg, IDC_EDIT1);
    hWndPrincipal2 = GetDlgItem(hWndDlg, IDC_EDIT2);
    hWndPrincipal3 = GetDlgItem(hWndDlg, IDC_EDIT3);
    hWndPrincipal4 = GetDlgItem(hWndDlg, IDC_EDIT5);
    string class1;
    string class2;
    string message;

    switch(Msg)
    {
    case WM_INITDIALOG:

        SetWindowText(hWndPrincipal, L"No Time");
        SetWindowText(hWndPrincipal2, L"No Class");
        SetWindowText(hWndPrincipal3, L"No Class");
        SetWindowText(hWndPrincipal4, L"No Message");
        return TRUE;


    case WM_COMMAND:
        switch(wParam)
        {
        case IDQUIT:
            EndDialog(hWndDlg, 0);
            return TRUE;

        case IDTK:
            ShellExecute(Handle,L"open",L"Time_keeper.exe",
                NULL,NULL,SW_SHOWDEFAULT);
            return TRUE;

        case IDOCS:
            ShellExecute(Handle,L"open",L"input.txt",
                NULL,NULL,SW_SHOWDEFAULT);
            return TRUE;

        case IDOML:
            ShellExecute(Handle,L"open",L"command.txt",
                NULL,NULL,SW_SHOWDEFAULT);
            return TRUE;

        }

    case WM_TIMER:
        hWndPrincipal2 = GetDlgItem(hWndDlg, IDC_EDIT2);
        SetWindowText(hWndPrincipal2, L"Hello");
       return TRUE;

    default:
        break;
    }

    return FALSE;
}
//---------------------------------------------------------------------------
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.