Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I'm making a simple UI system for a small game that is going to be made using Windows Forms and drawn on a PictureBox.

At the moment, i'm having some difficulty to implement a Scrollbar and need some help to solve two major problems as described below:

. Calculating the "Value" property of the Scrollbar when the current position of the Thumb on the Track is changed (maybe using the ThumbStep?).

. Calculating the "Minimum" and "Maximum" properties.

Some specifications:

. The "Value" property is used to scroll the Content.

. I want the "Value" property to behave similar to the one in Windows Forms as described in this link: ScrollBar.Value Property (System.Windows.Forms) | Microsoft Docs[^]

. The "Minimum" and "Maximum" should also behave similar to the ones in Windows Forms as described in these links:
ScrollBar.Minimum Property (System.Windows.Forms) | Microsoft Docs[^]
ScrollBar.Maximum Property (System.Windows.Forms) | Microsoft Docs[^]

. The UI is using Rectangles, where you can find in this link: Rectangle Struct (System.Drawing) | Microsoft Docs[^]

Below is part of the code that i currently have, please, look at the commented blocks.

C#
public int Minimum  { get; set; }
public int Maximum  { get; set; }

public int ThumbStep { get; set; }
public int Value     { get; set; }

public void Update()
{
	// Calculate the Scrollbar Thumb height:
	var viewable_ratio = ViewportHeight / ContentHeight;
	var scrollbar_area = ViewportHeight - ( ArrowHeight * 2 );
	
	this.ThumbHeight = scrollbar_area * viewable_ratio;
	
	// Calculate the Scrollbar Thumb step:
	var track_space = ContentHeight  - ViewportHeight;
	var thumb_space = ViewportHeight - ThumbHeight;
	
	this.ThumbStep = track_space / thumb_space;
	
	// TODO: Calculate the "Minimum" and "Maximum" values.
	this.Minimum = ???
	this.Maximum = ???
	
	Point mouse = Input.MousePosition();
	
	if(Thumb.Rectangle.Contains(mouse) && Input.MouseDown(0))
	{
		int x = Thumb.Position.x;
		int y = ( mouse.y - Thumb.Position.y );
		
		Thumb.Position = new Point(x, y);
		
		if(Thumb.Position.y < Minimum) Thumb.Position.y = Minimum;
		if(Thumb.Position.y > Maximum) Thumb.Position.y = Maximum;
		
		// TODO: Set the "Value" property to the correct value when the Thumb is moved.
		this.Value = ???
        
        content.Y += this.value;
	}
}


Thanks in advance!

What I have tried:

I found this code on the internet and edited it to fit my project, but i'm having issues
to calculate these properties.

I'll test the code in DotNetFiddle when i find a answer to both of the problems as i don't have a PC at the moment.
Posted
Updated 16-Aug-21 14:45pm
v22
Comments
Richard MacCutchan 16-Aug-21 8:09am    
"I found this code on the internet"
Not a good idea unless you fully understand it. The .NET framework contains plenty of controls that manage the scrolling automatically.
strangerick 16-Aug-21 8:17am    
Hello, i do understand some of the code and i posted here for people to help me with the parts i don't understand, also i'm implementing this UI system for practice and i don't want to use the .NET controls.
Could you please help me with this problem?
Richard MacCutchan 16-Aug-21 8:21am    
What problem? Until you can install that code on a PC and compile it you do not know what problems there are.
strangerick 16-Aug-21 8:24am    
The problem is i don't know how to calculate the Value and Minimum and Maximum properties as i described, i also described their role in this code at the end of the post and i'l test it in the DotNetFiddle when i finish the code.
Richard MacCutchan 16-Aug-21 8:35am    
All values are calculate from the size of the viewport and content that you are trying to scroll. There are plenty of examples of such code that Google will find.

Take a look at this CodeProject article: Custom Drawn Scrollbar[^]
 
Share this answer
 
Comments
strangerick 16-Aug-21 8:43am    
Thanks for the commend @RickZeeland, but this does not solve my problem directly as the only thing left for me to do is to calculate the Value, Minimum and Maximum properties as i described them in the original post. Also, as i stated in the other comments, i don't want to use .NET controls in this project

Edit: I already looked in the article you posted and also the link to the custom skinned Scrollbar he linked.
Why are you reinventing the wheel?
.NET has loads of controls that add toolbars as needed, as well as providing the ScrollBar Class (System.Windows.Forms) | Microsoft Docs[^] as a stand alone scrollbar ...
 
Share this answer
 
Comments
strangerick 16-Aug-21 8:34am    
@OriginalGriff Thanks for the comment but i'm creating this UI system in my game for practice and i don't want to use the .NET controls in this project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900