windows 如何在文本框中放置“X”(清除文本框)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7200914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put an "X" (to clear a text box) inside the text box?
提问by Matt Winer
In my Windows application I would like to put an X
at the right edge of a text box to clear it. Similar to what Google is using right now. Check out Google and do a search for anything. You'll see the X
in the text box as soon as you've started typing something.
在我的 Windows 应用程序中,我想X
在文本框的右边缘放置一个以清除它。类似于谷歌现在正在使用的。查看谷歌并搜索任何内容。X
一旦您开始输入内容,您就会在文本框中看到。
I know I can get away with just putting a button next to the text box. But I would like to do something a bit nicer.
我知道只要在文本框旁边放一个按钮就可以了。但我想做一些更好的事情。
回答by AlphaMale
Who says that google put an X in right end of text box....??
谁说谷歌在文本框的右端放了一个 X....??
Thats not inside the google search text box. its outside of that textbox. and thats the onlyway you can do it. google use a table in which one td contains textbox while other contains 'X'. They've put some css which makes you feel like the X is inside textbox. Whixh is really not.
那不在谷歌搜索文本框中。它在那个文本框之外。这就是你能做到的唯一方法。谷歌使用一个表格,其中一个 td 包含文本框,而另一个包含“X”。他们添加了一些 css,让您感觉 X 位于文本框内。Whixh 真的不是。
here is the html of google's search textbox:
这是谷歌搜索文本框的html:
<table cellspacing="0" cellpadding="0" style="width: 100%;"><tbody><tr><td class="gsib_a"><div style="position: relative; width: 100%; height: 25px;"><input type="text" maxlength="2048" name="q" id="lst-ib" class="gsfi" autocomplete="off" size="41" title="Search" value="mysql_num_rows" style="border: medium none; padding: 0pt 0pt 0pt 2px; margin: 0pt; width: 100%; outline: medium none; top: 1px; background: url("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D") repeat scroll 0% 0% transparent; overflow: hidden; position: absolute; z-index: 5; color: transparent;" spellcheck="false" dir="ltr"><div class="gsfi" style="top: 1px; padding: 0pt; white-space: nowrap; position: absolute; width: 100%; color: silver; z-index: 0; text-align: left; left: 3px;" id="gs_tad0" dir="ltr"></div><div class="gsfi" style="position: absolute; top: 1px; background: none repeat scroll 0% 0% transparent; color: rgb(0, 0, 0); padding: 0pt; white-space: nowrap; z-index: 2; left: 3px;" dir="ltr">aol</div><div style="position: absolute; top: 1px; width: 1px; height: 20px; background-color: rgb(0, 0, 0); padding: 0pt; z-index: 4; left: 14px; visibility: hidden;"></div><div class="gsfi" style="position: absolute; top: 1px; background: none repeat scroll 0% 0% rgb(255, 255, 255); color: rgb(255, 255, 255); padding: 0pt; white-space: nowrap; z-index: 1;"></div></div></td><td><table cellspacing="0" cellpadding="0" style="width: 100%;" dir="ltr"><tbody><tr><td class="gsib_e"><div></div></td><td class="gsib_b" style="color: rgb(161, 185, 237);">×</td><td class="gsib_f"><div></div></td><td class="gsib_d"><div></div></td></tr></tbody></table></td></tr></tbody></table>
回答by CodingBarfield
You could use custom painting to paint a little red icon on the rights side of you're control. Add some mouse click code to detect mouse clicks inside the little red icon to activate 'any' custom code.
您可以使用自定义绘画在您控制的右侧绘制一个红色小图标。添加一些鼠标点击代码来检测红色小图标内的鼠标点击,以激活“任何”自定义代码。
回答by Nour Enbabi
um sorry dude but in my opinion u can only do this by adding a button ( even inside the textbox) and set it as when it is clicked it clears the textbox
嗯,对不起,伙计,但在我看来,你只能通过添加一个按钮(甚至在文本框内)来做到这一点,并将其设置为点击时它会清除文本框
textbox.Clear();
Good Luck to You , Wish u the best
祝你好运,祝你一切顺利