Adding scrollbars to UserControl - c#

I am trying to add vertical and horizontal scrollbars to my UserControl with the HorizontalScroll and VerticalScroll properties, but I am having extreme issues. My problem arises when I drag or manipulate the scroll box on the bar. When I let it go, it simply jumps back to the start position!
I know of the AutoScroll property, but I do not want to use it since I want to be able to control every aspect of my scrollbars, and I don't want it to be done automatically. Also, according to the documentation, AutoScroll is for "[enabling] the user to scroll to any controls placed outside of its visible boundaries" which isn't what I want. I just want scrollbars.
...aaand I suppose I could add VScrollBar and HScrollBar to the control, but why should I do this when the scroll functionality already exists? Seems like a waste to me.

Set the AutoScrollMinSize property.
If you implemented the OnPaint() override then you'll need to use the AutoScrollPosition property to set the arguments for e.Graphics.TranslateTransform().

Related

How to change the back color of the scrollbar of a panel?

So I am making a dark mode option for my application and I want the scrollbar back color to also change color so that it doesn't look out of place. I have tried to search for a solution but so far I have only found code for a scrollbar as in the control. but I need to change the scrollbar of a panel. Does someone happen to know how to do this? Thanks a lot in advance.
I faced the same challenge when I started to work on custom controls that should support the Dark Mode. The problem with Panel control as well as other controls is that their ScrollBars are managed internally by those controls and there is no way to customize them.
My Idea was to create a custom ScrollBar control that supports custom colors and themes, see my answer https://stackoverflow.com/a/73613059/5514131
And in our custom Panel control, we create the custom ScrollBars internally and use the various Panel properties and events to link the custom ScrollBars to our Panel.
With the help of the Panel VerticalScroll and HorizontalScroll properties, we can know whether the default Panel scrollbars are visible or not and their properties to copy to our custom ScrollBars.
With the help of LocationChanged, SizeChanged, and other Panel events and properties we should bind the custom ScrollBars to our Panel and place them on top of the default ones to completely cover them.
I used the OnMouseWheel and OnScroll to update our custom ScrollBars when the Panel is scrolled using the code or mouse wheel.
We should dispose the custom ScrollBars when the Panel is disposed, or its handle is destroyed.
I know this isn't the best approach, but it should work to prevent the default ugly scrollbars from ruining your beautiful Dark Mode.
This workaround can be applied to other controls, I actually used it with TabControls, however, it will be more complex to implement.
Windows Forms Panel control with custom ScrollBars based on the Flat ScrollBar control https://gist.github.com/ahmedosama007/39f8b76e65300e5969110b753fe0a654

How to stop VScrollbar from flashing when selected

I have a System.Windows.Forms.VScrollBar control on my form and select it programmatically by calling scrollBar.Select() so that mouse-wheel and keyboard scrolling just works (without the user having to explicitly select the scrollbar beforehand). However, this causes the scrollbar cursor to constantly flash every half a second or so. Is there any way to stop this behaviour? I have looked around but I can't find any property to control this behaviour?
I even tried creating a custom scrollbar inherting from VScrollBar and overriding OnPaint, but that's not even getting called, so I guess its not used by VScrollBar at all.
I don't know that keyboard scrolling can be easily implemented on a WinForms AutoScroll control (see this answer). I do know that so long as the AutoScroll control has focus in the form, mouse-wheel scrolling should work without the user selecting the scrollbar itself. I suggest that rather than calling scrollBar.Select() to accomplish what you need, you instead call scrollBarParent.Focus(). This should take care of the flashing issue.

C# FlowLayoutPanel scrollBar

I'm making an instant messaging application in C#. The problem that I'm facing right now is that for the contacts list I've made a custom control extendinguserControl, which contains aFlowLayoutPanel`.
That panel will contain a list of userControls. I want to customize the VscrollBar, but no chance (not possible). Instead I have this genius idea to hide the VscrollBar from the FlowLayoutPanel, and make simple buttons (UP and down events). For this everything worked like a charm, but when I tried to hide the VscrollBar by making the property autoScroll = false , the buttons stopped working.
How can I hide the VscrollBar?
If you want to hide the the vertical bar, there are some possible solutions. ..
You could make an event for resize, controls add, controls remove and set all the child controls' width to flowlayoutpanel.width -20
You could add a panel to the flowlayoutpanel and set it to autosize and make the panels may width to flowlayoutpanel.width-20.
You could check if the width of the flowlayoutpanel is bigger than its real width (means vscrollbar appeared) , and resize the children that it'll/ld hide again
and if you are sure that your controls are smaller than the flowlayoutpanel's width, you simply could create a panel which covers the vertical bar. (use .BringToFront() to put it before the flowlayoutpanel's scrollbar)
I hope that I understood and perhaps have solved the problem

Scrollable Form in c#, AutoScroll=true doesn't work

What are the rules which I have to respect to make the Form scrollable...
I simple set the Property AutoScroll to true.
I also tried while Auto Scroll is true, to set AutoSize to true/false, but none of these worked... also tried to put Panel and added all components in there... still nothing...
Maybe using V or HScrollBar can help, but i really don't know how to link it with the Form...
form.AutoScroll = true;
formMainLayout.AutoScroll = true;
rootPanel.AutoScroll = true;
The content controls the scrolling. The scrollbars do not appear unless they are needed. Usually, there is a property available that you can set to force them to be visible always, and simply disabled until needed.
The AutoScroll property must be true, as you have already found. But then the content of the scrollable control must force the parent control to display the scrollbars. This part is up to how the controls are embedded within the parent.
Try these two experiments:
Place a Panel on your form and dock it to Fill. Set the AutoScroll property of the Panel to true. Into that panel, place a TextBox and set it to dock as Fill as well. Also set MultiLine to true. Run the application, and you will notice that the size of both is simply using the available space...no scrolling can occur because neither the Panel, nor its TextBox become larger than the space they occupy.
Perform the same steps as in #1, but this time, do not dock the TextBox. Instead, set it to a large size, something that you know will be larger than the amount of Panel that is visible. Running the application should now produce a scrolling Panel.
Hopefully this little test helps to demonstrate what is controlling the scroll on a form.
I was also having the same problem, I managed to fix it...
All the child controls inside the panel had a Left & Right anchor, and when I only set the anchor to Top, the scrollbars where working fine.
I am not sure as to why the Left and Right anchor (of the child controls) forces the panel not to show scrollbars.
But anyways... hope this will help anyone as of this date.
The AutoScroll property should work fine, but most likely you are not using it right: the bar appears only when required. Example: minimum Y of the Form is 0 and minimum Y of one of the controls in it (a TextBox) is -20.
If you want to include a scroll bar no matter what (controls inside the boundaries of the form or not), you can also do it. Sample code (from MSDN) for a vertical scroll bar:
// Create and initialize a VScrollBar.
VScrollBar vScrollBar1 = new VScrollBar();
// Dock the scroll bar to the right side of the form.
vScrollBar1.Dock = DockStyle.Right;
// Add the scroll bar to the form.
Controls.Add(vScrollBar1);
You need to set the properties for the parent panel.
Dock = Fill
Anchor = Top, Left
AutoScroll = true
That's it. Good luck! ^^
note its for vertical scroll
Turn On auto scroll property of your Form. insert one panel and
set panel width to the form width and panel height
equal to length of your total content or may be 1300 or 1500 as
required.
Place panel location as you want set panel anchor
property to top. place your all
content inside panel.
hope it will solve your problem
I had the same problem.
You have to add only this:
this.AdjustFormScrollbars(true);

Changing textbox text causes parent windows to resize incorrecly

I've tried looking many places for an answer to an issue I'm having and so far I've found nothing.
What I currently have is a c# windows form with user controls inside it. Some user controls have other controls inside them. What happens when I change the text in a textbox, is its parent windows will no longer resize like they should when changing the window size. i.e. A horizontal scrollbar will appear even though horizontal scrollbars are disabled in that specific window. Its almost as if changing the text changes the parent window's styling.
In case this is too vague, I have a textbox inside a panel with a docking property set to fill. The panel has a padding of 10 in order to allow the textbox to have some white space for aesthetic purposes. This control resides within a parent control (we'll call it parent 1), which in turn resides within another control as well (we'll call it parent 2). So when I change the textbox's text (at all, even adding a space), will then make parent 2 have a horizontal scrollbar flicker and sometimes even remain when resizing the form window manually.
You should make sure that not only the TextBox in the UserControl is docked to fill but also that the user control itself and its parent (and its parent) are Docked correctly or have anchor set so that they resize with the Form.
Do you execute any special code when the user enters a character? (KeyPressed event etc.). If yes you should try disabling the events temporarily to see if they cause the problem.
If you post a sample of your code it would be easier to help. Without this we can only guess, like I tried...
I found out my issue! When using autoScrollBars and double buffering, it caused the horizontal scrollbar to show when it shouldn't have (at least in my case) when resizing the window. The answer was simple, forget the autoScrollBars, and implement my own vertical scroll bar!
I was actually getting some code to post up on here for you guys to look at, but when looking at it, I decided to forget the autoscroll, and lo and behold it worked!
I'm actually curious as to why that is though. My friend heard that .net has some issues with autoScroll but I didn't think it would be to this degree.

Categories

Resources