Highlighted Text

T

Tim Wilson

What about setting the TextBox's SelectionLength property to zero? Does that
help?

--
Tim Wilson
..Net Compact Framework MVP

Marv Scheinbart said:
I've created a Dialog box with textbox to display messages to the
user of the program. Whenever I write text to the text box it always comes
out highlighted. I can't seem to figure out how to de-highlight the text
within the textbox. Anyone have any idea how to do this? Thanks
 
M

Marvin Scheinbart

Tim:

I just tried your suggestion and it does not de-highlight the text.
The only way I've found so far is to manually click on the text with my
mouse, then the text de-highlights. Thanks for the suggestion.

Marv
 
T

Tim Wilson

Interesting... Can you post some code that repro's this behavior? I'll have
a look at it.
 
M

Marvin Scheinbart

Tim

Below is the section of code that invokes the dialog box. This
code opens the System File Open dialog box and gets a target file from
the user. If the cancel button is pressed, a dialog box with a textbox
is displayed to tell the user that not file name was found. Hopefully
this is what you are looking for. Thanks for taking interesting is
solving my problem.

Marv


private void FileOpen_Click(object sender, EventArgs e)
{

DialogResult buttonClicked = OpenFile.ShowDialog();
global_vars gvars = new global_vars();
string inputfilename = OpenFile.FileName;

if(inputfilename != "")
{
FileInfo f_info = new FileInfo(OpenFile.FileName); // Get
file statistics
if (f_info.Exists == true)
{
gvars.input_file_name = inputfilename; // Get file
name.
Textbox_OpenFile.Text = inputfilename;
}
else
{
// Input file name was not found, error
}
}
else
{
InfoDialog InfoDialog = new InfoDialog();
InfoDialog.ProcessInfo_TextBox.Text = "No File Selected";
InfoDialog.ProcessInfo_TextBox.SelectionLength = 0;
InfoDialog.ShowDialog();
}

}
 
T

Tim Wilson

Now that I see the code, I would imagine that since the TextBox is the only
control on the InfoDialog, or the control with the lowest TabIndex, that the
TextBox is getting focus and all it's contents are being highlighted when
the Form is first shown. Set the SelectionLength property of the TextBox to
zero inside the constructor for the InfoDialog class instead of on the
InfoDialog instance before it is shown, as you are doing now.
 
M

Marvin Scheinbart

Tim:

Your explaination of the problem gave me an idea of how to solve
the problem. I adding another textbox, made it invisible, and set the
tabindex to zero. My visible textbox with a non-zero tabindex now has
regular text, not highlighted text.

I took a look in the VC# documentation and even knowing the fix, I
could never extract this information from the information provided.
Thanks for your help, I appreciate your assistance.

Marv
 
T

Tim Wilson

Did setting the SelectionLength property of the TextBox in the constructor
not work? Or was there another reason why an invisible TextBox made more
sense in your particular situation?
 
M

Marvin Scheinbart

Tim:

I'm a new user of the Microsoft Visual C# design environment and
don't know all ins and outs, I'm learning. I hunted around for a while
but could not determine with a great degree of certainty that I indeed
had found the constructor you talked about. I did put the suggested
change in what I thought was the contructor. It compiled and ran fine
but the text was still highlighted. Since the phantom textbox method
worked, I decided to stick this method of fixing the problem and move
on.

You comments are always welcome, thanks for your help.

Marv
 
T

Tim Wilson

Oh ok. In general terms, the constructor is a method of a class that has the
same name as the class. The purpose of a constructor is to allow for any
internal initialization that may need to take place when an instance of a
class is being created. In other words, the contructor is a method that is
called when an object is being constructed. Constructors can be overloaded
(same method name, different method parameter types or ordering), and
another way to identify a constructor is to look for a method without a
return type (not even "void"). So in this situation, a contructor would look
like this.

public class InfoDialog : System.Windows.Forms.Form
{
// Basic Constructor
public InfoDialog()
{
// Note: Designer-based forms will have a call to an
InitializeComponent()
// method. So place any other code you want after this method call.
InitializeComponent();
ProcessInfo_TextBox.SelectionLength = 0;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top