What happen actually when TextBox got selected ? or got focused..? - c#

I am trying to code for "Hint Text in Text Box" in C#. It works this way:
Initially show "Hint Text" with inactive caption color
Blank the text box if all the hint text is selected
Shows the user input text (after typing in this)
If user deletes his text and goes to the next control, then it shows the hint text again.
I need a help on 2nd point: It's not clearing the text box if its content is not selected. It receives the input text mixed with "hint text".
On which event I need to write the code txtBox1.Clear(); to achieve my aim?

Assuming this is WinForms: Look at the Enter and Leave events. They will be called when a control gets or loses focus. You then need to decide whether the value in the text box is your default hint or entered by the user.
For WPF: There are nice solutions out there that use adorners to achieve what you want. You could google for WPF textbox watermark adorner.

Related

Can you make a text box on a C# form act like an excel box?

I want the user to be able to type like = or whatever and then write an equation. Once they click off the box, I want the textbox to show the answer.
For example they could put =2+2 and it would display 4 when not selected.
I have no idea how to do this at all or even where to begin. I can get the textbox to calculate it somewhere else but not in the box itself where the equation is written or without having the user press a button.
What you're probably looking for is the LostFocus event. This event fires whenever a control loses focus (for example when a user tabs out of a text box). If you listen on this event for your text box, you can then call whatever processing code you want to handle any calculations required by the value of the text box and then set its value to the result of the calculation.

C# Check box, only check the box not the text

Okay i swear i looked for this on google before posting.
I tried:
C# check box active area
C# check box check box not text
and a few others.
But i have a check box on top of a button. I want to be able to click the button even when the mouse is over top the check box text area. I want the use to only be able to click the actual box. But when the mouse is over the check box's text, it should click the button.
Thanks all!
I'm assuming this is in WPF / XAML.
Simply add the checkbox to the button without text. Add the text to the button not the checkbox.
That said, I cannot for the world think why this would be a pleasant user experience and would suggest trying to use a different means of getting the information.
So instead of having the checkboxes over top the button, i just created a seperate window that will show with the three options to choose from upon clicking that button.
But i still think it would be cool/neat to have them on top of the button.

How To Highlight Text in a textBox While Typing in another textBox?

I'm making a typing windows program that has two text Boxes,
the first is the Source Text textBox and it is read only,
the other is where the user get to type in the text that is in the source TextBox.
When the user typing a letter in the TypingTextBox, I want that letter to be highlighted in the SourceTextBox..
I tried doing this in a couple of events, but none really worked:
SourceTextBox.Select(TypingTextBox.SelectionStart , 1);
I even Tried Making my own event, Also didn't work.
The thing is, I won't see the SourceTextBox highlighting unless I click on it.
and As I mentioned, I tried putting the above code in events like:
Mouse-Focus-Leave in the SourceTextBox
and: TextChanged in the TypingTextBox.
All didn't work .. :(
and If I manged to do that, Can I change the Highlight color ?
Assuming this is WinForm, you need to set the HideSelection property on the TextBox to "False". As far as changing the highlight colour, none that I'm aware of.

C# winforms: move text from one textbox to another

I have a winform application with two textboxes. The textboxes are multilined and has 5 rows.
When the user enters more than 5 lines of text in the first textbox I want the text to continue in the second textbox. And if he/she deletes text from the first textbox I want the text to move back from the second to the first one...
I have tried to solve this in my code by checking how many rows the first textbox has and moved text between the two textboxes. But it doesnt work that well so I wonder if anyone got a better solution??
You could accomplish this by registering for the TextChanged events on the TextBox controls. Then in the event handler, manually inspect the Text property and set focus to the appropriate control. However, what you are describing sounds like it may lead to an inconsistent user experience.
From a UX standpoint I would suggest changing the approach. First of all do you really need to split the text in the UI, or could it be split afterward in the business layer? If you do need it split in the UI, you could have a single TextBox which allows the user to enter the full text, and below it have 2 read-only textbox's which display the 2 split segments as they type (you would also use the TextChanged event logic to do this as they type).
I hope this helps.
Have you tried checking the visible Characters in the text box? or text box character length?

ASP.net textbox scrolling when disabled

Greetings,
I have a form where employees enter comments in a multiline textbox with a limit of 4000 characters. I have the rows set to 8 (obviously an arbitrary number).
When a supervisor looks at the comments the textbox is disabled so the employee comments cannot be modified.
The problem is when the data extends below row 8. Since the textbox is disabled the scrollbar cannot be moved and the supervisor cannot see all the comments. If I hide the textbox and databind to a label for the supervisor none of the line breaks are maintained and a well written paragraph turns into the biggest run on sentence ever…
Is there a way to enable the scroll bar leaving the text disabled?
Is there a way to preserve the structure of the entry in the label?
Instead of disabling the textbox you should set the ReadOnly property to True. This keeps the scrollbars functional but doesn't allow modification of the textbox.
txtComments.ReadOnly = true;
In supervisor mode, don't put the text into a textbox, put it in a label as you mentioned, with '.Replace("\n", "<br>")' in your code.
Alternatively, show the textbox without disabling it, and just disable the 'save' button. Put a note on the page saying that, "changes made here are not persistent" or something to that effect.
Make the text box read only, and set the fore color to the same shade of grey used in disabled text boxes.
<asp:TextBox ForeColor="#AFAFAF" ReadOnly="true" />
Put the text in a PRE tag, and apply overflow:scroll to it.
Make sure to escape the text using Server.HtmlEncode first.

Categories

Resources