Question Issue with writing to a textBox

chueseman

New member
Joined
Feb 23, 2021
Messages
2
Programming Experience
3-5
In my Visual Studio 2019 Solution, I am sending a result or error message to textBox1. See examples below. It succcessfully goes there, but it is highlighted. When I click on the contents of textBox1, the highlight goes away. Is there any way to turn off the highlighting in the first place?
C#:
textBox1.Text += thisPerm;
textBox1.text += violation1message;
 
Last edited by a moderator:
Yes, in general, you can turn off all of Visual Studio's helpful hints and syntax coloring, but then you might as well just use Notepad as your text editor. It would be better to try to figure out exactly what VS is trying to tell you or warn you about your code.

Is it a highlight or a red squiggly?

If it's a red squiggly, what does it say in the "Errors/Warnings" pane of Visual Studio? Or if you hover over the text that is marked with the squiggly -- hover, not click -- is there a tooltip message that shows up to indicate what VS thinks is wrong?

As an aside, in the second line of code you have above, .text should be .Text. C# is a case sensitive language.
 
Firstly, please post your questions in the most appropriate forum. I'm assuming that this is a WinForms app so I have moved the thread to the Windows Forms forum. Please specify if it is some other UI technology.

Secondly, don't use += with the Text property of a TextBox. If you want to append text then call the AppendText method. Coincidentally, that will fix the issue you're asking about as well, because AppendText leaves the caret at the end of the text. By doing what you are, you are getting the current value of the Text property, concatenating that with another string and then replacing the current value with the result. That code should be:
C#:
textBox1.AppendText(thisPerm + violation1message);
The multiple parts are combined first and then the result appended to the current contents of the control.

Finally, is a TextBox really the most appropriate control for an error message? A TextBox is generally for input as well as output. Would a Label not be more appropriate? If you are using a TextBox, I hope that you have, at least, set ReadOnly to true. The one advantage of that is that the user can copy the text in order to paste elsewhere.
 
Back
Top Bottom