如何限制winform PropertyGrid中输入字符串的长度? [英] How limit the length of the input string in winform PropertyGrid?

查看:242
本文介绍了如何限制winform PropertyGrid中输入字符串的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制winform PropertyGrid中输入字符串的长度?如果输入字符串的长度超出固定长度,则超出部分不显示!谢谢你的帮助!

How limit the length of the input string in winform PropertyGrid? If length of the input string eyond the fixed length,beyond the part is not displayed! Thank you for your help!

推荐答案

这是一个很大而又困难的话题,但由此产生的实现可以相对简单;它只是很难解释。



请参阅我对这个问题的评论。正确的想法是:你可以简单地剪掉太长的行,或抛出异常建议用户选择更短的字符串。当然,所有异常都应该在主UI事件循环中处理,因此用户将能够看到错误消息并继续使用UI,更正输入。这种异常处理是必须的;我在过去的答案中对此进行了描述:

捕获异常 [ ^ ],

错误记录和屏幕截图。 [< a href =http://www.codeproject.com/Answers/237363/Error-Logging-and-Screen-Shot#answer2target =_ blanktitle =New Window> ^ ],

何时我运行一个应用程序一个例外是如何处理这个? [ ^ ],

保持ac#windows表单应用程序运行,尽管有任何未处理的异常 [ ^ ],

如何在滚动条到达底部时停止循环 [ ^ ],

处理类库中的异常(dll) [ ^ ],

异常详细信息:System.Runtime.InteropServices.COMException:由于以下错误,检索具有CLSID {0006F03A-0000-0000-C000-000000000046}的组件的COM类工厂失败:... [ ^ ]。



对不起,我的建议有很多重复,但更好的阅读全部。



但是现在,如何修改 PropertyGrid 的行为?这种特殊情况相对简单:您可以编写和应用自定义用户界面类型编辑器。你可以从这里开始:

https://msdn.microsoft.com/en -us / library / 37899azc.aspx [ ^ ],

https://msdn.microsoft.com /en-us/library/ms171838.aspx [ ^ ]。



我的想法是:为您的类型设计,实现和应用一些自定义编辑器并应用 EditorAttribute 您的类型的编辑器类型应该在 PropertyGrid 下编辑:

https://msdn.microsoft.com/en-us/library/ms171840.aspx [ ^ ]。



基本上,你在方法 EditValue 的实现中达到了预期的效果(即时剪切字符串或抛出异常)。



-SA
This is a big and difficult topic, but resulting implementation can relatively simple; it's only hard to explain.

Please see my comment to the question. The right idea is: you can simply cut too long line, or throw the exception suggesting to the user to choose shorter string. Of course, all exceptions should be handled in the main UI event loop, so the user will be able to see the error message and continue working with the UI, correcting the input. Such exception handling is a must; I described it in my past answers:
Catching an Exception[^],
Error Logging and Screen Shot.[^],
When i run an application an exception is caught how to handle this?[^],
Keep a c# windows form application running despite any unhandled exception[^],
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
Handling exceptions in class library (dll)[^],
Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error:...[^].

Sorry, there are a lot of repetitions in my advice on exception, but better read it all.

But now, how to modify the behavior of the PropertyGrid? This particular case is relatively easy: you can write and apply custom User Interface Type Editor. You can start here:
https://msdn.microsoft.com/en-us/library/37899azc.aspx[^],
https://msdn.microsoft.com/en-us/library/ms171838.aspx[^].

The idea is: you design, implement and apply some custom editor for your type and apply EditorAttribute with your editor type to your type which should be edited under the PropertyGrid:
https://msdn.microsoft.com/en-us/library/ms171840.aspx[^].

Essentially, you achieve desired effect (cutting the string on the fly or throwing exception) in your implementation of the method EditValue.

—SA


这篇关于如何限制winform PropertyGrid中输入字符串的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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