wxTextCtrl

Fellow Programers, I was recently dwelling into C++ programming deeper than I have ever explored before. In my trials, I came across a GUI helper classes, known as widgets. So after practicing one or two times I thought that I would build a LC tank resonant frequency calculator. Here is the complete code:

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "wx/wx.h"
#include <iostream>
#include <math.h>
//#include "calc.cpp"

class MyApp: public wxApp
{
	virtual bool OnInit();
};

class MyFrame: public wxFrame
{
public:
	MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
	
	void OnQuit(wxCommandEvent& event);
	void OnAbout(wxCommandEvent& event);
	void Calc(wxCommandEvent& event);
	
	float f;
	float c;
	float h;
	
	DECLARE_EVENT_TABLE();
};



enum
{
	ID_Quit = 1,
	ID_About,
	ID_CAL
	
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
	EVT_MENU(ID_Quit, MyFrame::OnQuit)
	EVT_MENU(ID_About, MyFrame::OnAbout)
	EVT_MENU(ID_CAL, MyFrame::Calc)
END_EVENT_TABLE()


IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
	MyFrame *frame = new MyFrame ( _("Hello World"), wxPoint(50, 50), wxSize(450, 340) );
	frame->Show(true);
	SetTopWindow(frame);
	return true;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
        :wxFrame(NULL, -1, title, pos, size)
{
	wxPanel *panel = new wxPanel(this, wxID_ANY);
	//The main panel
	wxStaticBox *in = new wxStaticBox(panel, -1, wxT("Component Values"), wxPoint(5, 5), wxSize(340, 150));
	wxStaticText *pre =  new wxStaticText(panel, -1, wxT("Prefix"), wxPoint(240, 30));
	wxStaticText *Cap = new wxStaticText(panel, -1, wxT("Capacitance:"), wxPoint (15, 58));
	wxTextCtrl *C = new wxTextCtrl(panel, -1, wxT(""), wxPoint(100,53), wxSize(120,30));
	wxComboBox *Cp = new wxComboBox(panel, -1, wxT(""), wxPoint(240, 53), wxSize(70,30));
	Cp->Append(wxT("F"));		// *10^1
	Cp->Append(wxT("mF"));		// *10^-3
	Cp->Append(wxT("uF"));		// *10^-6
	Cp->Append(wxT("nF"));		// *10^-9
	Cp->Append(wxT("pF"));		// *10^-12
	
	wxStaticText *Ind = new wxStaticText(panel, -1, wxT("Inducance:"), wxPoint(15, 88));
	wxTextCtrl *I = new wxTextCtrl(panel, -1, wxT(""), wxPoint(100, 83), wxSize(120,30));
	wxComboBox *Ip = new wxComboBox(panel, -1, wxT(""), wxPoint(240, 83), wxSize(70, 30));
	Ip->Append(wxT("H"));
	Ip->Append(wxT("mH"));
	Ip->Append(wxT("uH"));
	Ip->Append(wxT("nH"));
	Ip->Append(wxT("pH"));
	
	wxButton *calc = new wxButton(panel, ID_CAL, wxT("Calculate"), wxPoint(240, 120));
	Connect(ID_CAL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyFrame::Calc));
	
	wxStaticBox *inn = new wxStaticBox(panel, -1, wxT("Results"), wxPoint(5,160), wxSize(340,90));
	wxStaticText *res = new wxStaticText(panel, -1, wxT("Fequency:"), wxPoint(15, 180));
	
	//

	wxMenu *menuFile = new wxMenu; //Creates menu option "File"
						      //One must first create the menu options before making the Menu tab or the Menu itself.
	menuFile->Append( ID_About, _("&About...") ); //create file menu option "About" 
	menuFile->AppendSeparator();                  //Adds a separator to the "File" menu
	menuFile->Append( ID_Quit, _("E&xit") );      //Create file menu option "Quit"
	
	wxMenuBar *menuBar = new wxMenuBar;
	menuBar->Append( menuFile, _("&File") );
	
	SetMenuBar( menuBar);
	
	CreateStatusBar();
	SetStatusText( _("Welcome to wxWidgets.") );
}

void MyFrame::Calc(wxCommandEvent& WXUNUSED(event))
{
	// The button works, is linked, and defined
	wxString *ca = new wxString();
	ca=C->GetValue();
	//wxString *capval = C->GetValue();
	//How do you reference the TextCtrl C?
	
	f = 1 / (2*M_PI* sqrt(h*c));
}

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) // Close window when "close" button is hit. 
{
	Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) // Do this when you click on "About".
{
	wxMessageBox( _("This is a wxWidget Hello World Sample"), _("About Hello World"), wxOK | wxICON_INFORMATION, this);
}


now my interface works just fine, buttons and all, but I can`t recieve any values from the two wxTextCtrl. I need to take the numbers in the field and lug them into an equation. I tried to use:

 
ca=C->GetValue();


but when I compile with G++ I get

'C' was not declared in this scope


Please assist! Thank you.
Your declaring the text control in the MyFrame constructor. Declare it in the header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class MyFrame: public wxFrame
{
public:
	MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
	
	void OnQuit(wxCommandEvent& event);
	void OnAbout(wxCommandEvent& event);
	void Calc(wxCommandEvent& event);
	
	float f;
	float c;
	float h;
        wxTextCtrl *C;
	DECLARE_EVENT_TABLE();
};
Last edited on
thank you very much naraku9333; It works fine now :)
Topic archived. No new replies allowed.