Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » text box action with enter key
text box action with enter key [message #25090] Wed, 10 February 2010 02:29 Go to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
After typing data into a text box how can an action be triggered when the enter key is pushed?

There are several examples in the tutorial and examples directories that work with data put into a text box but I could find none that worked directly with or triggered by the enter key.

A simple example would be two boxes. After text is entered into box 1 and then the enter key is pushed the data can be modified and then box2<<= modify(~box1);

Seems like this is often used and an example in the tutorial would be of benefit to those learning upp.

In Java it is done like this:
public void propertyChange(PropertyChangeEvent e) {
    Object source = e.getSource();
    if (source == point1Field) {
        point1Action();
	return;
    }
}


edit: The Converter package in tutorial uses:
value <<= THISBACK(ValueChanged);
but that picks up a change for each character and not just when all characters have been typed in and then the enter key pushed.
The characters could be checked for (13,10) OD,OA "\n" but it seems like something easier than this may have been implemented in upp if there are several text boxes. Especially when a string that ends with "\0" or "\n" is pasted into a box and action is not wanted until the enter key is pushed.

[Updated on: Wed, 10 February 2010 04:29]

Report message to a moderator

Re: text box action with enter key [message #25091 is a reply to message #25090] Wed, 10 February 2010 04:31 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
I was unhappy, too. Smile So,
class LaunchField : public EditField
{
public:
	virtual	bool Key(dword key, int count);
	Callback WhenEnter;
	typedef LaunchField CLASSNAME;
	LaunchField() {}

};

bool LaunchField::Key(dword key, int count)
{
	if(key == K_ENTER) {
		WhenEnter();
		return true;
	}
	return EditField::Key(key, count);
}


I hope you will know how to use WhenEnter? If not, ask.

Best regards, Aris
Re: text box action with enter key [message #25092 is a reply to message #25091] Wed, 10 February 2010 05:49 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
Thanks Aris. I am new to C++ GUI and upp.

I am lost on how to use the code you posted.

For a simple app re text only when enter key pushed (K_ENTER ?).
#include <CtrlLib/CtrlLib.h>

using namespace Upp;

GUI_APP_MAIN
{
	TopWindow  app;
	app.SetRect(0, 0, 200, 60);
	EditField  box1, box2;
	app.Add(box1.TopPosZ(0, 20).HSizePos());
	app.Add(box2.TopPosZ(20, 20).HSizePos());
	
	box2<<= ~box1;
//	box2<<= conv(~box1, units, decpl);
	
	app.Run();
}

Having the data typed into box1 go to box2 only when K_ENTER.

My upp Gui works good and converts angles decimal degrees to deg min sec, converts distance meters, km, feet, miles nautical miles, etc.
Calculate the geodesic distance with the Vincenty Formula/s. There are some situations where data typed in like miles it should be acted on by K_ENTER rather than and before a calculation function is called.

[Updated on: Wed, 10 February 2010 05:53]

Report message to a moderator

Re: text box action with enter key [message #25100 is a reply to message #25092] Wed, 10 February 2010 13:07 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
#include <CtrlLib/CtrlLib.h>

using namespace Upp;

class LaunchField : public EditField
{
public:
	virtual	bool Key(dword key, int count);
	Callback WhenEnter;
	
	typedef LaunchField CLASSNAME;
	LaunchField() {}
	~LaunchField() {}	
};

bool LaunchField::Key(dword key, int count)
{
	if(key == K_ENTER) {
		WhenEnter();
		return true;
	}
	return EditField::Key(key, count);
}

//==============================================

class MyApp : public TopWindow {
public:
	LaunchField  box1, box2;

	void OnEnter1();	

	typedef MyApp CLASSNAME;

	MyApp();
	~MyApp() {}
};

MyApp::MyApp()
{
	box1.WhenEnter = THISBACK(OnEnter1);
	
	Add(box1.TopPosZ(0, 20).HSizePos());
	Add(box2.TopPosZ(20, 20).HSizePos());

	SetRect(0, 0, 200, 60);  //if you really need
	
	Zoomable().Sizeable().Title("CallbackField Demo");
}

void MyApp::OnEnter1()
{
	box2<<= ~box1;
	PromptOK("you can launch a rocket from here! but better replace me with your code!");
}


//==============================================

GUI_APP_MAIN
{
	MyApp  app;	
	app.Run();
}




I hope to hear something from you... Smile like ... more questions!

Aris

[Updated on: Wed, 10 February 2010 13:10]

Report message to a moderator

Re: text box action with enter key [message #25107 is a reply to message #25100] Wed, 10 February 2010 20:37 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
Thanks Aris! I will try that tonight, the code looks good.
Re: text box action with enter key [message #25109 is a reply to message #25100] Wed, 10 February 2010 21:17 Go to previous messageGo to next message
unodgs is currently offline  unodgs
Messages: 1366
Registered: November 2005
Location: Poland
Ultimate Contributor

I think WhenEnter should be part of EditField. It's used by too many people Smile
Re: text box action with enter key [message #25111 is a reply to message #25109] Wed, 10 February 2010 22:52 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
Thanks for the code Aris, it works great.

I agree with unodgs. Code that does this should be in the upp lib.

Plus an example in tutorial. Having this explained in the Manual and/or in the Gui Tutorial would be good.
Re: text box action with enter key [message #25114 is a reply to message #25109] Thu, 11 February 2010 08:09 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3356
Registered: August 2008
Senior Veteran
unodgs wrote on Wed, 10 February 2010 21:17

I think WhenEnter should be part of EditField. It's used by too many people Smile

I agree.

In this case and in others some virtual functions could be converted into When...() funcions.


Best regards
IƱaki
Re: text box action with enter key [message #25124 is a reply to message #25114] Thu, 11 February 2010 21:22 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
OK, WhenEnter is in U++ now...
Re: text box action with enter key [message #25125 is a reply to message #25090] Thu, 11 February 2010 21:25 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
nlneilson wrote on Tue, 09 February 2010 20:29


In Java it is done like this:
public void propertyChange(PropertyChangeEvent e) {
    Object source = e.getSource();
    if (source == point1Field) {
        point1Action();
	return;
    }
}




You can do very similiar thing in U++, as key delivery starts at focused widget, but moves up until Key returns true.

So you can do

void MyDialogWindow::Key(dword code, int)
{
    if(point1Field.HasFocus()) {
         point1Action();
         return;
    }
}


Good to know to manage some more complex cases...
Re: text box action with enter key [message #25128 is a reply to message #25125] Thu, 11 February 2010 22:32 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
Thanks Mirek, that was fast.

I have 1952, I will find out how to load the updater module for the /live_update.

I appreciate your example code that is similar to Java.
void MyDialogWindow::Key(dword code, int)
{
    if(point1Field.HasFocus()) {
         point1Action();
         return;
    }
}

Since I have used this in Java this may be the way to port that to U++.

[Updated on: Sat, 13 February 2010 12:30]

Report message to a moderator

Re: text box action with enter key [message #25132 is a reply to message #25128] Fri, 12 February 2010 01:00 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
I had to search and tinker a bit but this works good.

    bool MyApp::Key(dword key, int count){
	if (key == K_RETURN && Point1.HasFocus()) {
	    Point2<<="OK";
//          Point1Action();
            return true;
    	}
    }

Now I can just remove Point2<<="OK"; and tie my "...Action()"/s in directly rather than just depend on clicking menu items.

GREAT HELP!!

Thanks
Re: text box action with enter key [message #25172 is a reply to message #25132] Sat, 13 February 2010 11:59 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Ops, sorry for the quirk in the example. Must have been tired or something...
Re: text box action with enter key [message #25174 is a reply to message #25172] Sat, 13 February 2010 12:32 Go to previous message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
That's OK, sent me in the right direction.

Works Great, thanks for the help.
Previous Topic: Clipped frame
Next Topic: is there any way to embed a browser window in a U++ window or view the html direclty?
Goto Forum:
  


Current Time: Fri Apr 19 13:12:32 CEST 2024

Total time taken to generate the page: 0.03397 seconds