[Closed] dotnet EventHandler problem
I wrote a basic custom slider that work pretty well
My problem is when i try to use it in max
I call “dotNet.addEventHandler” to add a function like “functionName sender arg”
It doesn’t do anything for mouseMove event!
Works fine with mouseClick event, why?!
here’s my control class source if it’s useful for anyone
public class Slider : UserControl
{
#region menbers
private MathControls MathCtrl;
// mouse
private int dragPoint;
private bool dragging = false;
// values
private float Maximum;
private float Minimum;
private float value;
private float percent;
private float dragValue;
//drawing
private float corn;
private int fontSize;
private float cursHeight;
private float cursWidth;
private Rectangle cursor;
private PointF[] slidBar;
private PointF[] controlCorner;
private SolidBrush[] slidBrush;
private Color BaseColor;
#endregion
//------------------------------------------------------------------------------------------------
#region constructor
public Slider()
{
MathCtrl = new MathControls();
Height = 25;
Width = 150;
Minimum = 0.0f;
value = 0.0f;
Maximum = 10f;
percent = 0.0f;
slidBrush = new SolidBrush[3];
BaseColor = Color.FromArgb(200, 200, 200);
slidBrush[0] = new SolidBrush(MathCtrl.multiplyColor(BaseColor,0.4f,0.4f,0.4f));
slidBrush[1] = new SolidBrush(MathCtrl.multiplyColor(BaseColor,0.6f,0.6f,0.6f));
slidBrush[2] = new SolidBrush(MathCtrl.multiplyColor(BaseColor,0.8f,0.8f,0.8f));
Resize += new EventHandler(OnResize);
// init draw
corn = Height*0.05f;
cursHeight = this.Height*0.7f;
cursWidth = cursHeight*0.5f;
//sliding bar
float slidBorder = cursWidth*3.0f;
slidBar = new PointF[4]
{
new PointF(slidBorder,(Height/2)-1),
new PointF(Width-slidBorder,(Height/2)-1),
new PointF(Width-slidBorder,(Height/2)+1),
new PointF(slidBorder,(Height/2)+1)
};
for (int i = 0; i<slidBar.Length; i += 1)
{slidBar[i].X += cursWidth*1.5f;}
//control corner
controlCorner = new PointF[5]
{
new PointF(Width-1,0),
new PointF(0,0),
new PointF(0,Height-1),
new PointF(Width-1,Height-1),
new PointF(Width-1,0)
};
//write cursor
float cursPosX = slidBar[1].X+((slidBar[3].X-slidBar[1].X)*(1f-percent));
value = Minimum+((Maximum-Minimum)*percent);
cursor = MathCtrl.floatRectangle(cursPosX,this.Height*0.15f,cursWidth,cursHeight);
//write value on top
Font curFont = new Font("Arial",8);
fontSize = MathCtrl.getFontSize(curFont,Height-(Convert.ToInt16(corn*2)) );
Invalidate();
}
#endregion
//------------------------------------------------------------------------------------------------
#region overrided methods
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && cursor.Contains(e.Location))
{
dragging = true;
dragPoint = e.Location.Y;
dragValue = value;
}
else if (e.Button == MouseButtons.Right && cursor.Contains(e.Location))
{
percent = 0.0f;
Invalidate(MathCtrl.floatRectangle(2f,2f,(Width-2f),(Height-2f)));
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (dragging)
{
int curPoint = e.Location.X;
percent = 1f-(curPoint-slidBar[1].X)/(slidBar[3].X-slidBar[1].X);
if (percent<0f) {percent=0f;}
if (percent>1f) {percent=1f;}
Invalidate(MathCtrl.floatRectangle(2f,2f,(Width-2f),(Height-2f)));
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
dragging = false;
}
protected void OnResize(object sender ,EventArgs e)
{
corn = Height*0.05f;
cursHeight = this.Height*0.7f;
cursWidth = cursHeight*0.5f;
//sliding bar
float slidBorder = cursWidth*3.0f;
slidBar = new PointF[4]
{
new PointF(slidBorder,(Height/2)-1),
new PointF(Width-slidBorder,(Height/2)-1),
new PointF(Width-slidBorder,(Height/2)+1),
new PointF(slidBorder,(Height/2)+1)
};
for (int i = 0; i<slidBar.Length; i += 1)
{slidBar[i].X += cursWidth*1.5f;}
//control corner
controlCorner = new PointF[5]
{
new PointF(Width-1,0),
new PointF(0,0),
new PointF(0,Height-1),
new PointF(Width-1,Height-1),
new PointF(Width-1,0)
};
//write cursor
float cursPosX = slidBar[1].X+((slidBar[3].X-slidBar[1].X)*(1f-percent));
float value = Minimum+((Maximum-Minimum)*percent);
cursor = MathCtrl.floatRectangle(cursPosX,this.Height*0.15f,cursWidth,cursHeight);
//write value on top
Font curFont = new Font("Arial",8);
fontSize = MathCtrl.getFontSize(curFont,Height-(Convert.ToInt16(corn*2)) );
}
protected override void OnPaint(PaintEventArgs e)
{
//e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
Pen Border = new Pen(BaseColor);
//control border and slid bar
e.Graphics.DrawPolygon(Border, slidBar);
e.Graphics.DrawPolygon(Border, controlCorner);
//write cursor
float cursPosX = (slidBar[1].X+((slidBar[3].X-slidBar[1].X)*(1f-percent)))-cursWidth/2;
float value = Minimum+((Maximum-Minimum)*percent);
cursor = MathCtrl.floatRectangle(cursPosX,this.Height*0.15f,cursWidth,cursHeight);
e.Graphics.FillRectangle(slidBrush[0], cursor);
//write value on top
Font curFont = new Font("Arial",8);
int fontSize = MathCtrl.getFontSize(curFont,Height-(Convert.ToInt16(corn*2)) );
e.Graphics.DrawString(MathCtrl.floatString(value), new Font("Arial",fontSize), slidBrush[0] ,new PointF(0,0));
}
#endregion
}
i would do this kind of control deriving from TrackBar and doing custom paint. if you are interested i can show details…
Finally i found the solution to my problem!
That’s was because i overwrote mouseMove function.
I add to use “eventHandler adding system” to existing function.
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Slider_Paint);
Don’t understand exactly why it didn’t work before.
If someone can explain, it will be interesting to hear!
bye
It doesn’t work because you are not calling base.OnMouseMove(e) inside your override method, so the event never bubble up out of your derived class.
Using Paint as a replacement for MouseMove is a poor choice, you should avoid it.
thanks for the reply lo!
I will be able to fix this error in not time.
Sorry but i don’t understand what you’re meaning by
Using Paint as a replacement for MouseMove is a poor choice, you should avoid it.
Am i doing the wrong thing with paint?
I’m just drawing a rectangle for control bounds, the slider bar and a cursor.
What did you understood? To make sure i don’t do any noob mistake
I thought you were using a paint event to catch mouse movements, never mind.
Of course I am intersted!
I just take a look at TrackBar control
Didn’t even know about it!
not really, I will just need to make it look like my other controls.
I will start from Trackbar and override onPaint method.
It don’t have time to do it now but thank you denisT for sharing knowledge! :bowdown: