I needed to add a trackbar to my menu as a
ToolStripMenuItem
but Visual Studio doesn't allow that by default. I tried to add it manually on
Designer.cs
and I managed to do it. However, it wasn't functional. Because
TrackBar
was locked up inside the toolstrip item without exposing any of its properties and events.
So I found this code and added it to my form:
[System.ComponentModel.DesignerCategory("code")]
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ContextMenuStrip | ToolStripItemDesignerAvailability.MenuStrip)]
public partial class ToolStripMenuItem : ToolStripControlHost
{
public ToolStripMenuItem()
: base(CreateControlInstance())
{
}
public TrackBar TrackBar
{
get
{
return Control as TrackBar;
}
}
private static Control CreateControlInstance()
{
TrackBar t = new TrackBar();
t.AutoSize = false;
return t;
}
[DefaultValue(0)]
public int Value
{
get { return TrackBar.Value; }
set { TrackBar.Value = value; }
}
protected override void OnSubscribeControlEvents(Control control)
{
base.OnSubscribeControlEvents(control);
TrackBar trackBar = control as TrackBar;
trackBar.ValueChanged += new EventHandler(trackBar_ValueChanged);
}
protected override void OnUnsubscribeControlEvents(Control control)
{
base.OnUnsubscribeControlEvents(control);
TrackBar trackBar = control as TrackBar;
trackBar.ValueChanged -= new EventHandler(trackBar_ValueChanged);
}
void trackBar_ValueChanged(object sender, EventArgs e)
{
if (this.ValueChanged != null)
{
ValueChanged(sender, e);
}
}
public event EventHandler ValueChanged;
protected override Size DefaultSize
{
get
{
return new Size(200, 16);
}
}
}
Now I can add
TrackBar
from designer just like other regular items.
Example usage:
private void ToolStripMenuItem1_ValueChanged(object sender, EventArgs e)
{
int valueB = ToolStripMenuItem1.Value;
pictureBox2.Image = ChangeB(new Bitmap(pictureBox1.Image), valueB);
}
P.S.: This is my first post here.