我动态创建了4个按钮并使用c#win forms水平放置它们。现在我想在鼠标悬停事件的每个4个按钮下显示一个自定义工具提示(实际上是一个无边框形式)。但是我如何定位我的工具提示按钮下的表格?? 我已经尝试了下面的代码,但它没有按照预期的方式工作。
tooltip.Location = new System.Drawing.Point(b.Left, b.Top);
'tooltip'是工具提示形式的对象& 'b'是动态按钮。请告知一些代码片段。
private void B_MouseHover(object sender, EventArgs e)
{
var b = sender as Button;
//MessageBox.Show(Cursor.Position.ToString());
if(b!= null)
{
if (tooltip == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = new System.Drawing.Point(b.Left, b.Bottom);
tooltip.data(b.Tag.ToString());
tooltip.Show();
}
}
答案 0 :(得分:4)
你的命名方式有点误导。据我所知,你称之为工具提示只是一个Form
。你需要考虑两件事
(1)Form.StartPosition必须设置为FormStartPosition.Manual
(2)Form.Location
必须位于屏幕坐标中。请注意,您尝试使用的Button.Location
位于按钮的父客户端坐标中。 Control.PointToScreen必须用于转换。
在你的情况下,它应该是这样的
tooltip.StartPosition = FormStartPosition.Manual;
var topLeft = b.PointToScreen(new Point(0, 0));
tooltip.Location = new Point(topLeft.X, topLeft.Y + b.Height);
答案 1 :(得分:0)
当您显示工具提示时,您可以控制其位置,请检查show method overloads:https://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.show.aspx