Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Wrap Point In EditBox

Status
Not open for further replies.

gbcpastor

Programmer
Jun 12, 2010
77
0
6
US
I used set memowidth to 60 to try to set wrap point in an editbox. I then set the font to a non-proportional font and set the width of the editbox to 60 characters wide. However, while it wraps at 60 on the third line, it doesn't return 4 from memline() until the 10th character of the 4th line of the editbox.

What am I missing?
 
First, MEMOWIDTH doesn't affect editbox controls. It only affect long strings of text, such as is held in a character or memo field.

Secondly, the width of an editbox is measured in pixels (or sometimes in foxels, but you can ignore that). It has no direct relationship to the number of characters in the line.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks for getting back to me Mike. Since that is the case, I need to know how to make a line wrap happen in an editbox when the length of the line reaches 60, any ideas?
 
Assuming you mean 60 characters (rather than 60 pixels), the only reliable way to do that is by using a fixed-width font (as I think you already know).

Two approaches come to mind. The first is by trail and error. Create a text string of 60 characters (it doesn't matter what the string contains). Paste it into the Value property of your text box. Then resize the edittbox so that the 60 characters all appear on the same line - no more, no less. Then look at the value of the Width property of the edittbox (you can do that in the Debugger). That will give you the value you need.

The other way is to programmatically measure the width of the line in pixels. You can do that by using TXTWIDTH() and FONTMETRIC(). Here is a function that will do that for you: Find the height and width of a text string in pixels.

In fact, the second method will also work with proportional fonts, but in that case the result will be approximate, and will depend on the actual characters that you pass to the function.

Also, when you talk about the text "wrapping", I assume you are using the usual meaning of that word, that is, the line breaking in such a way that every word stays together on a given line - no breaks in mid-word.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike, I'm using a non-proportional font now "Letter Gothic" and visually it is wrapping the text at 60 characters. But, when I inquire the line number of the editbox, it returns 3 for the first 10 characters of line 4 then returns 4 after that. I need it to report line 4 as soon as it wraps at 61 characters on line 3.
 
Just to further clarify, MLINE() returns a specific line of text, such as the third line (based on your MEMOWIDTH).

MEMLINES() return the total number of lines in the text (again, based on your MEMOWIDTH). Neither of those functions has any direct connection with an editbox, and are independent of the font.

There is also ALINES(), which returns an array, in which each array row contains the contents of one line of the text.

Does that help at all?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mr. GbcPastor,
If you use fixed font, and you want to know what is on line 4, which is the 4thline of 60 characters, I believer substr(myField,3*60) is to be used.
Regards,
Koen
 
I believer substr(myField,3*60) is to be used

Koen, your are right, except that you would need SUBSTR(MyField, 3*60, 60), otherwise you would get the fourth line and everything after that.

The problem with that approach is that it doesn't know about wordwrap. In other words, it can return a line of text in which the last word is broken in the middle.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
My point exactly. Since I'm using a "fix font" and I have the editbox set to 60 characters wide and the memowidth is set to 60, then when I reach the 61st character on the third line, it should wrap to the forth line which it does but MEMLINE() reports it's still on line three until another 10 characters are keyed in.

Example:
1234567890 1234567890 1234567890 1234567890 1234567890 12345
67890 1234

The above says it's all on line 3 and the last ten characters should be line 4.
 
MEMLINE() reports it's still on line three until another 10 characters are keyed in

(Emphasis added.)

Ah, that's the answer. You are testing MEMLINE() while the use is still editing the text. It can't know the final contents of the line until the user has hit the spacebar after the tenth character. That's why you are getting the behaviour you are seeing.

Perhaps if you tell us exactly what you overall goal is, we might be able to suggest a solution.

Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Easy enough you would think Mike. I have a small form containing nothing but an edit box 60 characters wide and 4 lines deep. There is an OK button on the bottom. The editbox is to allow no more than a total of 240 characters and no more than 4 lines. The control is linked to a character field called notes2 c(240). That's it.
 
For a start, you could set the EditBox's MaxLength to 240. That would solve the first part of the problem.

As for the check on the number of lines, I would do that after the editing is complete - perhaps in the EditBox's LostFocus. At that point, MEMLINES() should return the correct value.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I would do that after the editing is complete - perhaps in the EditBox's LostFocus.

On second thoughts, I can see why you wouldn't want to do that. You want to prevent the text going over four lines, not merely advise the user after the event.

It's getting late in the evening here now. I'm going to clock off. I'll be back in the morning.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
MEMLINES() expects the name of a memo field, how do you use that if your field is C(240)? If you copy over the editbox value into a memo field, you can apply it, but it'll return the number of lines the whole string has, not a "current" line number.

SET MEMOWIDTH help topic says it respects the font setting of _screen, quote:
VFP help said:
If output is directed to the main Visual FoxPro window, the width of the output is determined by the font for the main Visual FoxPro window. If output is directed to a user-defined window, the width of the output is determined by the font for the user-defined window.

I'm not sure what to make of that, as MEMOWIDTH still is about the number of characters.


I made a small experiment. This is what I get:
memlines_l0y0xg.jpg


The Editbox is set up to allow exactly 60 characters of monospaces Courier New text in that font size. I copied that Editbox content to a memo field os a cursor, SET MEMOWIDTH TO 60 (before even creating the cursor).

You see I didn't change the _Screen font to Courier New, too. The output directly below the Editbox is from simply printing that memo field. The output after '---' is done with MLINE(memo,1) to MLINE(memo,5). I think the output of the memo itself with ? memo wraps differently, because of that. MLINE() seems to work 1:1 and by the way MEMLINES() returns 5, too.

From that experiment, MEMLINES seems ok, I'd perhaps rely more on MLINE reproducing the lines. Both also seem to work when you introduce manual line breaks with ENTER. Anyway, more than 4 lines fit in char(240) with manual line breaks. If you want to limit this to 4 lines, then checking MEMLINES seems ok, once you go for a monospaced font.

Bye, Olaf.

Olaf Doschke Software Engineering
 
If you get different results, please check, if your editbox really gets 60 characters into one line, the scrollbar counts to the editbox width, which could break it.

Bye, Olaf.

Olaf Doschke Software Engineering
 
I was sure between you and Mike I would find a solution. But I guess I will have to come up with another approach. I've done all you did here and none of that is a solution.

I think I'll go at it doing a character count and count each Chr(13) as length of the line padded to 60. I'll see if that works. I have the scrollbars turned off so they don't affect what is allowed in each line.

Let me know if you guys come up with something that works, and I'll do the same.
 
Well, MEMLINES(memofield) works for me, or checking whether LEN(MEMLINE(memofield,5))>0.
But the editbox content must be saved to a memo field. Even if just for checking.

Bye, Olaf.

Olaf Doschke Software Engineering
 
OK, checked once more, you don't need a memo field. Anyway, does REPLICATE("W",60) fit into one line? I needed to set the EditBox width to 60*width by font metric plus scrollbar width (you don't need that) plus 12 or so extra pixels. So even a plain Editbox without scrollbars has pixels for border and some extra padding pixels as a margin of the inner text to the EditBox borders.

If that is correct you get word wrap at the exact points when MEMLINES() also computes more lines.

Just to state the obvious: MEMLINES is all about counting characters according to SET MEMOWIDTH, also recognizes carriage return. The Editbox wrapping is all about wrapping for pixel width, independent of MEMOWIDTH.

You don't have to set the Editbox to exactly the correct width, as soon as a 61st character wraps over this will wrap the same way as MEMLINES will count, but as I have shown in a screenshot: Doing ? to print the Editbox value can have more lines than the Editbox shows.

More things to realize: InteractiveChange would be a good moment to check MEMLINES. In that event you better check MEMLINES of the Editbox.Value than MEMLINES(field), because the field only updates when the Editbox valid allows writing back to the ConotrolSource, ie when you change focus to another control.

Then your non-proportional font might not really be non-proportional. Have you simply tried with "Courier New" just to ensure it's not the font?

Bye, Olaf.

Olaf Doschke Software Engineering
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top