User Input Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
bandali99
Knows some wx things
Knows some wx things
Posts: 40
Joined: Fri Jul 05, 2019 3:58 pm

User Input

Post by bandali99 »

Hi,

I want to add a text box where a user can input a number. The input would change the step size in my slider.
When I add

Code: Select all

wxTextCtrl *TextCtrl2 = new wxTextCtrl(panel, wxID_ANY, _("1"), wxPoint(1180, 180), wxSize(200, 40), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
in the slider class it does not appear.

I just started using wxwidgets so im not too sure how these classes are supposed to interact with one another
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: User Input

Post by Kvaz1r »

Provide full code for reproducing the behaviour.

Btw there is samples folder that contains examples of using for biggest part of the library.
bandali99
Knows some wx things
Knows some wx things
Posts: 40
Joined: Fri Jul 05, 2019 3:58 pm

Re: User Input

Post by bandali99 »

Hey I got it working but now I need to take the input and use it to change the step size.

Code: Select all

void myDoubleSlider::OnWheel(wxMouseEvent& event)
{
	int m, w, h;
	wxClientDC dc(this);
	dc.GetSize(&w, &h);
	wxPoint pos = event.GetLogicalPosition(dc);
	pos.x = pos.x - SLIDER_MARGIN;
	pos.y = h - SLIDER_MARGIN - pos.y;
	if (event.GetWheelRotation() > 0)
		m = 1; //THESE CHANGE THE STEP SIZE
	else
		m = -1;
	if (pos.y < h / 2) {
		leftval -= m;
		if (leftval < minval)  leftval = minval;
		if (leftval >= rightval) leftval = rightval;
	}
	else if (pos.y > h / 2) {
		rightval -= m;
		if (rightval > maxval) rightval = maxval;
		if (rightval <= leftval) rightval = leftval + 1;
	}
	Refresh();
	Update();
The user should input a number in the text box and should be stored as a global variable, m, that will be accessed by this function.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: User Input

Post by evstevemd »

bandali99 wrote: Wed Jul 10, 2019 7:40 pm Hey I got it working but now I need to take the input and use it to change the step size.
use wxTextCtrl::GetValue() to get string value and use wxAtoi() to change it to integer
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: User Input

Post by PB »

BTW, if the text control is for numbers only, you may consider using wxSpinCtrl instead of wxTextCtrl. With wxSpinCtrl you can be sure that the user entered a number and that number is within a range.
bandali99
Knows some wx things
Knows some wx things
Posts: 40
Joined: Fri Jul 05, 2019 3:58 pm

Re: User Input

Post by bandali99 »

Thanks for the tips, I decided to use SpinCtrl. I constructed a function which takes the value in the SpinCtrl and sets it as the step size. For some odd reason im getting an error saying that SpinCtrl1 is undefined on VS2017 but in code blocks the code runs fine. Any tips?

this is a snippet of my class:

Code: Select all

int stepsize = 1;
myDoubleSlider::myDoubleSlider(wxWindow *parent,
	wxWindowID id,
	int leftValue, int rightValue, int minValue, int maxValue,
	const wxPoint& pos,
	const wxSize& size,
	long style,
	const wxValidator& val,
	const wxString& name) : wxControl(parent, id, pos, size, wxBORDER_NONE)
{
	wxSpinCtrl* SpinCtrl1 = new wxSpinCtrl(this, wxID_ANY, _T("1"), wxPoint(1200, 200), wxDefaultSize, 0, 0, 100, 0, _T("ID_SPINCTRL1"));
	SpinCtrl1->SetValue(_T("1"));
	Connect(wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, (wxObjectEventFunction)&myDoubleSlider::OnSpinCtrl1Change);
	
}
this is the function:

Code: Select all

void myDoubleSlider::OnSpinCtrl1Change(wxSpinEvent& event)
{
	stepsize = SpinCtrl1->GetValue(); //error here "SpinCtrl1" is undefined
}
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: User Input

Post by doublemax »

Code: Select all

wxSpinCtrl* SpinCtrl1 = new wxSpinCtrl(this, wxID_ANY, _T("1"), wxPoint(1200, 200), wxDefaultSize, 0, 0, 100, 0, _T("ID_SPINCTRL1"));
SpinCtrl1 is a local variable, so you can't access it from other methods. Turn it into a member variable.
Use the source, Luke!
bandali99
Knows some wx things
Knows some wx things
Posts: 40
Joined: Fri Jul 05, 2019 3:58 pm

Re: User Input

Post by bandali99 »

doublemax wrote: Thu Jul 11, 2019 5:29 pm SpinCtrl1 is a local variable, so you can't access it from other methods. Turn it into a member variable.
ahh I see thanks, I got the spinctrl defined but when I use it, the scroll bars do not work im guessing that its an issue with my function that I have made

Code: Select all

void myDoubleSlider::OnSpinCtrl1Change(wxSpinEvent& event)
{
	stepsize = SpinCtrl1.GetValue();
	Update();
	Refresh();
	event.Skip();
}
sorry that this may feel like im spamming, im new to wxwidgets
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: User Input

Post by doublemax »

Please show real code, these small snippets are not helpful without context.
Use the source, Luke!
bandali99
Knows some wx things
Knows some wx things
Posts: 40
Joined: Fri Jul 05, 2019 3:58 pm

Re: User Input

Post by bandali99 »

https://github.com/fahadbandali/mediaslider

the main files that I am using are myDoubleSlider, and play

its a little messy ive been working all over the play these last couple of weeks
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: User Input

Post by doublemax »

I managed to build and run it and there are quite a few issues, but regarding the current problem:
myDoubleSlider.h

Code: Select all

wxSpinCtrl SpinCtrl1;
This should be:

Code: Select all

wxSpinCtrl *SpinCtrl1;
myDoubleSlider.cpp

Code: Select all

wxSpinCtrl *SpinCtrl1 = new wxSpinCtrl(...);
should be:

Code: Select all

SpinCtrl1 = new wxSpinCtrl(...);
Use the source, Luke!
bandali99
Knows some wx things
Knows some wx things
Posts: 40
Joined: Fri Jul 05, 2019 3:58 pm

Re: User Input

Post by bandali99 »

thanks!
Post Reply