Introduction
These simple bits of code will show you how to allow your users to resize a
label at runtime. Simple paste this code into an empty form (below the windows
generated stuff) and draw a label (called Label1) anywhere onto the form.
Run the program and notice the cursor change when your mouse is over the
right side of the label. Click and drag and watch the label resize. This can be
used for almost any control and the code can be easily changed to allow width
resizing as well.
Dim _mouseDown as boolean = false
Private Sub Label1_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
If _mouseDown = True Then
Label1.Width = e.X
Else
If e.X >= Label1.Width - 3 Then
Label1.Cursor = Cursors.VSplit
Else
Label1.Cursor = Me.Cursor
End If
End If
End Sub
Private Sub Label1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
_mouseDown = True
Label1.Cursor = Cursors.VSplit
End Sub
Private Sub Label1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
_mouseDown = False
Label1.Cursor = Me.Cursor
End Sub
| You must Sign In to use this message board. |
|
| | Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh) | FirstPrevNext |
|
 |
|
 |
Now Width and Height, both are resizable.
with this code,
bool _mouseDownForWidth = false , _mouseDownForHeight = false; const int _minLength = 35 , _minHeight = 20; private System.Windows.Forms.Button button1; const int _margin = 3; private void button1_mouseDown(object sender, MouseEventArgs e) { if (e.X >= button1.Width - _margin && e.X <= button1.Width + _margin) { _mouseDownForWidth = true; button1.Cursor = Cursors.SizeWE; } if (e.Y >= button1.Height - _margin && e.Y <= button1.Height + _margin) { _mouseDownForHeight = true; button1.Cursor = Cursors.SizeNS; } if (_mouseDownForWidth == true && _mouseDownForHeight == true) { button1.Cursor = Cursors.SizeNWSE; } }
private void button1_MouseMove(object sender, MouseEventArgs e) { if (_mouseDownForWidth == true) { button1.Width = (e.X < _minLength ? _minLength : e.X); } if (_mouseDownForHeight == true) { button1.Height = (e.Y < _minHeight ? _minHeight : e.Y); } if (_mouseDownForWidth == false && _mouseDownForHeight == false) { if (e.X >= button1.Width - _margin && e.Y >= button1.Height - _margin) { button1.Cursor = Cursors.SizeNWSE; } else { if (e.X >= button1.Width - _margin) button1.Cursor = Cursors.SizeWE; else { if (e.Y >= button1.Height - _margin) button1.Cursor = Cursors.SizeNS; else button1.Cursor = this.Cursor; } } } }
private void button1_MouseUp(object sender, MouseEventArgs e) { _mouseDownForWidth = false; _mouseDownForHeight = false; button1.Cursor = this.Cursor; }
"Today is mine, Tomorrow of no ones"
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
i m posting this, because it is very usefull, as i hv used it. and i can know its importance.
bool _mouseDown = false; const int _minLength = 35; const int _margin = 3; private void label1_MouseDown(object sender, MouseEventArgs e) { if (e.X >= label1.Width - _margin && e.X <= label1.Width + _margin) { _mouseDown = true; label1.Cursor = Cursors.VSplit; } }
private void label1_MouseMove(object sender, MouseEventArgs e) { if (_mouseDown == true) { label1.Width = (e.X < _minLength ? _minLength : e.X); } else { if (e.X >= label1.Width - _margin) label1.Cursor = Cursors.VSplit; else label1.Cursor = this.Cursor; } }
private void label1_MouseUp(object sender, MouseEventArgs e) { _mouseDown = false; label1.Cursor = this.Cursor; }
today is mine, tomorrow of no ones
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
General
News
Question
Answer
Joke
Rant
Admin