Krypton resizing issue
Scenario
I need help with the KryptonSeparator .
I would like to use the separator in the image below to change the width of the left and right controls:
Problem
The problem is that when I try to move the separator to the left, it creates a very disturbing visual effect, and more or less the same happens when I move the separator to the right and to the left is much more noticeable (and horrible):
I think I am not using eventargs correctly KryptonSeparator
, because when I move the separator to the left, I base the calculations using the separator's width instead of the event data (because I don't know how to do this correctly).
Question
What changes should I make in my code to fix the resizing issue?
code
Both left and right controls have an assigned property MinimumSize
, I am trying to stop the resize if MinimumSize.Width
reached.
This is the source code in VB.Net:
''' <summary>
''' Handles the SplitterMoving event of the KryptonSeparator1 control.
''' </summary>
Private Sub KryptonSeparator1_SplitterMoving(ByVal sender As Object, ByVal e As SplitterCancelEventArgs) _
Handles KryptonSeparator1.SplitterMoving
Dim separator As KryptonSeparator = DirectCast(sender, KryptonSeparator)
Dim leftCtrl As Control = Control1
Dim rightCtrl As Control = Control2
If (e.MouseCursorX > 0) _
AndAlso Not ((rightCtrl.Size.Width - e.MouseCursorX) < rightCtrl.MinimumSize.Width) Then
separator.Location = New Point(separator.Location.X + e.MouseCursorX, separator.Location.Y)
leftCtrl.Width += e.MouseCursorX
rightCtrl.Width -= e.MouseCursorX
rightCtrl.Left = separator.Right
ElseIf (e.MouseCursorX < 0) _
AndAlso Not ((leftCtrl.Size.Width + e.MouseCursorX - separator.Width) < leftCtrl.MinimumSize.Width) Then
separator.Location = New Point(separator.Location.X - separator.Width, separator.Location.Y)
leftCtrl.Width -= separator.Width
rightCtrl.Width += separator.Width
rightCtrl.Left = separator.Right
End If
End Sub
This is the C # source code:
/// Handles the SplitterMoving event of the KryptonSeparator1 control.
/// </summary>
private void KryptonSeparator1_SplitterMoving(object sender, SplitterCancelEventArgs e)
{
KryptonSeparator separator = (KryptonSeparator)sender;
FolderView leftCtrl = this.FolderView_Files;
KryptonListBox rightCtrl = this.KryptonListBox_Files;
if ((e.MouseCursorX > 0) && !((rightCtrl.Size.Width - e.MouseCursorX) < rightCtrl.MinimumSize.Width)) {
separator.Location = new Point(separator.Location.X + e.MouseCursorX, separator.Location.Y);
leftCtrl.Width += e.MouseCursorX;
rightCtrl.Width -= e.MouseCursorX;
rightCtrl.Left = separator.Right;
} else if ((e.MouseCursorX < 0) && !((leftCtrl.Size.Width + e.MouseCursorX - separator.Width) < leftCtrl.MinimumSize.Width)) {
separator.Location = new Point(separator.Location.X - separator.Width, separator.Location.Y);
leftCtrl.Width -= separator.Width;
rightCtrl.Width += separator.Width;
rightCtrl.Left = separator.Right;
}
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================
UPDATE
I have updated the codes above to make it easier to read and I am sharing this new video where you can see the design problem:
source to share
First, you need to get where the user clicked before dragging mouse down event of splitter control
and total width
from three controls:
Private mouse_Down As Point //you can use an integer also because y coordinate remains the same
Private totalWidth As Integer
//mouse down event
mouse_Down.X = e.MouseCursorX
totalWidth = seperator.Width + LeftControl.Width + RightControl.Width
Private Sub KryptonSeparator1_SplitterMoving(ByVal sender As Object, ByVal e As SplitterCancelEventArgs) Handles KryptonSeparator1.SplitterMoving
Dim separator As KryptonSeparator = DirectCast(sender, KryptonSeparator)
Dim leftCtrl As Control = Control1
Dim rightCtrl As Control = Control2
Dim leftWidth, rightWidth As Integer
leftWidth = leftCtrl.Width + (e.MouseCursorX - mouse_Down.X)
rightWidth = rightCtrl.Width - (e.MouseCursorX - mouse_Down.X)
If leftWidth <= leftCtrl.MinimumSize.Width Then
leftCtrl.Width = leftCtrl.MinimumSize.Width
separator.Left = leftCtrl.Left + leftCtrl.MinimumSize.Width
rightCtrl.Left = leftCtrl.Left + leftCtrl.MinimumSize.Width + separator.Width
rightCtrl.Width = totalWidth - leftCtrl.MinimumSize.Width - separator.Width
Return
End If
If rightWidth <= rightCtrl.MinimumSize.Width Then
leftCtrl.Width = totalWidth - rightCtrl.MinimumSize.Width - separator.Width
separator.Left = leftCtrl.Left + leftCtrl.Width
rightCtrl.Left = leftCtrl.Left + leftCtrl.Width + separator.Width
rightCtrl.Width = rightCtrl.MinimumSize.Width
Return
End If
separator.Left += (e.MouseCursorX - mouse_Down.X)
leftCtrl.Width = leftWidth
rightCtrl.Width = rightWidth
rightCtrl.Left = leftCtrl.Left + leftWidth + separator.Width
End Sub
EDIT
Try the following:
//mouse down event //mouse_Down.X = e.MouseCursorX mouse_Down.X = MousePosition.X mouse_Down.Y = MousePosition.Y mouse_Down = seperator.PointToClient(mouse_Down) totalWidth = seperator.Width + LeftControl.Width + RightControl.Width
and SplitterMoving
:
Dim leftWidth, rightWidth As Integer
Dim pnt As Point
pnt.X = MousePosition.X
pnt.Y = MousePosition.Y
pnt = seperator.PointToClient(pnt)
//replace e.MouseCursorX with pnt.X
...
EDIT 2
There are two minor bugs in your logic for resizing two windows:
- Using
e.MouseCursorX
to determine the direction of resizing (left or right) is incorrect, for example, you move the cursor to the left (left) while staying inside the separator,e.MouseCursorX
is still positive, so you resize to the right (until, of course, e.MouseCursorX becomes negative) instead of left ! - Your code checks for the minimum size, but does nothing when the comparison is false, which means the resulting size of the control is smaller. When this happens, you need to set the size of the control, for example, let's say the minimum size is 50 and the size of controls is 55. If the size changes very quickly, the resulting size of the control may become 49. Your code does nothing (comparison incorrect) and the size of the control remains 55 instead of setting it to 50.
My solution addresses both of these situations. However, the real problem with your terrible effect, as you said, is not these two errors. In fact, the application response is too slow when resizing controls. To be more specific when you resize and move the control to the right (ListBox_Files). You can check it yourself if you drag and drop a small number (1 or 2) files and see the result. This is an amazing difference. This, unfortunately, tells me that there is nothing you can do about it. You need to change your sizing logic. Two solutions:
- Use a single control and custom graphic, text, icons, vertical-horizontal scrollbars, etc. (Not recommended!)
- Make a visual studio and other app. Don't resize the controls until you release the button. Just show a vertical line:
source to share