How to implement Vertical Scrollbar in C# Console Application - c#

I'm implementing an hex editor in the console. I have 2 "Windows" displayed, one for the hex numbers and one for the text. In between i want to visualize a scrollbar.
Here a photo for better understanding.
Now my question:
I'm, not quite sure how to calculate the number of segments of the scrollbar and on which segment I'm currently. I'm using a file stream holding the bytes. I'm always displaying the number of bytes the left window can show. If the user presses arrow up or down, I'm reading the next or previous line of the stream.
Do you have any ideas how to solve that well?
EDIT:
My Scrollbar class is very minimalistic at the moment: It holds:
int numberOfSegments
int currentSegment
Point position
The renderer knows then how to draw the scrollbar. Do you think that's enough for the Scrollbar class?

Related

C#: How can I save a portion of the Command Screen for later use?

I'm learning how to code C# from scratch. Right now, I'm learning how to build a scene on the command line and update it as necessary.
Part of what i want to do is to "scroll text as it is generated".
The scene is a square command line (Size 100 by 40) and outlined by a box of "#" characters. This is the canvas on which everything is presented.
As the user interacts via simple inputs, the program will provide textual feedback, like a text-based game.
However, unlike a normal text-based game, I cannot allow for the natural flow of the command line to move the canvas. In other words, if I allow for the command line to act naturally, then the canvas would slowly travel upwards until it's no longer visible.
I need a way to read whatever is already being presented on the screen and storing it on memory. Then, I can delete a portion of the screen an paste the previously copied information shifted upwards, making space for new information.
Here is a mockup of what I want to do:
Image 1: desirable outcome
My question is: Is there a way to "read" only a specific portion of the command space and store into memory?
I know you can store whatever is being printed at the moment of printing, or I could keep track of a certain number of previously displayed information and print it again, but I would like to "copy, then paste shifted" a portion of the screen.
PS: This is what I'm trying to avoid
Image 2: Undesirable outcome
Since the top line needs to be moved too, really the only way to do this is to clear the screen and redraw everything. Instead of printing directly to the screen, save the new line to a list, clear the screen, then print that list to the screen, together with the border.
While I haven't found a way to store a portion of the command line to a variable for later use, I've succesfully "scrolled" the text, and found a workaround to "store" it.
Simply put, Console.MoveBufferArea() allows one to select a portion of the command line buffer and move it somewhere else on the buffer.
This way I can:
-Clear whatever space i need at the top by moving the cursor to, say, (1,1) and writing a box of spaces via a string.
-Console.MoveBofferArea() the portion of the buffer a few rows upwards
-Write the new text at the bottom
As a byproduct, i can also "store" a portion of the screen for late use by:
-Setting the buffer to a wider area with Console.SetBufferSize()
-Use Console.MoveBufferArea() to the buffer portion that is outside of normal view
-Do whatever i need to do on the screen
-Use Console.MoveBufferArea() to retrieve the "stored" portion back to where i need it
-Reduce the buffer area (I could also just leave it be)

How to fill part of the graphical figure

So, I have some simple figure (circle, ellipse, rectangle of closed polygon) and I need to fill only part of it.
I mean, I have GraphicsPath or similar object (set of points) and now I can simply fill the whole figure with, for example, orange color.
What I need: user enters 25% and I fill only 25% of the figure, starting from some side (top/bottom).
Maybe, it will be nessesary to find some sub-figure or (bad idea, I know) check all the points in on the field and fill them (only those which inside figure) one by one untill their amount will be 1/4 from the area of the figure. But it won't be so fast especially when image is about 5-6000 pixels from one side.
Here's sample what I have now and what I need for 25%. Important: instead of 25% can be any value.
Project: C# .net 3.5 WinForms
UPD:
Basic usecase.
User draws a figure (circle, ellipse, rectangle, polygon)
User enters value from 0 to 100 (percents)
I fill figure from bottom to the top until I filled amount of area (!), which is equal to user's value
I'm open for any ideas even without code.

Get the position of the GanttPresenterSplitter in a <telerik:RadGanttView>

I am working on a WPF application using Telerik and coded in C# .NET.
My following question goes over a XAML page using the model “Code behind” to implement the logic of the page.
I would like to get the position of the GanttPresenterSplitter in a telerik:RadGanttView.
This information would allow me to set the position of another element in another part of the same screen.
I would also know if it is possible to add an event handler on that GanttPresenterSplitter in order to get its position each time that line moves.
The ASP .NET page is split into 3 parts (please find in attachment draft_screen) :
Red square : telerik:RadGanttView
Green square : telerik:RadGridView
Blue square : telerik:RadScheduleView
As you can see, there is a dividing line between telerik:RadGridView and telerik:RadScheduleView.
I would like that line moves at the same time and same position as the GanttPresenterSplitter in the RadGanttView.
Using a more explicit picture based on an example (please find in attachment explicit_screen) :
Orange line : GanttPresenterSpliter
Purple line : GridSplitter
My point is to coordinate the orange and the purple lines in order they have the same position all the time.
Any help will be much appreciated :)
There is no straightforward way to get the position of the GanttPresenterSplitter, however, you could, for example, get the Width of the Grid area of the GanttView, and more specifically the width of the visible area. It is presented by a ScrollBar element, so you could subscribe to its SizeChanged event to handle when the Grid area is resized.
I have prepared a sample example to demonstrate the approach, please download the attachment and give it a try.

Find a character in a user drawn string at a given point

So I'm building a custom control in C# (not WPF), and I basically want to implement text highlighting with the mouse.
How do I efficiently find the character at a given Point (say where the mouse is clicked) in a string? I have the layout rectangle of the string as it was drawn and I could calculate the length of the string up to every character until I find the one closest to where the mouse is clicked... but there has to be a better way. Any suggestions?
If I had to do this, I would look at it backward.
I'd keep the text entered as a string member in the control, so at all times I know what is actually entered in the control (like the Text property in a TextBox).
Then I would use the TextRenderer.MeasureText() method (http://msdn.microsoft.com/en-us/library/7sy6awsb.aspx) and I would keep measuring the length of the string repeatedly until I pass the X coordinate of the mouse within the control, right then I know how many characters are chosen.
For example, assume the user has the text Hello written in the control.
And the X coordinate hit right between the l and the o, which could be of value 20.
Then I would repeatedly calling MeasureText() on the following strings:
H: width of 5 pixels.
He: width of 10 pixels.
Hel: width of 14 pixels.
Hell: width of 17 pixels.
Hello: width of 22 pixels.
Then I know the mouse was hit between the l and the o, so I would then highlight the text Hell.
Sorry for the distasteful example =)
UPDATE:
You can optimize this a bit by calculating the lengths in a binary-search-tree-like fashion.
Just like you would look up a name in the phonebook, you don't look page by page, but rather split in half as you go along, getting closer and closer until it's definitely between these two pages.
Similarly, especially for long string values of the control, calculate the width of the entire string, then half its length, and split there. I think that would be O(n log n) at that point.
Of course it would be O(1) if the text is of fixed width =)
Another thing you can do to build upon BeemerGuy's great suggestion is to precalculate an array of offsets. As the string is changed (the user types or the property is set in code) you can recalculate the offset array. That will save you the call to MeasureFont on the mouse clicks and will make finding the character trivial. You basically iterate the array until you find the nearest character. Since the offsets are implicitly sorted by value you can even use a binary search to make it more effective.

how to divide console screen in four parts and each part work separately using C# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
split a console in two parts for two outputs
I want to divide my console screen in four part and each part work separately,
I think in this we use thread but I am not getting how to use?
You can't just divide the console, but you have enough control over it to do it yourself.
See Console class reference. You can SetCursorPosition, set window position and size. Make your own method - something like WriteToArea(int area, string text). Track bounds of areas and wrap text to stay inside of area. Each area must have its own cursor position, track it too. And think how you will handle area overflow (or you need just four columns?)
Edit: I'm not going to give you fish, but here is your fishing rod ^_^
Get dimensions of console window (Console.WindowWidth) in columns
Divide that by four to get each area's width and calculate start and end column of each area. For example, if your console is 80 columns wide, you should get [0, 19], [20, 39], [40, 59], [60, 79] - these pairs are bounds of each area.
Next, store cursor position for each area (initially that would be [left bound, 0])
Implement method writeToArea(int area, String text):void. That's the hardest part. First, you SetCursorPosition into stored position for that area. Next, break text into parts that fit into area. Take into account how much space is left on current column. Write text with series of Console.Write, line by line. Then update cursor position for that area [CursorLeft, CursorTop].
Write, debug and have fun!
Well, theres libraries for this sort of thing. But you can't just split up the console window.
If you want more information about the library, check it out at sourceforge.

Categories