|
Looks fairly simple to have the height automatically increase to accommodate the text:
c# - WPF:Auto-Expand RichTextBox - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
yes, yes, what about the width though? This is the width that I was looking for, as I tried to explain..
|
|
|
|
|
A Canvas works well when you need absolute positioning (e.g. game pieces). In other cases, it's the opposite of "fluent design". A Grid is better at relative positioning and using available space, dynamically.
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
|
|
|
|
|
- Canvas is absolutely the right choice here (long story, I made a screenshot to explain, just for you! )
- Even in a Grid, the problem remains, the Width of the RichTextBox is NOT determined by its content
modified 13-Dec-21 18:02pm.
|
|
|
|
|
- The position of the edit box is not dependent on the canvas since you can have "layers".
- Content does NOT "stretch" the edit box (that's why it has scroll bars); the "available space" does; depending on the alignment settings.
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
|
|
|
|
|
Gerry Schmitz wrote: The position of the edit box is not dependent on the canvas since you can have "layers".
No idea what you mean. Nor, obviously, did you guess correctly how this drawing surface work (I thought it would be obvious, I am obviously wrong )
Anyway, could we please stop wasting time?
How about wondering how to auto-width that instead?
<Grid>
<RichTextBlock HorizontalAlignment="Center" Width="0"/>
</Grid>
Gerry Schmitz wrote: Content does NOT "stretch" the edit box (that's why it has scroll bars); the "available space" does; depending on the alignment settings.
Indeed, that's why I gave up on the RTB and decided to use a TextBox instead, glad we agree.
modified 13-Dec-21 21:04pm.
|
|
|
|
|
Re: "wasting your time".
You show no code; there is nothing "obvious". Re: "Layers":
<Grid>
<Canvas />
<EditBox />
</Grid>
HorizontalAlignment="Center" only makes sense when you have an absolute Width; in all other cases it compresses the control. You should be using "Stretch".
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 should be obvious after I repeated it emphatically at least 6 times...
Why do I have even have to argue about that?
I guess there is nothing I can really say here, sigh...
I am not even using a Panel anyway... Those don't support adding DrawingVisual s...
Canvas is just the most well known approximation of my scenario.
modified 15-Dec-21 5:43am.
|
|
|
|
|
Fitting the width of a RTB does not make sense from a control POV - the RTB is designed to wrap text not extend itself (otherwise you would end up with 1 line of text disappearing off to the right of the screen ).
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
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.
|
|
|
|
|