I want to add a line in a multiline textbox after every information that the user enters in the TextBox in Windows form. I wont be able to determine the point to draw line as I wont know how long the user will enter information. So I am thinking of adding a string "_______" after a button click in the code. Is there a better way?
If the user is going to be required to click a button to confirm a single entry then I don't think you should continue to store the information in the same text box & provide a visual separation with a bunch of dashes, rather move the text entered from the input to a more formatted display (set of controls). Then the display will be separated from input. You could them allow the user to select individual input in the display to modify entry providing a nice user experience.
If you intend to use a regular multiline textbox, printing a line of '-' characters is your best bet. Of course, you could print a line of '─' characters to make the line solid. Another alternative is to use a WebBrowser control, and set its DocumentText property to the HTML you would like to display. This will allow you to append <hr/> whenever you want a dividing line.
You might consider writing a custom control.
You could either paint the control yourself, or use an autosized label and the control's scollbars.
Related
I am using Visual Studio 2019 in order to create a Windows Form Application. I need some titles in my application, which means these strings will not be modified by the user.
For now, I created textboxes for these titles and made these textboxes "read-only". However, this does not satisfy my aesthetical expectations.
Therefore, I wonder if there is a way to add a string without adding a textbox, to the form. Is there a way?
Thanks in advance :)
Consider using a Label control rather than a TextBox.
The only time I would use a TextBox as a label is if I want the user to be able to copy the info, and I make it borderless, readonly and have the same colour as the background of the form. It's not superb UX though as there isn't anything that screams "you can highlight and copy this text" other than an I beam cursor, which is pretty much "mystery meat navigation" - better off putting a copy button next to it if you expect the user to copy info often
Why not use Label for your titles?
Since label, by default, cannot be modified by the user, thats what you want. Textbox is used for the user input, not for the titles.
Use Label control that is the right control to use for your requirement
I am currently implementing a form that allows the user to input a lengthy message. For this application, the user's text must have some preset text added to the end.
An example would be:
Hello, I'm a message. END
How would I add the 'END' text in a text editor and enforce it to always remain at the end of the text? I want to show it there at all times and disallow the user from deleting or editing it. Do you know if this is possible?
My initial thoughts would be to on the TextChanged event, check if the characters are already on the end, and if they are not, delete them. But simple logic like that just seems bound to cause problems, and I was wondering if there is a known method of doing something like this, as unusual as it may seem.
You can add a handler for the KeyPress event, and if the key represents an edit that you don't want applied set the value of KeyPressEventArgs.Handled to true before returning.
If you need predefined text at the end of text entered by user then add that text at the end of message in code behind. Use text which are very rare to be entered by the user. Like ##END##. So you knows ##END## is the end of the message. When showing that message back to editor substring message till the index of ##END## in your code and display only actual message to the user.
I want a textbox that it is possible to add "text blocks" to it. The definition of a "text block" is:
Deleting a char in the textbox that is part of a block causing to the whole block to be deleted.
Setting the carot position in the textbox in a location that is part of a block causing to the whole block to be selected.
Any attempt of the user to write a char in a middle of a block will fails followed by an appropriate message box.
I have a DataGridView with a CellClick event attached to it. I want that when the CellClick event is occurs, the content of the cell will be placed in a textbox and will be considered as a text block. The location of the added block will be in the carot position of the textbox or at the end of the textbox if the textbox is not focused and thus the carot is not blinking.
I also want that the textbox could function as a normal textbox, meaning that the user can add or delete any chars/text he want excepts the text blocks that they must be added/deleted completely as defined above.
Maybe I need to use some other GUI controls except textbox to accomplish this mission, I don't know.
I thought about some solutions but I don't know which is the best (maybe none of them):
(remark: each of the following solutions assumes that there is an KeyPress event attached)
Adding hidden characters (if possible) before and after each block to mark it.
Creating a list of objects which represents all the text blocks, each object containing two fiels: startIndex, endIndex. Not so good because adding or deleting chars from the textbox requires to update all the indexes of the blocks located after the added/deleted chars by +1 or -1 for each char.
Creating a list of objects which represents all the text in the textbox, each object containing two fields: text, flag. Each time a char is added manually or a word is added by the event, an object is added to the list and the object's text field is set to the added chars, and the object's flag filed is set to true if the chars are a word added by the event, or false otherwise.
Splitting the textbox to 2 parts and creating a small textbox between them for each text block added by the event, and treating the small textbox diffetently. When the text in the small textbox is deleted the whole textbox control is deleted and the splited textbox is united. Doesn't sounds me so good.
What is the best way to implement this?
Thanks!
I have never seen a control with this behavior. I'm not saying that someone hasn't written one, just that it's unlikely. This means that you're going to have to build it.
If you've never created a custom control, search the web for "creating custom winforms controls" or similar. You first need to understand the basic techniques.
Then you're going to want to know how to store the underlying text for editing. You can start by learning some of the techniques commonly used for standard text-editing controls. Pick one that you can modify for your custom scenario. Here are a few off the top of my head:
Gap Buffer
Rope
Piece Chains
Good luck!
In my WPF application I am having some following type of textbox
So, In this textbox, the text is not written by user it is generated by program, i wish to Highlight the particular text "Enter String Value" and when user clicks on that particular highlighted text only then I wish to fire a event.
Can u guys give me some hint or overview to achieve this. I tried few tricks but can't get success.
Thanks in advance.
Use <RichTextBox> with <FlowDocument>. As I know, TextBox allow you to determinate font (color, size, etc.) only one time, and you wouldn't able to modify color of partial text in one TextBox
I think you can use Regex to get your match from your textbox, and then create Run objects using that match. You can set the Background of the match (runobject.Background = Brushes.Red)
What code should I write to prevent any special characters except '_' (underscore) while entering the name in text box?
If such character exist then a popup message should appear.
Rather than me writing the code for you, here are the basic steps required for accomplishing such a feat:
Handle the KeyDown event for your TextBox control.
Use something like the Char.IsSymbol method to verify whether or not the character that they typed is allowed. Make sure you check explicitly for the underscore, because you want to allow it as a special case of other symbols.
If a valid character is typed, do nothing. WinForms will take care of inserting it into the textbox.
However, if an invalid character is typed, you need to show the user a message, informing them that the character is not accepted by the textbox. A couple of things to do here:
Set the e.SuppressKeyPress property to True. This will prevent the character from appearing in the textbox.
Display a tooltip window on the textbox, indicating that the character the user typed is not accepted by the textbox and informing them what characters are considered valid input.
The easiest way to do this is using the ToolTip class. Add this control to your form at design time, and display it when appropriate using one of the overloads of the Show method.
In particular, you'll want to use one of the overloads that allows you to specify an IWin32Window to associate the tooltip with (this is your textbox control).
Alternatively, instead of a tooltip, you can display a little error icon next to the textbox control, informing the user that their last input was invalid. This is easy to implement using an ErrorProvider control. Add it to your form at design time, just like the tooltip control, and call the SetError method at run-time to display an error message.
Whatever you do, do not display a message box! That disrupts the user trying to type, and it's likely that they'll inadvertently dismiss it by typing the next letter they wanted to type.
Add a handler to the TextBox's KeyDown event. You can examine which key was pressed there, and do whatever you want with it, including popping up a message box.