RichTextBox replacing input with "random" unicode character - c#

I have an application that allows users to browse through data. I have menu items controlling navigation and a RichTextBox displaying the data. Very straightforward.
tl;dr version
It mostly works except for one strange problem. There are instances where the RichTextControl will replace the first character typed with a random unicode character. Feel free to download this sample app and see for yourself:
http://www.technitivity.com/stackoverflow/RichTextFocusTest.zip
Full Explanation
The issue happens when navigating between rows. It's best described with a few use cases:
Use Case 1
Navigate anywhere into the dataset.
Press back.
Press next.
Type any letter, say, "F".
Result: "F" appears in the RichTextBox as expected.
Use Case 2
Navigate anywhere into the dataset.
Press back twice.
Press next twice.
Type the letter "F".
Result: instead of "F", "ᅲ" appears in the RichTextBox.
Use Case 3
Navigate anywhere into the dataset.
Press next twice.
Press back twice.
Type the letter "F".
Result: instead of "F", "᧴" appears in the RichTextBox.
The navigation process entails nothing more than:
// either forward
i++;
// or backward
i--;
// then update
RichTextBox1.Text = MyData[i];
Procedurally speaking:
// This works
RichTextBox1.Text = MyData[3];
// This works
RichTextBox1.Text = MyData[3];
RichTextBox1.Text = MyData[2];
RichTextBox1.Text = MyData[3];
// This doesn't work
RichTextBox1.Text = MyData[3];
RichTextBox1.Text = MyData[2];
RichTextBox1.Text = MyData[1];
RichTextBox1.Text = MyData[2];
RichTextBox1.Text = MyData[3];
Granted, that's not what's actually happening, but it is what's effectively happening.
It's important to note that this doesn't happen if the RichTextBox loses focus between updates. It only happens if the RichTextBox retains focus while its Text attribute is updated in accordance with the above description.
I'm at a complete loss about what's causing this, how to fix it, or why I can't seem to find anyone else with this problem.
I've reproduced it on 64-bit Windows 7 and 32-bit Windows Vista. This is on .NET Framework 4 though I was also able to reproduce on a .NET Framework 2 project.
Here's hoping someone else has run across this (and solved it!)
Edit:
Here's a screenshot:
http://www.technitivity.com/stackoverflow/RichTextBox-Screenshot1.png
As mentioned in the comments, to reproduce this in the sample app, you have to use the keyboard menu shortcuts. If you click on the menu items (or the toolbar buttons), the RichTextBox loses focus and the problem goes away. But if you navigate through the items using Alt+Left or Alt+Right (back/next) and then type, you should see something like what's shown in the above screenshot.

I hesitate to call this an "answer", but I couldn't find a "Post a Hack" button and this hack does get me by for now. I'm not thrilled about it, but sometimes you just have to move on. Here it is.
Since the problem went away when the RichTextBox lost focus, I tried an experiment:
I created a visible, 0-pixel wide textbox, called Hacktastic.
I added a KeyPress event to the RichTextBox.
On KeyPress:
Hacktastic.Focus();
Hacktastic.Text = KeyChar.ToString();
MyRichTextBox.Focus();
This worked and (at least for now) I'm sticking with this as a solution. If anyone could still try out my sample project and reproduce and/or solve this, I would love further feedback:
http://www.technitivity.com/stackoverflow/RichTextFocusTest.zip
Steps to repro in the test project:
Using Alt+Right arrow, move through the dataset to, say, the fourth record.
Using Alt+Left arrow, move back through the dataset two places, to the second record.
Using Alt+Right arrow, move back to the fourth record.
Press any KeyChar.
Observe KeyChar is replaced with a "random", large-value unicode character.
I say "random" because a specific set of navigation (back/next) keystrokes will insert the exact same unicode character. However, depending on where you start in the set or how far back you go, you'll get a different character.
Also, note that only going back one record and forward one record does not cause the problem. You have to move at least two records for this to happen.

Related

Visual Studio - Space after auto filling word

recently moved over to visual studio
When I start typing a word, let's say "Tex" I get the popup with possible auto-fillings, and if I press enter, it automatically changes it to "TextBox"
The problem I have is, whenever I do that, I have to press 'space' to start typing the next word, something that get's pretty annoying if you type 10,000 words..
Is there an easy way to make the typing start one space after the autofilled word?
Thank you in advance :)
Pic1: http://prntscr.com/fbpzdq
Pic2: http://prntscr.com/fbpzlc
Yes, you can simply use the Spacebar to perform the auto-complete instead of pressing Enter. Both methods finish the completion of the word, but using the Spacebar will also add a space after the word.
It even works when using the "." character after a name; you still get the intellisense list of object members to choose from, and auto complete will magically remove the space(s) when you type a ; or closing brace }, or any other character that triggers line formatting.
Example
// This is what it looks like while typing, using Spacebar to complete the words
this .textBox1 .Text = "hello"
// And then as soon as the `;` is added, it's all fixed:
this.textBox1.Text = "hello";
And, for what it's worth, whitespace is allowed on either side of the . between an object and it's members. So even if the spaces were left as in the first example, the code still compiles and runs just fine.

Why is this WinForm in execution so different from its design?

I have a simple WinForm whose only function is to display a message passed to it. The message is passed as a string and is displayed in a textbox. It is set to be a FixedToolWindow (and no matter how else I set it I get the same behavior in the end).
The form is called with:
PswCoordFailDisplay coord1 = new PswCoordFailDisplay(inputString);
coord1.Show();
Here are two screenshots: top is of the designer; bottom is how it is actually painted.
This is part of a fairly complicated application with about 10 other WinForms. They all act like they're supposed to, except this one.
The changes from design are:
the Form's dimensions change
the textbox changes to occupy the entire width of the form (possibly in response to the size of the text that fills it -- although ScrollBars is set to Both)
the Close button changes its text to "OK" and migrates to the bottom right
the form's Title is gone
the X-closer is different
when I turn ControlBox to False, the X-closer appears regardless, and in the same form as in the bottom example
I pretty much take all the defaults that a WinForm provides when creating the WinForm, except for a couple of items that shouldn't make this kind of difference.
What on earth is happening? I mean, it still works fine, but it's not what the design calls for!
If you pay close attention to the pictured screenshots presented in the question, you will notice that the bottom one shows a MessageBox, not the intended WinForm. As commenters T McKeown and Grant Winney pointed out, there is no way for the elements of the form to go that far off the rails unless the form was coded that way. Since it wasn't coded that way, the expected form is NOT being called. There is a very superficial resemblance, of course, which accounted for my confusion.
If I had showed the complete calling code, a sharp eye would have seen this quickly:
switch (opRes.OpResult)
{
case OperationResult.Result.Succeeded :
MessageBox.Show("Password coordination succeeded.");
LoadUser();
break;
case OperationResult.Result.PartialSuccess :
MessageBox.Show(String.Format("Password coordination partially succeeded: {0}{1}", Environment.NewLine, opRes.Notation));
LoadUser();
break;
default :
MessageBox.Show("Password coordination failed.");
PswCoordFailDisplay coord1 = new PswCoordFailDisplay(opRes.Notation);
coord1.Show();
break;
}
As you can clearly see, with a "partial success" control would be passed to the second case in the switch statement, not the default. The second case was supposed to be coded with the new WinForm, but I forgot to do it! And so there you see the problem:
READ YOUR DARNED CODE BEFORE POSTING YOUR PROBLEM ON STACKOVERFLOW!
Less embarrassment that way.
ETA: Also, if I had posted the above code in my question, as #Rockster suggested, someone would have noticed it immediately, perhaps saving a step or two.

C# Windows app, clear textbox on focus

I'm very new to C# and I'm trying to make a simple GUI hangman game to help me learn. I am using a textbox to both output errors (The letter was already entered. Try again) etc. and input the user's guess. However my problem comes in that whenever the user is given an error, they have to manually clear the textbox. What I'm looking for is a feature in most search boxes, google for example, that clears (in google's case, highlights) the text currently in the box.
I know it can be simply done using
textboxname.Clear();
but I'm not sure where it should go, I can place it under the code for a button without a problem but my instinct is to put it outside of the button's {} , however when I try this the text box isn't recognized and if statements can't be used.
I think I'm looking for:
if (TextBoxName.Focus)
{
TextBoxName.Clear();
}
But I'm just not sure where to put it

Updating The .Text of the TrayNotifyIcon

I'm currently using the .Text of the TrayNotifyIcon to display a statusdisplay when the user has the mouse over it (for a percentual completion of a process)
Thus I just set: TrayNotifyIcon.Text to the appropriate % of completion.
Example (the following code is part of a code I use where I create a new thread which sets in the subprocess variable if it is completed or not and also how many % completion are. The code below shall display as the TrayNotifyIcon.Text how many % of the subprocess are completed with updates every second):
while (subprocess.NotCompleted)
{
TrayNotifyIcon.Text = "TextToUpdateTo....." + subprocess.percent.ToString() + "% completion";
Thread.Sleep(1000);
}
Now I've seen that it only updates the display whenever I move the mouse and does not update it as soon as I set .Text appropriately.
Thus my quesiton is is there any way to make it so that I can tell the system to update the text that is being displayed?
The system displays the hint text when the mouse is hovered over the notification icon. This text is not expected to be dynamic. The display of the text is handled by the system. When it wants to display the hint, it queries the icon for the text, and then displays it. It will not go back and check if the text has been changed, and there is no mechanism to inform the system that the text has been changed and should be updated. This is a feature of the shell (Shell_NotifyIcon) rather than anything in the .net libraries.
So, using TrayNotifyIcon in its vanilla form, there is nothing you can do to change this behaviour. If you really want dynamic update of hint text you will have to suppress the system drawn hint (by setting Text to an empty string) and drawing your own hint window. You'd need to detect the mouse hovering over the icon, and also detect when it leaves the icon.
Frankly, I don't think that this is really a very good piece of UX design. If I were you I would find a different way to let the user receive this feedback.

Problem changing values in textbox

Simplifying
I have a text box and a button
The button just create an messagebox with the text from the textbox.
But i change the value of the textbox, the new value apears (Ex: Type 123) but the message box does not show the value.
If i try to use the value in the programming (get the value by textbox1.text) the variable has nothing ( textbox1.text = "") but i can still see what i typed in the form.
Anyone has any clue?
Your button's click event handler should look something like this
private void button_Click(object sender, EventArgs e)
{
MessageBox.Show(textBox.Text);
}
I suspect you already have code similar to this and that at some point the textbox is cleared or otherwise set to String.Emppty but without seeing actual code it is difficult to help you
When/where did you check the value of textBox1.Text? If you're checking it in the constructor, Form1_Load, or anything else that occurs before you'll have typed text, you will get an empty value.
To properly check the value of textBox1.Text, you should set what's called a breakpoint on the line that calls MessageBox.Show(textBox1.Text). To do this, click in the grey area of the source editor (it's on the far left) on the line containing MessageBox.Show(..). A red circle will appear and your code should be highlighted. When you run your application and click on your button, your application should pause and Visual Studio will highlight that line and from here you can hover over "textBox1.Text" in the MessageBox.Show() line and it should show you the current value.
If your application is as simple as a form, a textbox, and your button1_Clicked event handling code, this should work no problem. If it is not this simple, then you need to look for anything that sets the value of the textBox in your code and make sure it isn't passing any blank values by using breakpoints.
To solve this properly, though, we really need more information.
Thanks Eric and Crippledsmurf. As both of you said, its hard to help without the code.
The problem I found is that when calling the form, I send some objects by reference, so I can track them down and I found that when (don't ask me why it happens that way, I'm still working on it) the construtor is called he make an new component, so the component in the interface no longer represents the one pointed by the variable "textbox1" (Yes Crash893, I haven't mispelled the name).
I found that I was making some mess with the references, and probably that was causing the problem. I fixed the problem by changing the actions performed by references for delegates and events, but I couldn't track down the exactly source of the problem.
Thanks, again, everyone for the insights.

Categories

Resources