My program has several Label controls that are updated to have different text every so often. I have a few icons that I want to reference within the text. I figured that instead of just displaying "(E)" in the Label, there should be a way to replace that with the corresponding image that I have that looks like: . I figure that I need to override the Label.Paint event, but I'm not too sure how to do that properly. Every occurrence of "(E)" needs to be replaced with the image inline.
Example
Look for the (E) icon on the top. → Look for the icon on the top.
This is not a trivial exercise.
You'd have to provide a property on your derived label class to attach the image(s) to the text.
Embed some sort of token(s) in the text to represent the image(s).
In the OnPaint you have to parse the text for the token(s)
Do a graphics.MeasureString() for each bit of text between the tokens. And then render it with graphics.DrawString() move to the right by the width of the text, render the image based on the token using one of the many graphics.DrawImage() overrides - move to the right by the width of the image and repeat.
Related
I'm adding buttons (with text and images) to a richtextbox control, but because I don't use the following line:
richTextBox1.AppendText(Environment.NewLine + str);
The scroll bar doesn't match the total number of lines, and doesn't resize properly, how can I solve it?
What is going wrong
I find this to be a very peculiar way of doing things. So first, lets add a little background:
When you use richTextBox1.Controls.Add(new button()) - what you are doing is: you are adding the new button as a child of the richtextbox, not as content - and this is the problem.
The scrollbars are set to read their height/scrollable area etc from the content of the richtextbox, and yours has no content, hence no scrollbar (or false readings etc).
You could create a custom richtextbox control that inherits its scrollable area from the location of its child controls. However, if you would rather avoid that route, please continue reading:
The correct way
I do not know your use case, so this may not work, but I would remove the richtextbox control and instead use a panel control. The panel control for example will update it's scrollbars based on child controls, whereas the richtextbox wont - and this should give you the behaviour you are expecting.
I have a question...i'm trying to hover a specific word from a label with another label, but i don't know how to get the specific word position from the label's text. I would be very happy if you could help me at least, how to get the word location in the label. I have to say that the label Autosize is false, so it could have more than just one line. Thank you!
If I understand correctly, you want to reference one of the words in one label's .Text property and use it in another label when you hover over the first label.
If you know already what word in the string you want, it's easy. You can fetch it by the 0-indexed position of the letters, since a string is really just an array of type Char. You can also use String class tricks like .Substring() to fetch the word.
I think the degree of complexity you've added, though, is that (and correct me if I'm wrong) you want the second label to display the specific word over which the mouse is hovering while the mouse is hovering over the first label. That involves getting the label's coordinates as well as the pointer's, and a rather complex method for determining "what is a word, really?". If possible, I would split your source label into a label for each word. This can be done with dynamic labels by inheriting from the Label (or a container, like Panel) class in a custom control and giving it an IEnumerable of Labels as a property. Doing this, you can assign the OnHover handlers at runtime, and evaluate these events as individual labels rather than having to do the math and hope that a screen resolution change doesn't ruin your day.
Using the latest pre-release, I noticed that the button now has a Button.ContentLayout property, which I am hoping will allow us to add custom views to buttons whilst retaining the rest of the buttons functionality.
The questions are, is this what this is for? And if so, how is it used?
Kind Regards
Brian
No, this property does not allow you to set any custom content to be rendered inside the button.
ContentLayout on the Button element is a property of type "ButtonLayoutContent" it determines the positioning of the button's image in relation to it's text.
It has two properties, image position and spacing.
Position is used to set the placement of the image in relation to the text. The image can be above or below the text, or to the left or the right side of the text.
Spacing is the amount of space between the image and the text.
In the Android implementation of the Button renderer, it sets the CompoundDrawablePadding property, which is defined as the padding between the compound drawables and the text.
On iOS, the default renderer does some math to figure out the correct values for ImageEdgeInsets, TitleEdgeInsets and ContentEdgeInsets
Example usage in XAML:
<Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me" ContentLayout="Top,10">
In C# code, you simply pass the two values in the constructor
btn.ContentLayout = new ButtonContentLayout(ImagePosition.Top,10);
We have a case where we need to display Character Ellipsis(i.e. show text as trimmed) when we have multiline text.
The textblock shows trailing ellipsis when the content is anyway bigger than the width of the multiline TextBlock (i.e. TextWrapping is set to Wrap).
But we have a case wherein, we need to show only one line with ellipsis whether the text width of the first line is greater than the width of textblock or not.
For example, consider the following sample text
String str = "1\n2\n3456\n45889";
textBlock.Text = str;
The TextBlock should display as shown below:
1...
and the ToolTip will show the entire text. I tried doing some research on the possibilities but could not find much help and was wondering if anyone in the community has encountered such a situation or perhaps could suggest me?
Since, we shouldn't change the underlying data object (real time scenario) but only change what is rendered to the user, I am guessing a Converter should do the trick but I am still stuck on how to proceed. Or do you guys have any other alternatives?
Create a custom control based off of the textblock which handles the business logic needed for the ellipse.
The binding of the actual text to a specific property can ensure that the text is not changed. While in a separate property you have the visual text with the ellipse which gets updated when the original text changes (the dependency property change event) and the visual text subsequently displayed on the screen. Also have the tooltip bound to the original text which helps in that scenario of showing the actual text and not the ellipsed text.
By creating an easy custom control you have the ability to handle the business logic all in one location and it can be used in other screens and projects.
I need the property called SelectionColor in the TextBox class, for a simple Syntax Highlighter - I can't use directly a RichTextBox - it causes too many problems, that's why I try to do this.
Is there any way to make that property available for a TextBox?
If it isn't possible, I'd try to write my own, but I need an idea on how to do it, basically how it works - is it based on drawing strings over the original text?
Thanks in advance.
Basically, using a TextBox for anything but plain text is a bad idea. First of all, you will eventually get a new feature to implement which is not present in TextBox and you will have to handle it manually. After some time you will implement a custom RichTextBox or something similar.
Second, it is relatively hard to even solve the problem you mentioned. Technically, you can override painting function (which you have to do if you want new functionality for TextBox). You can then let TextBox paint itself and paint the colored text above the image. But don't do it. You will get two (maybe more) problems:
Flicker of image. Once the original textbox has drawn itself, the image can be shown on screen (if you don't use double buffering).
Text alignment. It is hard to place colored text exactly above black text, plus you can run into problems with text rendering: you will need to clear area you're drawing in.