讲述如何开发一个控件,很有价值(四)
2008-04-09 04:31:05来源:互联网 阅读 ()
To start with I used the Edit1 Control to display the results of all these variables. I then tried manipulating text in the RichEdit to see what values I got. You should do the same. Type slowly in:
1234567890<CR>1234567890<CR>1234567890
and see how the results are reflected in the Edit control as you do so. Then experiment - try adding stuff to the ends of lines, and in the beginning of the line, and middle of lines. You may have to refer back to the Code to work out which number represents which variable.
Okay, now using the variables we have, lets try selecting the text of the current line, and display it in a new Edit Control (Edit2).
Add the following code to see what happens (don’t forget to add the second edit control and make it as wide as possible):
MyRe.SelStart := BeginSelStart;
MyRe.SelLength := EndSelStart - BeginSelStart;
Edit2.Text := MyRe.SelText;
end;
Run the program and try it out.
OOPS - That doesn''''t work - the text remains selected and the original cursor position is lost.
We need to reset SelStart and SelLength before we finish in the [OnChange] event. So let’s add at the end:
MyRe.SelStart := WasSelStart; //back to were we started
MyRe.SelLength := 0; // nothing selected
end;
While playing with text in the edit control I discovered something weird.
If you typed [1] then <CR> then [2] the Edit1 displayed [4-1-3-4].
But there were only two characters in the display.
I made a mistake. It appears that RichEdit.Text can tell you where the beginning and end of line is. Why? Because you can access the <CR><LF> characters in the Text string. So we could have manipulated the Text property of the control to work out the beginning and end of lines by reading back and forward from SelStart to find <CR><LF> characters. We may not have known which line we were on, but we would know where it began and ended. Nevertheless we should keep this in mind, it might come in handy later.
But it doesn''''t matter - the EM_###### messages are a neat way of doing things. And they work. For the moment at least we''''ll stick with them.
7. Okay implement: Part 2 - Change the format
After the line Edit2.Text := MyRe.SelText, but before the "resetting" part, lets put some logic in to turn lines RED when they are longer than a certain length:
if (MyRe.SelLength > 10) then MyRe.SelAttributes.Color := clRed;
You''''ll notice two things if you test this out. First - it does work. Second however, is that if you type a line > 10 characters, press return and type one character - its in Red. This is because it inherits the Attributes of the preceding text. Just like if you have bold on in a Word processor, it doesn''''t reset if you press return. So lets change the line to include an else situation:
else MyRe.SelAttributes.Color := clBlack;
That seems to work - except when you press return in the middle of a > 10 character line you have already typed (which is already Red) to leave a stump < 10 characters on the line above - it remains red. This is because the code leaves you on the next line, and SelStart refers to this new line, not the previous one. In our eventual code, we''''ll have to take care to ensure this doesn''''t happen - we have to catch this situation and deal with it. It wont be the only situation I''''m sure....
PS: There will be a number of situation we''''re we''''ll have to be careful. Can you think of any now? Try putting a lot of text in the Control (or manipulate a loaded file) and selecting some and using the inherit Drag and Drop (move your Mouse over some selected text, press and hold down the Left MouseButton and then drag away) to move some text. This only Triggers one OnChange Event. We may also be moving multiple lines along the way. In the future we''''ll have to put in some code to detect this happening, and ensure the [OnChange] event can deal with the need to reformat in two different locations. That means thinking in the back of the head about how in the future we may have to deal with this kind of situation, and ensure our code to deal with the simple situation can be adapted - i.e. be "versatile".
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 讲述如何开发一个控件,很有价值(七) 2008-04-09
- 讲述如何开发一个控件,很有价值(六) 2008-04-09
- 讲述如何开发一个控件,很有价值(三) 2008-04-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash