winform datagridview tooptip 设置气泡方向朝上。
当鼠标运行到单元格时,出现气泡,并且气泡朝上。
如图:
关键函数是:tooltip中的函数SetToolTip。
代码如下:
View Code 1 private void dgv1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
2 {
3 if (e.RowIndex < 0 || e.ColumnIndex < 0)
4 {
5 return;
6 }
7
8 this.toolTip1.Hide(this.dgv1);
9
10 this.CellColumnIndex = e.ColumnIndex;
11 this.CellRowIndex = e.RowIndex;
12 if (this.CellColumnIndex >= 0 && this.CellRowIndex >= 0)
13 {
14 DataGridViewCell dgvc = this.dgv1[this.CellColumnIndex, this.CellRowIndex];
15 Rectangle rec = dgvc.ContentBounds;
16 string tip = "Tip is " + dgvc.Value.ToString();
17 this.toolTip1.SetToolTip(dgv1, tip);
18
19 }
20
21 }
2 {
3 if (e.RowIndex < 0 || e.ColumnIndex < 0)
4 {
5 return;
6 }
7
8 this.toolTip1.Hide(this.dgv1);
9
10 this.CellColumnIndex = e.ColumnIndex;
11 this.CellRowIndex = e.RowIndex;
12 if (this.CellColumnIndex >= 0 && this.CellRowIndex >= 0)
13 {
14 DataGridViewCell dgvc = this.dgv1[this.CellColumnIndex, this.CellRowIndex];
15 Rectangle rec = dgvc.ContentBounds;
16 string tip = "Tip is " + dgvc.Value.ToString();
17 this.toolTip1.SetToolTip(dgv1, tip);
18
19 }
20
21 }
若要让气泡的方向向下,可以使用这个函数Show();
代码如下;
View Code 1 private void dgv1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
2 {
3 if (e.RowIndex < 0 || e.ColumnIndex < 0)
4 {
5 return;
6 }
7
8 this.toolTip1.Hide(this.dgv1);
9
10 this.CellColumnIndex = e.ColumnIndex;
11 this.CellRowIndex = e.RowIndex;
12 if (this.CellColumnIndex >= 0 && this.CellRowIndex >= 0)
13 {
14 int dgvY = dgv1.Location.Y;
15 int dgvX = dgv1.Location.X;
16 int cellX = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).X;
17 int cellY = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).Y;
18 int x = dgvX + cellX;
19 int y = dgvY + cellY+5;
20 DataGridViewCell dgvc = this.dgv1[this.CellColumnIndex, this.CellRowIndex];
21 // Point mousePos = PointToClient(MousePosition);
22 Point mousePos = new Point(x, y);
23 Rectangle rec = dgvc.ContentBounds;
24 string tip = "Tip is " + dgvc.Value.ToString()+" "+x+" "+y;
25
26 this.toolTip1.Show(tip, this.dgv1, mousePos);
27 //this.toolTip1.SetToolTip(dgv1, tip);
28
29 }
30
31 }
2 {
3 if (e.RowIndex < 0 || e.ColumnIndex < 0)
4 {
5 return;
6 }
7
8 this.toolTip1.Hide(this.dgv1);
9
10 this.CellColumnIndex = e.ColumnIndex;
11 this.CellRowIndex = e.RowIndex;
12 if (this.CellColumnIndex >= 0 && this.CellRowIndex >= 0)
13 {
14 int dgvY = dgv1.Location.Y;
15 int dgvX = dgv1.Location.X;
16 int cellX = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).X;
17 int cellY = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).Y;
18 int x = dgvX + cellX;
19 int y = dgvY + cellY+5;
20 DataGridViewCell dgvc = this.dgv1[this.CellColumnIndex, this.CellRowIndex];
21 // Point mousePos = PointToClient(MousePosition);
22 Point mousePos = new Point(x, y);
23 Rectangle rec = dgvc.ContentBounds;
24 string tip = "Tip is " + dgvc.Value.ToString()+" "+x+" "+y;
25
26 this.toolTip1.Show(tip, this.dgv1, mousePos);
27 //this.toolTip1.SetToolTip(dgv1, tip);
28
29 }
30
31 }

浙公网安备 33010602011771号