3

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.

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.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Matt Winer
  • 495
  • 9
  • 26
  • 1
    Should be no problem, if you're using WPF. – Quasdunk Aug 26 '11 at 06:42
  • 1
    Are you using WinForms or WPF? WPF will make this a lot easier I think. – RB. Aug 26 '11 at 06:43
  • possible duplicate http://stackoverflow.com/q/1861166/754438 – Renatas M. Aug 26 '11 at 06:43
  • check this question out: http://stackoverflow.com/questions/447089/embed-image-into-a-textbox – Waqas Aug 26 '11 at 06:44
  • Waqas: That's unrelated a bit, I think, as they look for a way of having interactivity too, not just an image. – Joey Aug 26 '11 at 06:46
  • I'm using WinForms. WPF wasn't around when this program was originally written. Not much of a need to re-write. But you're right. Doing it in WPF is super easy. Just trying to see if I missed something and maybe I can do this without too much hassle. I'll look into those other posts. – Matt Winer Aug 26 '11 at 07:18
  • There is a solution here, I believe it helps http://stackoverflow.com/q/15868817/3737790 – Osman Pirci May 16 '16 at 06:58

3 Answers3

3

Who says that google put an X in right end of text box....??

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.

here is the html of google's search textbox:

<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(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) 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>
talha2k
  • 24,937
  • 4
  • 62
  • 81
  • 4
    @MADMap, I believe this answer is still helpful (aside from the HTML snippet), because the idea explained here can be easily transferred to Winforms or WPF. – stakx - no longer contributing Aug 26 '11 at 07:03
  • 5
    You really did not have to paste a looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line of HTML to prove your point. – BoltClock Aug 26 '11 at 07:07
  • 1
    Maybe the OP is referring to the Google search box in Safari on iOS (probably in a load of other places too). -1 – PaulB Aug 26 '11 at 07:28
0

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.

CodingBarfield
  • 3,392
  • 2
  • 27
  • 54
0

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