How to use RichTextBlock.HasOverflowContent in WP 8.1 RT? - c#

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)?

Related

StatusStrip label not visible when text too long

I have a StatusStrip docked to the bottom of a C# Form, it contains a label, the text in it displays fine, except when there is longer length of text then it does not display at all, and I have to widen the form and then all of a sudden it appears. Is it possible to show it in the form below:
This is a very long tex...
So that the user knows that the app is showing something and then he can widen it himself, because when it is not visible at all, it does not indicate anything to user.
You can create a custom renderer based on ToolStripProfessionalRenderer and override OnRenderItemText method and draw text with ellipsis:
public class CustomRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if (e.Item is ToolStripStatusLabel)
TextRenderer.DrawText(e.Graphics, e.Text, e.TextFont,
e.TextRectangle, e.TextColor, Color.Transparent,
e.TextFormat | TextFormatFlags.EndEllipsis);
else
base.OnRenderItemText(e);
}
}
Then it's enough to set Renderer of your StatusStrip to your custom renderer:
this.statusStrip1.Renderer = new CustomRenderer();
In below example, You can see the behavior of a ToolStripStatusLabel which it's Spring property is set to true and its StatusStrip uses CustomRenderer:
If you set
ToolStripStatusLabel.Spring = True;
then you won't get the "..." but the text will be shown even when the available space is insufficient.
On Visual Studio 2017, the accepted answer didn't work for me. So here is another simple solution.
Set LayoutStyle property of StatusStrip to Flow. i.e:
statusStrip1.LayoutStyle= LayoutStyle.Flow;
And Set
`statusStrip1.AutoSize= false;`

use custom control in windows forms checked list box

The Items property of a checked list box control in Windows forms is of type object, so my naive hope was that I can add a customized User control as item. (Since, usually, my task is to write logic for background tasks I'm not too familiar with UI programming, so this may be a stupid idea..)
More precisely I want to display two labels and a button in each line of the the checked list box. The first label is supposed to display the name of an object the user can select (so that later on a specific operation will be performed on all checked items). For any item checked, the button is supposed to allow the user to choose a file from which custom settings can be read for performing that operation and the second label should display the choice the user has made using the button (i.e. the file name or something like the string "default settings").
So, in the forms designer, I created a custom control CustomControl1 with label1, label2, button1, and methods to set the text properties, set autosize of the labels and the button to false, defined their size manually. Then in the main window I created the checked list box, to which I added custom controls. The constructor of my main window now looks as follows:
InitializeComponent();
UserControl1 uc1 = new UserControl1();
uc1.setLabel1("label1_text");
uc1.setLabel2("label2_text");
uc1.setButtonText("button_text");
this.checkedListBox1.Items.Add(uc1);
uc1.Visible = true;
This compiles without any error and also runs, but the checked list box shows an empty field. I also experimented with the size of the list box. If I reduce the height so that the check box just fits into it then I do see fragments of the button in the corresponding line, but no label.
Is it possible to use a custom form in a checked list box and if yes, what am I missing?
No, you can't do this.
The listbox only shows a list of elements. The listbox uses the property .ToString() for each objects in the list to show the items.
You need to look for a custom listbox
private void Form1_Load(object sender,EventArgs e)
{
checkedListBox1.Items.Add("IIT");
checkedListBox1.Items.Add("CSE");
checkedListBox1.Items.Add("EEE");
checkedListBox1.Items.Add("ICT");
checkedListBox1.Items.Add("URP");
checkedListBox1.Items.Add("ENGLISH");
checkedListBox1.Items.Add("BANGLA");
checkedListBox1.Items.Add("MATH");
}
private void checkedListBox1_SelectedIndexChanged(object sender,EventArgs e)
{
//var item=checkedListBox1.SelectedItem;
label1.Text=checkedListBox1.SelectedItem.ToString();
}

NullReferenceException Error When Trying to Change the Property IsEnabled of an AppBar Item

On one of my .xaml pages, I have an appbar with a few icons on it.
One of the icons pins the page to Start, so when it is pinned I want to change the IsEnabled property of that icon to false.
However I get this weird error; as described in the title when this procedure is called.
Here's the code:
if (Tile == null) { }
else { appBarPin.IsEnabled = false; }
any ideas?
The behavior with the Application Bar is different to the rest of UI elements. From App bar for Windows Phone:
The app bar doesn’t support some common features of controls, such as
data-binding. As a result, you can’t change the icon button and menu
item text by using Name properties that you set in XAML.
If you want to change a property of the appbar item, do it the following way:
ApplicationBarIconButton button = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
button.IsEnabbled = false;
Replace the 0 with the index of the button. I.e. if the button is the second button of the appbar, the index will be 1.
See more in How to change app bar icon buttons and menu items dynamically for Windows Phone
A null reference exception means that you can't say ".IsEnabled" if the thing before the dot is null.
It appears that appBarPin is null.

FieldInfo.CaptionStyle in custom form control not working

I have created a custom form control for use in my Kentico bizform using asp.net and I want to change the field caption style of another field in the form depending upon the value in my custom form control field. So, this is what I have done:
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if(this.Value == "1")
{
FormEngineUserControl formItem = (FormEngineUserControl)this.Form.FieldControls["Other"];
formItem.FieldInfo.CaptionStyle = "font-weight:bold";
}
}
However, the field caption in the form doesn't seem to get bolded. I tried testing if the event even fires and it does. Infact, if I try something like formItem.Text = "Something" then the texbox gets filled with "Something". While debugging I also noticed that the field caption style does get changed to "font-weight: bold" but that doesn't show on the form. So, there is something wrong with the captionstyle property or the way I am using it. How do I get it to work?
(Please note that the field control "Other" is a text box input)
It's probably too late in Page's lifecycle and the control has already been rendered. Try to set the CaptionStyle earlier (e.g. in control's OnLoad or OnInit) then you'll know with certainty.

How do I scroll a RichTextBox to the bottom?

I need to be able to scroll a RichTextBox to the bottom, even when I am not appending text. I know I can append text, and then use that to set the selection start. However I want to ensure it is at the bottom for visual reasons, so I am not adding any text.
You could try setting the SelectionStart property to the length of the text and then call the ScrollToCaret method.
richTextBox.SelectionStart = richTextBox.Text.Length;
richTextBox.ScrollToCaret();
The RichTextBox will stay scrolled to the end if it has focus and you use AppendText to add the information. If you set HideSelection to false it will keep its selection when it loses focus and stay auto-scrolled.
I designed a Log Viewer GUI that used the method below. It used up to a full core keeping up. Getting rid of this code and setting HideSelection to false got the CPU usage down to 1-2%.
//Don't use this!
richTextBox.AppendText(text);
richTextBox.ScrollToEnd();
In WPF you can use ScrollToEnd:
richTextBox.AppendText(text);
richTextBox.ScrollToEnd();
Code should be written in the rich text box's TextChanged event like :
private void richTextBox_TextChanged(object sender, EventArgs e) {
richTextBox.SelectionStart = richTextBox.Text.Length;
richTextBox.ScrollToCaret();
}

Categories

Resources