Help with Calculator GUI...

Aug 25, 2013 at 1:11pm
Hi! I'm creating a calculator with a GUI, and I'm having some troubles.
I've created a button what says: "Calculate" and a Text Box what says "Value".

My questions are:
How I create a label like VB.Net? Just a text in my GUI, can someone explain for me? 8)

And...

How I get the text which the user has typed in the TextBox? Like the scanf in a TextBox...

Thanks!
Aug 25, 2013 at 1:27pm
This depends on what you are using to make your GUI. Is it the WinAPI? Is it MFC? Is it Qt?

I'll do a quick mockup in Qt to show you.
Aug 25, 2013 at 1:34pm
I'm using Code:Blocks to make the Calculator...

The button code(if helps):
CreateWindow(TEXT("button"), TEXT("Calculate"),
WS_VISIBLE | WS_CHILD,
70, 200, 412, 25, /*X Pos, Y Pos, Widht, Height*/
hwnd, (HMENU) 1, NULL, NULL
);
Aug 25, 2013 at 1:44pm
Ah, you're using the winAPI. (I've #include <windows.h>).

Unfortunately I can't help you with this. I'm in Linux. But here's what I've done in Qt to show you what it might look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <QMainWindow>
#include <QApplication>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0) : QMainWindow(parent)
    {
        verticalLayout = new QVBoxLayout(centralWidget);
        verticalLayout->setSpacing(6);
        verticalLayout->setContentsMargins(11, 11, 11, 11);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        Button1 = new QPushButton(centralWidget);
        Button1->setObjectName(QString::fromUtf8("Button1"));

        verticalLayout->addWidget(Button1);

        lineEdit = new QLineEdit(centralWidget);
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));

        verticalLayout->addWidget(lineEdit);

        this->setCentralWidget(centralWidget);

        this->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
        Button1->setText(QApplication::translate("MainWindow", "PushButton", 0, QApplication::UnicodeUTF8));

        QMetaObject::connectSlotsByName(this);
    }

    ~MainWindow(){}

private slots:
    void on_pushButton_clicked()
    {
        // In this function, you've just clicked the button.
        // Here I'm just extracting text from the lineEdit box, that's it.
        QString str = lineEdit->text();
    }

private:
    QWidget *centralWidget;
    QVBoxLayout *verticalLayout;
    QPushButton *Button1;
    QLineEdit *lineEdit;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}
Aug 25, 2013 at 5:48pm
How I create a label like VB.Net? Just a text in my GUI, can someone explain for me? 8)

A label is a STATIC control. Create a window of this type with the appropraite text, position, etc.

Static Control
http://msdn.microsoft.com/en-us/library/windows/desktop/bb760769%28v=vs.85%29.aspx

How I get the text which the user has typed in the TextBox? Like the scanf in a TextBox...

To get the text use the following function with the HWND for the Edit control.

GetWindowText function
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633520%28v=vs.85%29.aspx

GetWindowTextLength function
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633521%28v=vs.85%29.aspx

Once you get the text you'll have to convert in into an int or double, or whatever you're using, for you calculations.

Andy

PS I assume you understand how the WM_COMMAND message works!?
Last edited on Aug 25, 2013 at 5:58pm
Aug 25, 2013 at 6:05pm
If you don't understand how the WM_COMMAND works check this out:
http://www.winprog.org/tutorial/
Aug 28, 2013 at 1:53am
Ty andywestken, it was almost what I was searching for...

But, how I can "say" to the computer which textbox he is going to get the value? ;)

Maybe you can give an exemple using a textbox with ID "x" which just accept numbers(as I said, I'm doing a calculator)

Thx;
Last edited on Aug 28, 2013 at 2:03am
Aug 28, 2013 at 10:57am
But, how I can "say" to the computer which textbox he is going to get the value? ;)

As you're using CreateWindow to create your Edit control, you use the HWND value that returns.

Andy

PS You use the term "text box" I presume you are talking about Edit controls (created using CreateWindow with the class "EDIT").
Aug 29, 2013 at 2:36am
Can you make a example? =/ I'm new with C++ programming :|

Yes, "Text Box" it's "Edit", in VB.Net it's text box ;P
Topic archived. No new replies allowed.