How to know the current visibility offset in TextEdit? - c#

When you write in a TexBox/TexEdit, if the text is larger than the size of the control, it is scrolled to the right.
How can I get the current visibility offset or scrolled length?
To illustrate what I want:
The black box represent the full text. The red box is the TextEdit, containing the visible part of the text. The brown vertical line is the caret. What I want to know is the offset measure in pixels.

I have done minimal testing on this, and it seems to work, though there are cases where it may be off by 1 pixel:
int offset = tb.GetPositionFromCharIndex(0).X;
I have put this in the TextChanged event handler for testing purposes.
Let me know if it does what you need.
Cheers
EDIT: I have played with adding the width of the border (defaulted to Fixed3D) by doing this instead:
int offset = tb.GetPositionFromCharIndex(0).X + SystemInformation.Border3DSize.Width;
This seems to give better results.

Related

Dynamic sized multi line TextBox field in a PDF

I have created a document in an open office with a multi-line form field:
The issue I am having is when the dynamic content exceeds the initial size of the multi-line text box:
Sure I can re-size the Text Box in the original template but the dynamic content may be from 1 to 50 lines and I want the text after the Text Box to be close to the last line of dynamic content.
Can someone suggest a way to solve this?
I have once coded a solution to set the width and height of a textbox programmatically based on the characters supplied. I think this was a school assignment a long time ago.
This can be done with both VBA in a code behind or probably with a macro even. Or with VB.net.
I don't have the code I used way back when, but basically determine the maximum width you are able to provide in character width, the preferred width. Determine the pixel requirement per character for that width. This becomes the textbox width.
Divide your total string character count by your preferred width character count. Round up, calculate the pixel height per character. And use this value times the rounded result for the textbox height. Dirty but it should work.
Any chance you can change to a label and set AutoSize to True? You can fix the width and let the height auto adjust. This should be done before converting to a pdf. In fact all of the sizing should be resovled before pdf conversion.
Another Down voter without a comment, should not be allowed.

Fill width of panel with rectangles(variable size)

I'm making a sorting algorithm vizualizer(Like Sound of Sorting without the Sound) But i stumbled upon a problem, and i can't seem to figure it out. I need to layout rectangles over the width of a panel(variable amount).
Image of the bars:
But when i get more bars, sometimes i will get a blank space because it won't fit:
How can i make it that some rectangles are a bit bigger than some others to make it fit? The way i calculate the width right now is like this:
barWidth = draw.Width / amount; //draw is the panel where i draw the rectangles on
Any help will be greatly appreciated!
I guess this is a rounding issue so I think you need to do some calulations:
totalBarWidth = sum of barWidths
blankSpace = draw.Width - totalBarWidth
numberOfBarsPerBlankSpacePixel = amount / blank space
Loop over the bars and add a pixel to the width of every numberOfBarsPerBlankSpacePixel bars.
Also don't forget to add the pixels to the x-axis location of the following bars.

C# Calculate the image scroll with scrollbar

I'm working a 2d tile-editor, used Winforms and XNA(for the map rendering) and i have a problem with the custom scrollbar.
I would like to know the formula for calculating the map scrolling depending the position value of my scrollbar.
Let me explain
Actually, i have a functional code but is not exact.
Here is my calculation (is the problem):
(Point Map.PixelMapSize = map size in pixel)
hScrollBarMap.Minimum = 0;
hScrollBarMap.Maximum = 100;
hScrollBarMap.LargeChange = hScrollBarMap.Size.Width * 100 /(Map.PixelMapSize.X);
mapScrollCalcul = ((hScrollBarMap.Value - hScrollBarMap.Minimum) * 100 / (hScrollBarMap.Maximum - hScrollBarMap.Minimum)) * Map.PixelMapSize.X / 100;
Sorry, I do not know how to explain. I found it by making a lot of tests...
But, the mapScrollCalcul is the final calcul to be applied to map position display.
What is the exact calculation for this ?
I hope you understand, I'm not speak English, just little understand.
But I understand you. :) (programmation language is universal)
Thank you for reading and, maybe, future responses.
You are overcomplicating things.
First, you are setting your LargeChange value relative to your image, not your view, and your scroll.Maximum relative to... what, exactly?
On MSDN, they say:
User interface guidelines suggest that the SmallChange and LargeChange
properties are set relative to the size of the view that the user
sees, not to the total size including the unseen part. For example, if
you have a picture box with scroll bars displaying a large image, the
SmallChange and LargeChange properties should be set relative to the
size of the picture box, not to the size of the image.
Now, lets assume that your image is 1000x1000 and your view is 100x100, and you are scrolling vertically. Then:
myVscroll.Minimum=0;
myVscroll.Maximum=image.Height; //1000
myVscroll.SmallChange = 10; // here: view.Height/10 or other value that makes scrolling look smooth
myVscroll.LargeChange = 100; // in this example, this is set to view.Height
And finally:
mapScrollCalcul = myVscroll.Value;

How can I predict a XAML Label's height on a Canvas

I have an application where I need to dynamically build the content to a Canvas. Everything works just fine, but I am a little unsure of how I can set the y coordinates for the labels in the safest way. For example, I need to add three labels that are essentially lines of text. In Java Swing or C# GDI I would just query the the font for the line height and add that value to the y coordinate of the drawText command.
This is my code.
double y = 0.0;
_line1.Content = "Line1";
_line1.SetValue(Canvas.TopProperty, y);
_line1.SetValue(Canvas.LeftProperty, 0.0);
CanvasChart.Children.Add(_line1);
double textHeight = _line1.Height;
y += textHeight;
_line2.Content = "Line2";
_line2.SetValue(Canvas.TopProperty, 0.0);
_line2.SetValue(Canvas.LeftProperty, y);
CanvasChart.Children.Add(_line2);
This does not work because _line1.Height does not seem to be set to anything useful at this point. I suppose it has not rendered yet. The above code is in the loaded event for the window. ActualHeight does not help either.
Most code that I've seen seems to just set them to a hard coded value. That I suppose looks right on the developer's display, and you just hope looks good at other resolutions/DPI. In Swing and GDI I always had the best results finding out exactly how many pixels a string will be rendered at and using this to offset the next line.
You must call the Measure method, specifying an infinite available size. This will update the DesiredSize of the control:
_line1.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
double textHeight = _line1.DesiredSize.Height;
Another easy way to achieve the desired effect is to put the labels in a StackPanel.
In Swing and GDI I always had the best results finding out exactly how many pixels a string will be rendered at and using this to offset the next line.
This is possible in WPF as well. The GlyphTypeface class provides the AdvanceWidths and AdvanceHeights properties for each character in a typeface. By using CharacterToGlyphMap, you can map a character to an index within the AdvanceHeights, and use that to determine the actual height of any character.
For a detailed example, see GlyphRun and So Forth.

Fit ColumnHeaders to ListView's width, this code needs an explanation and wonder if there is a standard way?

I found this code (a loop) which can fit the total width of all ColumnHeaders to the width of a listview (of course if the ListView's width is large enough, otherwise there will be a horizontal scrollbar displayed).
foreach(ColumnHeader ch in myListView.Columns){
ch.Width = -2;
}
I've just tried changing the value -2 to some other values and it didn't work. I need an explanation on this code, why does it have to be -2 and not any other?
By the way, if possible do you know any other (standard?) way to do this (fit the total width of all ColumnHeaders to the width of a ListView)?
Your help would be highly appreciated!
There is no automatic way to do this beyond what you have found. This is documented at http://msdn.microsoft.com/en-us/library/windows/desktop/bb761163(v=vs.85).aspx though you can't see the value for LVSCW_AUTOSIZE_USEHEADER, it is -2.
In order to do this yourself, you need to react to the ListView being resized, and calculate the widths yourself. There is an event for the resizing. Then you use listview.ClientSize.Width - SystemInformation.VerticalScrollBarWidth for the total width to account for the scrollbar.

Categories

Resources