Silverlight C# - Set selection in textbox via code? - c#

I'm working on a spellcheck function for my app, and want to have the word that's currently being looked at highlighted. I'm tracking the char count as I loop through the words in the textbox, so I know where to set the selection at.
I've tried txtArticle.Select(0, 10); just as a test, as well as setting the txtArticle.SelectionStart and txtArticle.SelectionLength properties, but the textbox doesn't show anything highlighted. What's the dealio?
Actual code I've tried:
txtArticle.SelectionStart = charCount;
txtArticle.SelectionLength = checkedWord.Length;
as well as
txtArticle.Select(charCount, checkedWord.Length);
I've positively no idea what I'm doing wrong, unless you can't set what's selected in the TextBox via code, which I just can't imagine is the case. Is there perhaps some extra property that I need to set for the TextBox itself?
Thanks yet again!
-Sootah

Documentation on MSDN of TextBox.SelectionStart Property has an example that works. This states that programmatic text selection is actually supported in Silverlight.
Looks like something else is going wrong in your application. When do you call this code? Try calling it after everything is loaded, and rendered on screen. May be on a click of a button.
If above does not work, create a sample application/page and try to follow MSDN example. When you get it working, try to figure out why it doesn't work in your application.

Related

How to force a textbox to update its text

So I've looked through maybe five to ten questions on stackoverflow about setting text into a textbox using a range of commands. I've tried SetWindowText, SendMessage with EM_SETSEL and EM_REPLACESEL, and a few others that I can't think of off the top of my head. For the most part I have been successful, except for one strange occurrence.
When I set the text of this specific text box, nothing appears, nothing changes. At first I thought I was not setting the data correctly. However, when I use Spy++ or Winspector to see the text of a textbox, the correct data with my changes are in there, but not displayed on the actual textbox. Even stranger, when I click back into the form with the textbox I "edited", spy++ and Winspector's data changes to what the textbox is displaying.
I spoke with a friend of mine and he mentioned it might be a race condition. I'm trying to edit this box and the textbox is being edited by some other thread as well.
If anyone has any suggestions I would really appreciate it.
Edit: Alright so I did some more digging into what is causing the problem with the text changing back. I opened up Winspector and had it watch the textbox. From there I was able to identify the messages sent to the window between the final time the text data is correct, to the first time it changes. I also exported the results to an xml document with the parameters, but it is about 680 lines long.
Heres the list of messages:
http://i.imgur.com/SBCFHK8.png
The control may just need to re-paint itself.
You can try two API's to do that:
InvalidateRect - PInvoke
RedrawWindow - PInvoke

Highlight text in Windows 8 TextBox

I'm looking for a way to highlight (not select) words based on search terms inside a Windows.UI.Xaml.Controls.TextBox control.
There doesn't seem to any way to override the text rendering behaviour? Can this be done with the textbox control?
Edit:
I was originally trying to use the RichEditBox but was having a problem with being able to paste in formatted text which I was trying to prevent (the only event I can clear the formatting on is TextChanged which seems a little late). I also really need more control over the rendering of the highlights
Textboxes are very limited in regards to formatting and text selection.
You might want to use the RichTextBox control rather than a TextBox as it gives you greater selection and formatting capabilities to make highlighting multiple terms easier.
Here's a quick start: http://www.devx.com/dotnet/Article/34644
I am having the same issue. But Incase you didn't know, I'll post this anyway (it's not a complete answer but might get you started.)
You can highlight words by using:
int indexOfFoundKeyword = 1;
int lastIndexOfFoundKeyword = 12;
txtbox.SelectionStart = indexOfFoundKeyword;
txtbox.SelectionLength = lastIndexOfFoundKeyword;
If you place this inside a button click event and then type about 20 chars and click the button you'll see that you can highlight words in a Metro textbox. The problem that I'm having is getting the start and ending of the found keyword so that I know what values to assign to SelectionStart and SelectionLength and if I understand your question correctly, I think that's where you're stuck at, too.
Update
OK, this seems to be a little tempremental but works most of the time. (and accurate when it does work):
// find the word 'jason'.
string word = "jason";
int a, b;
a = genericBox.Text.IndexOf(word);
b = word.Length;
genericBox.SelectionStart = a;
genericBox.SelectionLength = b;
If all you are trying to do is find some text, then select it, then this proves that it can be done. Now I know this is not the best way but it's the only way I've been able to get something like this to work in a plain TextBox for metro apps.
Perhaps you could create a control based on the TextBox control or altogether create a custom control. See here and here for more help.

Custom MessageBox in C#

I am making a custom MessageBox in my own language (Persian). I want to know which component may I use for the text?
Label is not multiline and TextBox is a little bit not appropriate.
Which component does Visual Studio use itself?
What is the component I specified in the picture?
You can use a label. It has a number of options for laying out the text... One way is to set it to "AutoSize = True".
I support the answer of RQDQ, labels could be enlarged to occupy more than one line.
They have a property named TextAlign. When set to LeftCenter or MiddleCenter you could simulate the behavior of text in a message box. If the text doesn't fit in a single line, the label wraps it automatically on another line. However in this case I will let the property AutoSize to its false default value.
It all depends on what kind of Visual Studio project you are doing. Wpf or Silverlight or Asp.net or even windows, everything will depend upon which kind of project it is.
That looks to me like the MFC style label with an image, or in a browser it would be done with javascript.
You Can Use RichTextBox. I Use it For My Custom MessageBox And it works Correctlly

How can I display an error to the user while they're still editing a DataGridView cell?

If a user enters invalid text in a DataGridView's cell I want to prevent the user from leaving that cell and display an error icon in that cell with an error message in a tooltip. It seems that normally the error icon won't appear until the cell is no longer in edit mode. I found an example of how to get the error icon to appear while it's still in edit mode, but the tooltip won't show up when I hover over it.
I'm using Windows 7, but I couldn't get it to work in XP either, so we can rule that out.
Can anybody figure out what the example is missing or suggest an alternative approach that would yield the same results?
To get the example working you'll need to create your own form, copy/paste the example code, add using System.Drawing.Drawing2D;, and assign the event handlers yourself.
I suggest you look at IDataErrorInfo interface
For example see the following post.
Its a little hack-y, but simply adding something like this to the end of your validation method would work (assuming your view isn't bound to some data so that EndEdit() submits a new value to a data structure or something):
if (e.Cancel)
{
myView.EndEdit();
myView.BeginEdit(true);
}

Infragistics WebTextEdit - Setting value in Javascript function

I'm currently using the Infragistics component set for .Net 2.0, Visual Studio 2005 and C#. I have the following chunk of javascript code (text and other variables are declared elsewhere):
***alert(box[select].value);
text.value(box[select].value);
alert(text.value);***
'text' is an Infragistics webTextEdit, while box is just a standard listbox. The two alerts seem to be working fine. Before I set the value, the listBox's selected value might be 'hello', and the alert box which pops up after I've assigned this value to 'text' is also 'hello'.
However, the value shown in the box on my form never appears to get updated. Anybody have some suggestions as to where I'm going wrong, gotchas in how Infragistics handles this kind of thing or anything else? I'm aware there may not be enough info here to diagnose the problem.
The value property is only available server-side. Using it client-side won't do anything. Setting it would have to be done server-side, or you'll need to craft fun javascript to address the text of the element that the control is actually rendered as in the browser.
http://help.infragistics.com/Help/NetAdvantage/NET/2007.3/CLR2.0/html/Infragistics2.WebUI.WebDataInput.v7.3~Infragistics.WebUI.WebDataInput.WebTextEdit~Value.html
Unless I misunderstand the question, if text is an instance of the Infragistics WebTextEdit, you should just be able to do:
text.setValue(box[select].value)
Or if text is the underlying input control, but 'id' is the ID of it,
var edit = igedit_getById(id)
edit.setValue(box[select].value)
See the WebTextEdit CSOM for more.

Categories