Looking for C# Spin Control

  • Thread starter Thread starter Stewart Berman
  • Start date Start date
S

Stewart Berman

Is there a control equivalent to the old Spin control? The one with two hour heads and an optional
textbox in the middle? The closest I can find is the NumericUpDown control and it's not the same as
the Spin control. I just want a sizeable up/down arrow pair.
 
hi Stewart,

Stewart said:
Is there a control equivalent to the old Spin control? The one with two hour heads and an optional
textbox in the middle? The closest I can find is the NumericUpDown control and it's not the same as
the Spin control. I just want a sizeable up/down arrow pair.
You may google for

c# spin control site:codeproject.com

this gives you some hits.


mfG
--> stefan <--
 
Hello Stewart,

Thank you for using Microsoft Managed Newsgroup Service, my name is Zhi-Xin
Ye, it's my pleasure to work with you on this issue.

You can create an UserControl, put two buttons in it, one on the top and
the other on the bottom. Handle the Paint event of the buttons and use
ScrollBarRenderer.DrawArrowButton() method to draw arrows on the buttons.

For example:

========= Sample code for your information ==============

public class SpinControl : UserControl
{
private Button _UpButton;
private Button _DownButton;

//Button status 0:Normal, 1:Pressed; 2:Hot(Mouse Hover)
private int _UpButtonStatus = 0;
private int _DownButtonStatus = 0;

public SpinControl()
{
this._UpButton = new Button();
this._DownButton = new Button();
this._UpButton.Paint += new PaintEventHandler(UpButton_Paint);
this._DownButton.Paint += new PaintEventHandler(DownButton_Paint);

this._UpButton.MouseDown += new
MouseEventHandler(_UpButton_MouseDown);
this._UpButton.MouseUp += new MouseEventHandler(_UpButton_MouseUp);

this._DownButton.MouseDown += new
MouseEventHandler(_DownButton_MouseDown);
this._DownButton.MouseUp += new
MouseEventHandler(_DownButton_MouseUp);

this._UpButton.MouseEnter += new EventHandler(_UpButton_MouseEnter);
this._DownButton.MouseEnter += new
EventHandler(_DownButton_MouseEnter);

this._UpButton.MouseLeave += new EventHandler(_UpButton_MouseLeave);
this._DownButton.MouseLeave += new
EventHandler(_DownButton_MouseLeave);

this.AjustArrowButtonSize();
this.Controls.Add(this._UpButton);
this.Controls.Add(this._DownButton);
this.Width = 20;
}

void _UpButton_MouseLeave(object sender, EventArgs e)
{
_UpButtonStatus = 0;
}

void _UpButton_MouseUp(object sender, MouseEventArgs e)
{
_UpButtonStatus = 0;
}

void _UpButton_MouseDown(object sender, MouseEventArgs e)
{
_UpButtonStatus = 1;
}

void _UpButton_MouseEnter(object sender, EventArgs e)
{
_UpButtonStatus = 2;
}

void _DownButton_MouseUp(object sender, MouseEventArgs e)
{
_DownButtonStatus = 0;
}
void _DownButton_MouseLeave(object sender, EventArgs e)
{
_DownButtonStatus = 0;

}
void _DownButton_MouseDown(object sender, MouseEventArgs e)
{
_DownButtonStatus = 1;
}
void _DownButton_MouseEnter(object sender, EventArgs e)
{
_DownButtonStatus = 2;
}

protected override void OnResize(EventArgs e)
{
this.AjustArrowButtonSize();
base.OnResize(e);
}

private void AjustArrowButtonSize()
{
this._UpButton.Size = this.Size;
this._DownButton.Size = this.Size;
this._UpButton.Height /= 2;
this._DownButton.Height /= 2;
this._DownButton.Top = this._UpButton.Bottom;
}

void UpButton_Paint(object sender, PaintEventArgs e)
{
if (_UpButtonStatus == 0 )
{
ScrollBarRenderer.DrawArrowButton(e.Graphics,
this._UpButton.ClientRectangle,

System.Windows.Forms.VisualStyles.ScrollBarArrowButtonState.UpNormal);
}
else if (_UpButtonStatus == 1)
{
ScrollBarRenderer.DrawArrowButton(e.Graphics,
this._UpButton.ClientRectangle,

System.Windows.Forms.VisualStyles.ScrollBarArrowButtonState.UpPressed);
}
else
{
ScrollBarRenderer.DrawArrowButton(e.Graphics,
this._UpButton.ClientRectangle,

System.Windows.Forms.VisualStyles.ScrollBarArrowButtonState.UpHot);
}

}

void DownButton_Paint(object sender, PaintEventArgs e)
{
if (_DownButtonStatus == 0)
{
ScrollBarRenderer.DrawArrowButton(e.Graphics,
this._UpButton.ClientRectangle,

System.Windows.Forms.VisualStyles.ScrollBarArrowButtonState.DownNormal);
}
else if (_DownButtonStatus == 1)
{
ScrollBarRenderer.DrawArrowButton(e.Graphics,
this._UpButton.ClientRectangle,

System.Windows.Forms.VisualStyles.ScrollBarArrowButtonState.DownPressed);
}
else
{
ScrollBarRenderer.DrawArrowButton(e.Graphics,
this._UpButton.ClientRectangle,

System.Windows.Forms.VisualStyles.ScrollBarArrowButtonState.DownHot);
}
}
}

===========================

If you have any questions or concerns, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Zhi-Xin Ye said:
Hello Stewart,

Thank you for using Microsoft Managed Newsgroup Service, my name is
Zhi-Xin Ye, it's my pleasure to work with you on this issue.

You can create an UserControl, put two buttons in it, one on the top
and the other on the bottom. Handle the Paint event of the buttons
and use ScrollBarRenderer.DrawArrowButton() method to draw arrows on
the buttons.

For example:

========= Sample code for your information ==============

public class SpinControl : UserControl
{
private Button _UpButton;
private Button _DownButton;

//Button status 0:Normal, 1:Pressed; 2:Hot(Mouse Hover)
private int _UpButtonStatus = 0;
private int _DownButtonStatus = 0;

Why create a new de facto enum type without even naming the constants? You
already have the System.Windows.Forms.VisualStyles.ScrollBarArrowButtonState
enum which has exactly what you want, and you can avoid the if statements
later on.
 
Hi Ben,

You're right, it's more concise to use the ScrollBarArrowButtonState enum,
I somehow ignore that.

public class SpinControl : UserControl
{
private Button _UpButton;
private Button _DownButton;

private ScrollBarArrowButtonState _UpButtonStatus;
private ScrollBarArrowButtonState _DownButtonStatus;

public SpinControl()
{
this._UpButton = new Button();
this._DownButton = new Button();
this._UpButton.Paint += new PaintEventHandler(UpButton_Paint);
this._DownButton.Paint += new PaintEventHandler(DownButton_Paint);

this._UpButton.MouseDown += new
MouseEventHandler(_UpButton_MouseDown);
this._UpButton.MouseUp += new MouseEventHandler(_UpButton_MouseUp);

this._DownButton.MouseDown += new
MouseEventHandler(_DownButton_MouseDown);
this._DownButton.MouseUp += new
MouseEventHandler(_DownButton_MouseUp);

this._UpButton.MouseEnter += new EventHandler(_UpButton_MouseEnter);
this._DownButton.MouseEnter += new
EventHandler(_DownButton_MouseEnter);

this._UpButton.MouseLeave += new EventHandler(_UpButton_MouseLeave);
this._DownButton.MouseLeave += new
EventHandler(_DownButton_MouseLeave);

this.AjustArrowButtonSize();
this.Controls.Add(this._UpButton);
this.Controls.Add(this._DownButton);
this.Width = 20;
}

void _UpButton_MouseLeave(object sender, EventArgs e)
{
_UpButtonStatus = ScrollBarArrowButtonState.UpNormal;
}

void _UpButton_MouseUp(object sender, MouseEventArgs e)
{
_UpButtonStatus = ScrollBarArrowButtonState.UpNormal;
}

void _UpButton_MouseDown(object sender, MouseEventArgs e)
{
_UpButtonStatus = ScrollBarArrowButtonState.UpPressed;
}

void _UpButton_MouseEnter(object sender, EventArgs e)
{
_UpButtonStatus = ScrollBarArrowButtonState.UpHot;
}

void _DownButton_MouseUp(object sender, MouseEventArgs e)
{
_DownButtonStatus = ScrollBarArrowButtonState.DownNormal;
}
void _DownButton_MouseLeave(object sender, EventArgs e)
{
_DownButtonStatus = ScrollBarArrowButtonState.DownNormal;

}
void _DownButton_MouseDown(object sender, MouseEventArgs e)
{
_DownButtonStatus = ScrollBarArrowButtonState.DownPressed;
}
void _DownButton_MouseEnter(object sender, EventArgs e)
{
_DownButtonStatus = ScrollBarArrowButtonState.DownHot;
}

protected override void OnResize(EventArgs e)
{
this.AjustArrowButtonSize();
base.OnResize(e);
}

private void AjustArrowButtonSize()
{
this._UpButton.Size = this.Size;
this._DownButton.Size = this.Size;
this._UpButton.Height /= 2;
this._DownButton.Height /= 2;
this._DownButton.Top = this._UpButton.Bottom;
}

void UpButton_Paint(object sender, PaintEventArgs e)
{
ScrollBarRenderer.DrawArrowButton(e.Graphics,
this._UpButton.ClientRectangle, _UpButtonStatus);
}

void DownButton_Paint(object sender, PaintEventArgs e)
{
ScrollBarRenderer.DrawArrowButton(e.Graphics,
this._UpButton.ClientRectangle, _DownButtonStatus);
}
}


Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Stewart,

How about this issue now? Does my reply make sense to you?
If you still need any help or have any concern, please feel free to
feedback, thanks.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
It makes sense. However, I already rolled my own ArrowButton and SpinButton controls.

The ArrowButton restricts the "clickable" area to the area inside the arrow -- not the rectangle it
is panted in. It also has a "Faces" property so I can use it as up, down, left or right.

The SpinButton uses two ArrowButton controls and a Label.
 
Back
Top