Create a custom eventargs to make the eventhandling simpler.
Any code interested in the change in the tractbar is very likely interested in its value....
public class TrackBarEventArgs: EventArgs
{
public int NewValue {get;set;}
}
[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 += trackBar_ValueChanged;
}
protected override void OnUnsubscribeControlEvents(Control control)
{
base.OnUnsubscribeControlEvents(control);
TrackBar trackBar = control as TrackBar;
trackBar.ValueChanged -= trackBar_ValueChanged;
}
void trackBar_ValueChanged(object sender, EventArgs e)
{
if (this.ValueChanged != null)
{
var arguments = new TrackBarEventArgs();
arguments.NewValue = TrackBar.Value;
ValueChanged(sender, e);
}
}
public event EventHandler<trackbareventargs> ValueChanged;
protected override Size DefaultSize
{
get
{
return new Size(200, 16);
}
}
} </trackbareventargs>
Usage:
private void ToolStripMenuItem1_ValueChanged(object sender, TrackBarEventArgs e)
{
int trackbarValue = e.NewValue;
pictureBox2.Image = ChangeB(new Bitmap(pictureBox1.Image), trackbarValue );
}