如何计算C#2.0中多行文本框中的行数 [英] How to count the number of lines in multiline text box in C# 2.0

查看:120
本文介绍了如何计算C#2.0中多行文本框中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用多行文本框来存储输入。



它的大小因系统而异。



所以尺寸不固定。



由于如果一个人输入文字并且线条超过3行,尺寸不固定我需要

显示一个消息框,显示超出3行。



怎么做。



谢谢advance

I am using a multiline textbox to store input.

Its size varies according to different systems.

So size in not fixed.

Since size is not fixed if a person inputs a text and the lines exceeds 3 lines I need to
display a messagebox showing 3 lines exceeded.

How to do this.

Thanks in advance

推荐答案

问题是当你使用多行文本框时,lines是一个模糊的概念:因为文本框所谓的行是分隔的通过用户使用ENTER键输入的'\ n'字符 - 但是就显示而言,它包括自动换行,因此在运行的应用程序中,一行文本可能会显示十几个行。 />


如果你想将用户输入限制为多个行,那么你需要使用Graphics.MeasureString [ ^ ]方法结合输入的文本和TextBox.Font属性来确定显示实际文本的大小。



这听起来不会那么简单:你需要使用用户输入的行字符,以及文本框的大小,并记住每次都要处理你创建的图形上下文,否则你的内存会很快耗尽!
The problem is that "lines" is a nebulous concept when you use a multiline textbox: because what the text box thinks of as a "line" is delimited by a '\n' character entered by the user using the ENTER key - but as far as the display goes it includes word wrap, so a single line of text may display over a dozen "lines" in the running application.

If you want to restrict the user input to a number of "lines" regardless, then you will need to use the Graphics.MeasureString[^] method in conjunction with the text entered and the TextBox.Font property to determine the display size of the actual text.

This will not be as simple as it sounds: you will need to work with the line characters the user has entered, as well as the size of the textbox, and remember to Dispose the graphics context you create each time or you will run out of memory extremely quickly!


快速点击谷歌搜索,我遇到了这个问题这似乎与你的要求类似:



http://stackoverflow.com/questions/1048425/limit-number-of-lines-in-net-textbox [ ^ ]



供您参考:



这是我用Google搜索的windows窗体多行文本框限制行数,因为您还没有说明什么类型的应用程序这是我假设它是一个Windows窗体应用程序。
With a quick bit off googling, I have come accross this question which seems to be similar to what you are asking:

http://stackoverflow.com/questions/1048425/limit-number-of-lines-in-net-textbox[^]

For your reference:

This is what I googled "windows forms multiline textbox limit number of lines" as you have not stated what type of application this is I have assumed it is a Windows Form application.


这篇关于如何计算C#2.0中多行文本框中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆