OWL NExt - Knowledge Base

[ Home | Contents | Search | Next | Previous | Up ]

How to transferring data to and from a dialog box?

Date: 11/30/99
Time: 12:06:28 AM
Q.I am having trouble figureing out how to create custom dialog boxes and get information in and out of them. If you could send me any simple examples you might have hanging around your hard drive, I would appreciate it.

A.
The first step in transferring data to and from a dialog box is to decide on the data that is to be transferred:

    A TCheckbox will need to transfer a uint16 data type;
    A TEdit will need to transfer a char[MAX_CHARS] where MAX_CHARS is defined as some integer value e.g.
#define MAXCHARS 50
    A TComboBox will need to transfer a TComboBoxData
    etc...

    In this example, I will transfer 3 radiobuttons, a checkbox, and a string.
    Once you've decided on the data to be transferred (i.e. the controls in your
    dialog box), you need to define a structure which holds this data. This
    structure, called a transfer buffer, is used to transfer data to and from
    your dialog box.

struct MyDialogXfer{
    uint16 radio1;
    uint16 radio2;
    uint16 radio3;
    uint16 check1;
    char textline[MAX_CAHRS];

/*
    this section is optional it is one of three places where you can initialize your dialog box

    MyDialogXfer(){
    radio1 = BF_CHECKED; // radiobutton 1 checked
    radio2 = BF_UNCHECKED; // radiobuttons 2&3 unchecked
    radio3 = BF_UNCHECKED;
    check1 = BF_CHECKED; // checkbox 1 checked
    *textline = 0; // clear text line;
    };
*/
};

    If you initialize your transfer buffer in this way, every time you create a
    new MyDialogXfer, it will automatically be initialized with these settings.
    This method has the advantage that you can use the same transfer buffer over
    and over, keeping the same status from one use to the next. Note that
    either of the other two ways will override this method.

    Next you have to declare your dialog box class. In the constructor you
    should include a pointer to the above defined transfer buffer.

class MyDialog : public TDialog{
     // class constructor:
    // note that the second variable is a pointer to the transfer buffer
    public:
        MyDialog(TWindow *parent,  MyDialogXfer *tb, TModule *module = 0);

    // pointers to dialog box controls
    protected:
        TRadioButton *radio1;
        TRadioButton *radio2;
        TRadioButton *radio3;
        TCheckBox *check1;
        TEdit *textline;
};

    After this, the only thing left to do is define the dialog box's
    constructor. In the constructor you must define the controls in the same
    order in which they appear in your transfer buffer. Also, you must make a
    call to SetTransferBuffer; this function will cause the member variables in
    the transfer buffer to automatically update upon exiting the dialog box to
    reflect the status of the controls.

MyDialog::MyDialog(TWindow *parent, MyDialogXfer *tb, TModule *module)
:
    TDialog(parent, IDD_MYDIALOG, module)
{
    // construct the controls
    // Note that they _must_ be constructed in the same order in
    // which the are defined in the transfer buffer
    radio1 = new TRadioButton(this, IDC_RADIO1, 0, module);
    radio2 = new TRadioButton(this, IDC_RADIO2, 0, module);
    radio3 = new TRadioButton(this, IDC_RADIO3, 0, module);
    check1 = new TCheckBox(this, IDC_CHECK1, 0, module);
    textline = new TEdit(this, IDC_TEXTLINE, MAX_CHARS, module);

    SetTransferBuffer(tb);
}

    The second possible place to initialize the controls is in the dialog box's
    SetupWindow. If you initialize you dialog box in this way, the values will
    be set every time you create the dialog box.

void MyDialog::SetupWindow()
{
    radio1.Check();
    radio2.Uncheck();
    radio3.Uncheck();
    check1.Check();
    textline.Clear();
}

    Finally, your program needs to create the dialog box. The third way you can
    initialize your transfer buffer data is to create it and then modify its
    member variables before creating your dialog box. The advantage to this
    method is that it allows you to load default values from a file, the windows
    registry, or some other source. If, instead of defining it here, you define
    your transfer buffer in the class header, the status of the controls in the
    dialog box will remain unchanged from one use to the next.

int MyProgram::CallDialog()
{
    MyDialogXfer tb;

    // At this time you can initialize your transfer buffer
    tb.radio1 = BF_CHECKED;
    tb.radio2 = BF_UNCHECKED;
    tb.radio3 = BF_UNCHECKED;
    tb.check1 = BF_CHECKED;
    *tb.textline = 0;

    int retval = new MyDialog(this, &tb);
    return retval;
}

    Now let's put this all together. The following program fragment will call a
    dialog box from a TWindow object called MyWindow. The transfer buffer will
    be created in the declaration of MyWindow. Because of this, SetupWindow
    will not be over-ridden in MyDialog.

////////////////////////////////////////////////////////////////
//
// file mydialog.h
//
#if !defined(mydialog_h)
#define mydialog.h

#include <owl\dialog.h>
#include <owl\radiobut.h>
#include <owl\checkbox.h>
#include <owl\edit.h>

#include "resources.rh"

#define MAX_CHARS 50

struct MyDialogXfer;
{
uint radio1;
uint radio2;
uint radio3;
uint check1;
char textline[MAX_CAHRS];

MyDialogXfer()
{
radio1 = BF_CHECKED; // radiobutton 1 checked
radio2 = BF_UNCHECKED; // radiobuttons 2&3 unchecked
radio3 = BF_UNCHECKED;
check1 = BF_CHECKED; // checkbox 1 checked
*textline = 0; // clear text line;
};
};

class MyDialog : public TDialog
{
// class constructor:
// note that the second variable is a pointer to the transfer buffer
public:
MyDialog(TWindow *parent,
MyDialogXfer *tb,
TModule *module = 0);

// pointers to dialog box controls
protected:
TRadioButton *radio1;
TRadioButton *radio2;
TRadioButton *radio3;
TCheckBox *check1;
TEdit *textline;
};

#endif

////////////////////////////////////////////////////////////////
//
// file mydialog.cpp
//
#include <owl\pch.h>
#include "mydialog.h"

MyDialog::MyDialog(TWindow *parent, MYDialogXfer *tb, TModule *module)
: TDialog(parent, IDD_MYDIALOG, module)
{
// construct the controls
// Note that they _must_ be constructed in the same order in
// which the are defined in the transfer buffer
radio1 = new TRadioButton(this, IDC_RADIO1, 0, module);
radio2 = new TRadioButton(this, IDC_RADIO2, 0, module);
radio3 = new TRadioButton(this, IDC_RADIO3, 0, module);
check1 = new TCheckBox(this, IDC_CHECK1, 0, module);
textline = new TEdit(this, IDC_TEXTLINE, MAX_CHARS, module);

SetTransferBuffer(tb);
}

////////////////////////////////////////////////////////////////
//
// file mywindow.h
//
#if !defined(mywindow_h)
#define mywindow_h

#include "mydialog.h"

class MyWindow : public TWindow
{
MyWindow(TWindow* parent,
const char far* title = 0,
TModule* module = 0);

protected:
MyDialogXfer tb;
void LoadDefaults(MyDialogXfer &buffer);
CallDialog();
};

#endif

////////////////////////////////////////////////////////////////
//
// file mywindow.cpp
//
#include <owl\pch.h>
#include "mywindow.h"

MyWindow::MyWindow(TWindow* parent, const char far* title, TModule* module)
: TWindow(parent, title, module)
{
LoadDefaults(tb);
}

int MyWindow::CallDailog()
{
return MyDialog(this, tb).Execute();
}

void MyWindow::LoadDefaults(MyDialogXfer &buffer)
{
// In this function you can set the initial status of the dialog box
// of, if left empty, the defaults defined in "mydialog.h" apply

buffer.radio1 = BF_CHECKED;
buffer.radio2 = BF_UNCHECKED;
buffer.radio3 = BF_UNCHECKED;
buffer.check1 = BF_CHECKED;
*buffer.textline = 0;
}

I hope this will be of help to you.

Frank.

Last changed: July 14, 2001