Windows Store aps - select text and tap - c#

I have a problem with TextBlock text selection (Windows Store apps, C#).
If I set IsTextSelectionEnabled = True, then I can't Tap on the text.
I need to be able to open popup on taping text and select this text, but it seems like I can't do both.
Is there a way to have both - Text Selection and Tap - for one TextBlock?
Edited: I need Text Selection to be able copy text.
Edited: TextSelection and Copy feature works for multiple word text, where not every word is tappable.
Set TextBlock property IsTextSelectionEnabled = True
Set text to TextBlock, using TextBlock.Inlines:
textBlock.Inlines.Add(new Run() { Text = "Click " });
Hyperlink hyperlink = new Hyperlink();
hyperlink.Inlines.Add(new Run() { Text = "here" });
hyperlink.Click += hyperlink_Click;
textBlock.Inlines.Add(hyperlink);
But if whole text is tappable, this solution doesn't help.
If I set text like this:
textBlock.Text = "Click here";
textBlock.Tapped += textBlock_Tapped;
Then I can't tap text. I can only copy this text.
If I set text like this:
Hyperlink hyperlink = new Hyperlink();
hyperlink.Inlines.Add(new Run() { Text = "Click here" });
hyperlink.Click += hyperlink_Click;
textBlock.Inlines.Add(hyperlink);
Then I can't copy text. I can only tap on it.

Using IsTextSelectionEnabled will handle Tap event by itself. So you will not have ability to use both. If you want to show PopUp when text get selected you can use SelectionChanged event.
UPDATE
If you just need user to be able to copy any part of text you don't need to create any PopUp. Just enable IsTextSelectionEnabled and user will be able to select any text part, press right mouse button and copy text using TextBlock's own PopUp. And in case it's touch or pen interaction this PopUp will be shown right after user selects text.

Related

Hide button text behind button image

Is there any method to hide a button text behind button image like bring to front or send to back option?
I only need to hide or show button image only as I have a code that coverts the original text CloseButton.text = "&Close"; to CloseButton.Text = "&Cancel";
to perform another command so I can't use CloseButton.Text = "";.
Tried this link - WinForms button with image and text but my button size is too small that it would only show the text and not the image no matter how I mix and match TextAlign and ImageAlign.
Any help is much appreciated. Thanks in advance.
Sample Button Size below:
Check this
Place Textbox in Button and set textbox.visible=false method
Is there any method to hide a button text behind button image like
bring to front or send to back option?
There is no such built in but you can simply clear out the text on click event of the controls. Example: if you have radio buttons for send to back then on click of that clear out the control text saying controlId.Text = string.Empty
As #Rotem posted in the comments.
Have your code behind use the Tag property rather than Text. Easiest way out is using properties for what they were made for.
Instead of using CloseButton.text = "&Close"; I changed it to CloseButton.Tag = "&Close"; and made my code worked around it to have the same function without placing an actual Text in my Buttons. Credit this asnwer to #Rotem. Thanks.

How to use RichTextBlock.HasOverflowContent in WP 8.1 RT?

I'm using RichTextBlock in Windows Phone 8.1 RT to show some text. To limit the size of the text that can be displayed at a given time, I'm setting the MaxLines property. Whenever the text exceeds this value, it's trimmed.
Now, I have a hyperlink at the bottom of the RichTextBlock that should get visible whenever the text is trimmed. For detecting if the text was trimmed, I'm using the RichTextBlock.HasOverflowContent. If this property is set to true, I set to visibility of the hyperlink to visible so the user can click on it and see the full untrimmed message.
But there is a problem with this solution. Sometimes the text is trimmed, but the property is still false and the hyperlink stays hidden.
I don't really know how to use the above property to detect content trimming. What's the correct way to use this? I'm doing the processing in the Loaded event of the RichTextBlock:
private void RichTextBlock_Loaded(object sender, RoutedEventArgs e)
{
var richtextblock = sender as RichTextBlock;
// Check if the content of the RichTextBlock was trimmed.
if (richtextblock.HasOverflowContent)
{
// Prepare hyperlink and set visibility to visible.
}
}
Instead of checking the value of HasOverflowContent when the RichTextBlock is loaded, why don't you try to bind the visibility property of the hyperlink to the HasOverflowContent property (using a Boolean To Visibility converter of course)?

WPF TextBox selectall and show caret at the same time

after a long search on the net, I hope you can help me.
My Problem:
I want to select the complete text in a TextBox
and will show the caret (blinking cursor) after the last character.
Always I have found information about one problem or information to hide the caret.
The separate things are no problems but the combination of it don't work.
// Set the focus to the TextBox
myTextBox.Focus();
// Select the complete text, but hide the caret (blinking cursor)
myTextBox.SelectAll();
// or
// myTextBox.Select(0, myTextBox.Text.Length);
// Set the caret after the last character, but loss the selection from the text
myTextBox.CaretIndex = myTextBox.Text.Length;
So, I see the caret after the last character, but the text is not selected
myTextBox.Focus();
myTextBox.SelectAll();
myTextBox.CaretIndex = myTextBox.Text.Length;
And so, the text is selected, but no caret is shown.
myTextBox.Focus();
myTextBox.CaretIndex = myTextBox.Text.Length;
myTextBox.SelectAll();
And that's the problem: one of them deactivate the another one, but I need these two things at the same time
I using WPF and .Net 4.0
Thanks for helping :-)
The problem is the strong internal connection in the TextBox between CaretIndex and the Selection.
Whenever you modify the selection with Select() or SelectAll(), the TextBox automatically places the CaretIndex at the beginning of the selection. In reverse, the TextBox clears the selection when you manually modify the CaretIndex. You can make this behavior visible, if you register for SelectionChanged in the TextBox and output the current CaretIndex to Console.
This is for a good reason, as Okuma.Scott already mentioned in his comment.
So if your desired behaviour is really required, you probably need to implement your own CustomTextBox.
This worked for me:
TextBox.Text = _Text;
System.Windows.Input.Keyboard.Focus(TextBox);
TextBox.GotFocus += (sender, e) => {
if (_selectAll)
{
//I think Caret can be set here but I didn't try it
TextBox.SelectAll();
}
};

Changing RichTextBox to a Label [duplicate]

This question already has answers here:
How do I change the style of a disabled control?
(2 answers)
Closed 9 years ago.
I am using a rich text box as a label for my application. The text box is read-only but its content can be selected. How can I make users can't select text in the rich text box while it is read-only?
When I disable the control can't select text but I loose the colors, because they become grey (disabled). How can I disable text selection without disabling the rich text box control?
FYI: I am using a rich text box as a label because, I need to change the fore color to red for one word in the string which needs to be shown to the user. I used this SO article and following method to do it.
string word = "red";
int start = richTextBox1.Find(word);
if (start >= 0) {
richTextBox1.Select(start, word.Length);
richTextBox1.SelectionColor = Color.Red;
}
EDIT: BTW It's C# WinForm
Simply handle the selection, and restore it to "nothing":
// so you have colour (set via the Designer)
richTextBox.Enabled = true;
// so users cannot change the contents (set via the Designer)
richTextBox.ReadOnly = true;
// allow users to select the text, but override what they do, IF they select the text (set via the Designer)
richTextBox.SelectionChanged += new System.EventHandler(this.richTextBox_SelectionChanged);
// If the user selects text, then de-select it
private void richTextBox_SelectionChanged(object sender, EventArgs e)
{
// Move the cursor to the end
if (this.richTextBox.SelectionStart != this.richTextBox.TextLength)
{
this.richTextBox.SelectionStart = this.richTextBox.TextLength;
}
}
Taken from: http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/d1132ee5-acad-49f3-ae93-19d386fe2d12/
(By the way, a little bit of searching goes a long way.)

Programmatically changing button label in Monotouch

I made a .xib with a button, and referenced it with an outlet to "btnActies"
when i do the following (at any given time) in the back-end C# code
btnActies.TitleLabel.Text = "This is a
new label!";
When i build and run the app, the label on the button changes to "This is a new label",
but then, if I touch the button, the label reverts to the 'default' text i set in the .xib file.
How do i change the label on a Monotouch UIButton and keep this from happening?
When you want to set some text on a UIButton, you do not do it by altering the text of its TextLabel property. You do it by calling its SetTitle method, passing as the second argument, the button state for which the title will be set at runtime. Chetan Bhalara's answer is correct, here is the C#/MonoTouch equivalent:
btnActies.SetTitle ("title", UIControlState.Normal);
They way you are doing it right now doesn't work, because the label's text is changing internally whenever needed, to the title set in Interface Builder (if you have set it), in this case when you tap the button.
//Improved Formatting
Hello
You can use this code for change the button label for iPhone.
[btnActies.setTitle:#"Title value1" forState:UIButtonStateNormal];
[btnActies.setTitle:#"Title value2" forState:UIButtonStateSelected];
[btnActies.setTitle:#"Title value3" forState:UIButtonStateHighlighted];

Categories

Resources