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 » U++ Library support » LineEdit, EditFields, DocEdit » How to trim leading and trailing spaces in EditString?
How to trim leading and trailing spaces in EditString? [message #23765] Fri, 20 November 2009 10:10 Go to next message
Zbych is currently offline  Zbych
Messages: 307
Registered: July 2009
Senior Member
Hi,

What is the simplest way to add a trimming of spaces to EditString?
I tried to add a conversion. It works fine, but EditString stopped reacting to NotNull flag.

Here is the code of my converter:
struct TrimConvert : public Convert {
	virtual Value Scan(const Value& text) const {
		return TrimBoth(text.ToString());
    }
}; 


Question is what I am doing wrong?
Re: How to trim leading and trailing spaces in EditString? [message #23797 is a reply to message #23765] Sun, 22 November 2009 23:28 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11993
Registered: November 2005
Ultimate Member
Zbych wrote on Fri, 20 November 2009 04:10

Hi,

What is the simplest way to add a trimming of spaces to EditString?
I tried to add a conversion. It works fine, but EditString stopped reacting to NotNull flag.

Here is the code of my converter:
struct TrimConvert : public Convert {
	virtual Value Scan(const Value& text) const {
		return TrimBoth(text.ToString());
    }
}; 


Question is what I am doing wrong?



It is because NotNull is method of StringConvert, so if you replace the Convert, it is your responsibility to test for Null.

Note that in such case, using EditString has little merit, you should rather use EditField directly as used Convert is the only difference between both....

Mirek
Re: How to trim leading and trailing spaces in EditString? [message #23848 is a reply to message #23797] Thu, 26 November 2009 10:55 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 307
Registered: July 2009
Senior Member
luzr wrote on Sun, 22 November 2009 23:28

It is because NotNull is method of StringConvert, so if you replace the Convert, it is your responsibility to test for Null.


Ok, I see Smile.

Maybe you could add trimming to EditString and ConvertString?
file EditCtrl.h:

class EditString : public EditValue<WString, ConvertString> {
[...]
	EditString& TrimSpaces(bool ts = true)           { ConvertString::TrimSpaces(ts); return *this; }
};



file Convert.h:

class ConvertString : public Convert {
[...]
	ConvertString& TrimSpaces(bool b = true)       { trim_spaces = b;  return *this; }
[...]
#ifdef flagSO
	ConvertString(int maxlen = INT_MAX, bool notnull = false, bool trim_spaces = false);
	virtual ~ConvertString();
#else
	ConvertString(int maxlen = INT_MAX, bool notnull = false, bool trim_spaces = false)
		: maxlen(maxlen), notnull(notnull), trim_spaces(trim_spaces) {}
#endif
[...]



file Convert.cpp:

Value ConvertString::Scan(const Value& text) const {
	if(IsError(text)) return text;
	if(IsNull(text)) return notnull ? NotNullError() : Value(text);
	if(text.GetType() == STRING_V && String(text).GetLength() <= maxlen ||
	   text.GetType() == WSTRING_V && WString(text).GetLength() <= maxlen) return trim_spaces ? Value(TrimBoth(text)) : text;
	return ErrorValue(UPP::Format(t_("Please enter no more than %d characters."), maxlen));
}



CtrlLib.usc:

ctrl EditString {
	>EditNotNull;
	bool TrimSpaces;
	raw MaxLen;

	PaintData(w) {
		text = (.NotNull ? "!" : "") + "Str";
		if(.MaxLen != "")
			text << " < " << .MaxLen;
		PaintText(w, text);
	}
}


[Updated on: Thu, 26 November 2009 11:11]

Report message to a moderator

Re: How to trim leading and trailing spaces in EditString? [message #23873 is a reply to message #23848] Sun, 29 November 2009 23:36 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11993
Registered: November 2005
Ultimate Member
I will think about it, but it seems to specific to me...
Re: How to trim leading and trailing spaces in EditString? [message #23874 is a reply to message #23873] Sun, 29 November 2009 23:45 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11993
Registered: November 2005
Ultimate Member
OK, added in enchanced form (separate TrimLeft/TrimRight, TrimBoth calls them all.

Mirek
Re: How to trim leading and trailing spaces in EditString? [message #23879 is a reply to message #23874] Mon, 30 November 2009 09:21 Go to previous message
Zbych is currently offline  Zbych
Messages: 307
Registered: July 2009
Senior Member
luzr wrote on Sun, 29 November 2009 23:45

OK, added in enchanced form (separate TrimLeft/TrimRight, TrimBoth calls them all.


Thank you.


Previous Topic: What is the best way to create a mask in a editfield.
Next Topic: Incorrect position of chars in DocEdit ctrl.
Goto Forum:
  


Current Time: Fri Aug 23 07:37:22 CEST 2019

Total time taken to generate the page: 0.00800 seconds