|
I gathered... It's why I gave up on the RTB, since in my case, rich text is not a priority, but auto-width is...
Thankfully normal TextBox does work
|
|
|
|
|
You can do this by handling each keypress, measuring the length of the displayed string, and changing the size of the RTB as needed. I would probably also turn off the scroll bars.
using System.Drawing;
private void RtbField_TextChanged(object sender, TextChangedEventArgs e)
{
RichTextBox rtb = sender as RichTextBox;
double horzPadding = rtb.Padding.Left + rtb.Padding.Right;
double vertPadding = rtb.Padding.Top + rtb.Padding.Bottom;
System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular;
if (!Enum.TryParse(rtb.FontStyle.ToString(), out style))
{
style = System.Drawing.FontStyle.Regular;
}
System.Drawing.Font measureFont = new Font(rtb.FontFamily.ToString(), (float)rtb.FontSize, style, GraphicsUnit.Pixel);
TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
Bitmap image = new Bitmap(1, 1);
Graphics graphics = Graphics.FromImage(image);
rtb.Width = graphics.MeasureString(textRange.Text.Replace("\r\n",""), measureFont).Width + horzPadding+10;
rtb.Height = graphics.MeasureString(textRange.Text.Replace("\r\n",""), measureFont).Height + vertPadding;
}
<Canvas Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
<RichTextBox x:Name="rtbField" Height="30" Width="0"
Padding="0,2"
VerticalContentAlignment="Center"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
TextChanged="RtbField_TextChanged"/>
</Canvas>
I gotta ask, though - aren't you concerned with too much text being entered to keep your layout reasonable?
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 14-Dec-21 6:43am.
|
|
|
|
|
Interesting mix of WPF and Windows Forms! And on point answer!
I studied your code a bit but then I realized one possible problem. It might not work if there are multiple Font in used on the longest line, like Courier 64 and Courier 12. Which is the point of a RichTextBox after all.
Which bring me back to just working with plain old TextBox . I can give them a custom Font too, and bold and italic and etc..., just a single font (or other properties) for the whole text.
The RichTextBox is not really hiding the possible width of the rich text (which would be "easy" to circumvent), it's not calculating it, and there doesn't seem to be any easy way to measure a FlowDocument that I could find...
Unless, could there be an easy way to convert FlowDocument to FormattedText ? Mmm... perhaps I should google that...
As to the "too much text" issue, it's not my problem, it's the user problem. Worst case scenario they can use a few line return.
The view has builtin zoom and scroll, so that help too.
Additionally I'd like to keep the text centered on the target square as the text is entered / updated (though it could be dragged with the appropriate tool later)
I need to move on with (for now) plain text. And on a much later day I might spend some.. weeks making a custom text editor.. haha
modified 14-Dec-21 7:44am.
|
|
|
|
|
Why would you allow font changes in a box that grows as you type? That's just asking for trouble. Besides, you said you'd be willing to use a regular TextBox if need be. That implied to me that you weren't concerned with font changes.
You can't specify a requirement, and then move the goal posts after I've kicked a field goal.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
well... I just did...
What I would like in order of priority:
1. I need text
2. I need to know the size of the text live as it updates
4. (preferable)no page size (i.e. no max line length)
3. (bonus, optional) rich text would be nice
so I started with RichTextBox, thought it would be great. But then, pfff.. too much bother, could I at least get some text going, I realy would like to have some marker on my dungeon map ASAP.. the RichText could always come later, if ever....
|
|
|
|
|
I'm still having trouble with the growing nature of the textbox. I honestly can't come up with a use case for that, especially since a text box can horizontally scroll (without scrollbars), but hey, it's your app...
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Obviously, and I understand that it can have been confusing of me to forget that, scrolling will be disabled / scrollbar will be removed.
But Anyway TextBox does (almost) all I need (yes, including the resize, see, it's not so hard)
|
|
|
|
|
Forget about LOB applications. Imagine there is a (zoomable, scrollable, pannable, rotatable) drawing area.
Now when the user click somewhere multiple things could happen (like, say, drawing a bezier curve), one of them is adding some text centered around said point. Which start by typing said text centered at point inline, ie. wysiwig editing. With the box around the text, previewing textblock handles, for later manipulation, such as rotation or translation (as in displacement)
modified 15-Dec-21 6:35am.
|
|
|
|
|
I just thought of a use case - when you're doing an app like Photoshop, that allows you to specify text objects. However, even those objects only grow to the extent needed, and properly process return/linefeed, and don't show scroll bars at all.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Yeah, well, mmm.. isn't what I just said?
Anyway, this is exactly what I am doing!
And, in other news, I got the text editor just appearing on my map, finally!
|
|
|
|
|
Actually.. you gave me another idea!
I am just going to prevent (or try to) any text longer than arbitrary length, say 256 characters. That might prevent some grief...
Oh.. look at that, TextBox.MaxLength !
|
|
|
|
|
Where can I find an up-to-date library or set of wpf converters?
I have also found that System.Windows.Controls contains 3 convertors. The closest I can find to a set.
I did a query wpf converters github - Google Search[^]
It found several candidates but they are quite dated.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
In over 10 years, I have yet the use a "converter"; unless it was my own code in a setter / getter. Your question make it sound like a necessity.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
It is a feature of binding. I know some people have never used binding. I use it but would not recommend it for the faint of heart. When you have a good set of converters, binding is much more useful. Still very difficult to debug.
Do you know about Behaviors? Another obscure and little used feature.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
When I first learned PL/I, I made it my mission to use every possible language feature and function; whether it needed it or not.
My apps "bind" and they animate all the time; all without "convertors" and "behaviors". I just work closer to the metal.
I would suggest looking at the Community Toolkits for WPF / UWP though.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
If you want to be close to the metal then you would do everything in assembly.
When the IBM PC first came out there was quite a discussion about the pros and cons of using assembly. Out of this the one thing I remember was the story about the cowboy that liked to chew on metal nails. Do you remember it?
Someone asked a cowboy why he like to chew on nails. He answered, when I look in a mirror and see all my broken and cracked teeth, I look tough. When it pokes me and cuts me in the mouth, the pain makes me feel tough. When there is blood running down the corner of my mouth others will know I am tough.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
Converters and behaviors are another layer of indirection; they don't make life easier.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I sometimes think binding is a new form of spaghetti code. Converters are kind of like the meatballs. All covered in sauce, a kind of slippery connection. Binding and converters are a great idea but are almost rendered useless by the lack of debugging tools.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
|
A different kind of converter. This is about the old Converter dialog box. Not the binding converters.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
So I must reveal my ignorance ... I never learned about "binding converters".
How does a "binding converter" work? What does it do? How do you use it?
|
|
|
|
|
This is a very glossy overview
I assume you know what binding is since you are watching this forum on WPF. Let’s say you want to bind a bool to visibility. Bool has only 2 states, true and false. Visibility has 3 states, Visible, Hidden, and Collapsed. (Ignoring null). A binding converter will convert bool to visibility.
As stated, before binding and converters are not for the faint of heart. This link will get you started, barely.
Value conversion with IValueConverter - The complete WPF tutorial[^]
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
OK, I have used and written that kind of converters. I just didn't trigger on the name "binding converter". I though it was some way of converting the binding as such, not the value type!
|
|
|
|
|
michaelbarb wrote: they are quite dated
Really? The first result[^] was updated two years ago. WPF hasn't changed that much since 2006.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I've always written my own converters. I have about a dozen or so. I have a bool-to-visibility converter that can be configured (in XAML) to set the visibility to any of the three possible values according to the bool value (just as an for instance).
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|