本文整理汇总了C#中System.Windows.Forms.DataGridView.HitTest方法的典型用法代码示例。如果您正苦于以下问题:C# DataGridView.HitTest方法的具体用法?C# DataGridView.HitTest怎么用?C# DataGridView.HitTest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.DataGridView
的用法示例。
在下文中一共展示了DataGridView.HitTest方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DataGridViewSelection
public DataGridViewSelection(DataGridView grid)
{
_rows = grid.SelectedRows.OfType<DataGridViewRow>();
if (!_rows.Any())
{
_rows = grid.SelectedCells.OfType<DataGridViewCell>().Select(c => c.OwningRow).Distinct();
var cols = grid.SelectedCells.OfType<DataGridViewCell>().Select(c => c.OwningColumn).Distinct().ToArray();
if (cols.Length == 1)
_columnPropertyName = cols[0].DataPropertyName;
}
if (!_rows.Any() && grid.CurrentCell != null)
{
_rows = Enumerable.Repeat(grid.CurrentCell.OwningRow, 1);
_columnPropertyName = grid.CurrentCell.OwningColumn.DataPropertyName;
}
else
{
_rows = Enumerable.Empty<DataGridViewRow>();
_columnPropertyName = null;
}
var client = grid.PointToClient(Cursor.Position);
var hit = grid.HitTest(client.X, client.Y);
if (!_rows.Any(r => r.Index == hit.RowIndex))
{
if (hit.RowIndex >= 0)
{
_rows = Enumerable.Repeat(grid.Rows[hit.RowIndex], 1);
_columnPropertyName = grid.Columns[hit.ColumnIndex].DataPropertyName;
}
else
{
_rows = Enumerable.Empty<DataGridViewRow>();
_columnPropertyName = null;
}
}
}
示例2: showContextMenus
//show the contextmenu after mouseup to select transform type
private void showContextMenus(DataGridView dgv, MouseEventArgs me)
{
DataGridView.HitTestInfo ht = dgv.HitTest(me.X, me.Y);
int intColndx = ht.ColumnIndex;
int intRowndx = ht.RowIndex;
DataTable _dt = (DataTable)dgvObs.DataSource;
if (intRowndx > 0 && intColndx > 0) return; //cell hit, go away
//get transform user selected
if (intRowndx < 0 && intColndx >= 0)
{
if (intColndx == 1)
{
if (!boolObsTransformed)
{
cmforResponseVar.MenuItems[0].Enabled = true; //we can transform a response variable
cmforResponseVar.MenuItems[1].Enabled = false; //but we cannot untransform an untransformed variable
}
else
{
cmforResponseVar.MenuItems[0].Enabled = false; //but we cannot transform a transformed response
cmforResponseVar.MenuItems[1].Enabled = true; //but we can untransform a transformed response
}
cmforResponseVar.Show(dgv, new Point(me.X, me.Y));
}
}
}
示例3: dgvCommandersLog_Click
private void dgvCommandersLog_Click(object sender, EventArgs e)
{
DataGridView.HitTestInfo hit;
try
{
m_ClickedDGVArgs = (MouseEventArgs)e;
if(m_ClickedDGVArgs.Button == System.Windows.Forms.MouseButtons.Right)
{
m_ClickedDGV = (DataGridView)sender;
hit = m_ClickedDGV.HitTest(m_ClickedDGVArgs.X, m_ClickedDGVArgs.Y);
if (hit.Type == DataGridViewHitTestType.TopLeftHeader)
{
DataGridViewSettings Tool = new DataGridViewSettings();
if(Tool.setVisibility(m_ClickedDGV) == DialogResult.OK)
{
m_GUIInterface.saveSetting(m_ClickedDGV);
}
}
else if (hit.Type == DataGridViewHitTestType.Cell)
{
cmsLog.Show(m_ClickedDGV, m_ClickedDGVArgs.Location);
}
}
}
catch (Exception ex)
{
throw new Exception("Error in DataGridView_Click", ex);
}
}
示例4: DecideDropDestinationRowIndex
/// <summary>
/// ドロップ先の行の決定
/// </summary>
/// <param name="grid"></param>
/// <param name="e"></param>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="next"></param>
/// <returns></returns>
private bool DecideDropDestinationRowIndex(
DataGridView grid, DragEventArgs e,
out int from, out int to, out bool next)
{
from = (int)e.Data.GetData(typeof(int));
// 元の行が追加用の行であれば、常に false
if (grid.NewRowIndex != -1 && grid.NewRowIndex == from)
{
to = 0; next = false;
return false;
}
Point clientPoint = grid.PointToClient(new Point(e.X, e.Y));
// 上下のみに着目するため、横方向は無視する
clientPoint.X = 1;
DataGridView.HitTestInfo hit =
grid.HitTest(clientPoint.X, clientPoint.Y);
to = hit.RowIndex;
if (to == -1)
{
int top = grid.ColumnHeadersVisible ? grid.ColumnHeadersHeight : 0;
top += 1; // ...
if (top > clientPoint.Y)
// ヘッダへのドロップ時は表示中の先頭行とする
to = grid.FirstDisplayedCell.RowIndex;
else
// 最終行へ
to = grid.Rows.Count - 1;
}
// 追加用の行は無視
if (to == grid.NewRowIndex) to--;
next = (to > from);
return (from != to);
}
示例5: showContextMenus
// user click captured - decide what menu items are appropriate and show them
public void showContextMenus(DataGridView dgv, MouseEventArgs me, DataTable dt)
{
dtColumnInformation dtCI = new dtColumnInformation(dt);
Utilities utils = new Utilities();
DataGridView.HitTestInfo ht = dgv.HitTest(me.X, me.Y);
int colndx = ht.ColumnIndex;
int rowndx = ht.RowIndex;
if (rowndx > 0 && colndx > 0) return; //cell hit, go away
if (rowndx < 0 && colndx >= 0)
{
//col header hit, show proper menu
intSelectedColIndex = colndx;
//do nothing if col 0 selected
if (colndx >= 1)
{
string colname = dt.Columns[colndx].Caption;
if (colname == strResponseVarColName)
{
if (utils.testValueAttribute(dt.Columns[intResponseVarColIndex], VBCommon.Globals.DEPENDENTVAR))
{
cmforResponseVar.MenuItems[0].Enabled = true; //we can transform a response variable
}
else
{
cmforResponseVar.MenuItems[0].Enabled = false; //but we cannot transform a transformed response
}
if (utils.testValueAttribute(dt.Columns[intResponseVarColIndex], VBCommon.Globals.DEPENDENTVARIBLETRANSFORM))
{
cmforResponseVar.MenuItems[2].Enabled = true; //we can untransform the transformed response variable
}
else
{
cmforResponseVar.MenuItems[2].Enabled = false; //but cannot untransform a response variable
}
cmforResponseVar.Show(dgv, new Point(me.X, me.Y));
}
else
{
//show context menu for ivs
if (dtCI.getColStatus(dt.Columns[intSelectedColIndex].ColumnName.ToString()))
{
//here if col enabled
cmforIVs.MenuItems[0].Enabled = true;
cmforIVs.MenuItems[1].Enabled = false; //cannot enable enabled col
cmforIVs.MenuItems[2].Enabled = true;
//response variable must be a ME, T(RV) or I(IV) not created by general transform
//if they do this then we're to remove all general operations performed,
if (canSetRV(utils)) cmforIVs.MenuItems[2].Enabled = true;
else cmforIVs.MenuItems[2].Enabled = false;
if (dt.Columns[intSelectedColIndex].ExtendedProperties.ContainsKey(VBCommon.Globals.MAINEFFECT))
cmforIVs.MenuItems[4].Enabled = false; //cannot remove maineffect column
else cmforIVs.MenuItems[4].Enabled = true;
}
else
{
//here if col disabled
cmforIVs.MenuItems[0].Enabled = false; //cannot disable disabled col
cmforIVs.MenuItems[1].Enabled = true;
cmforIVs.MenuItems[2].Enabled = false; //cannot disabled the response variable
}
cmforIVs.Show(dgv, new Point(me.X, me.Y));
}
}
}
else if (rowndx >= 0 && colndx < 0)
{
//row header hit, show menu
intSelectedRowIndex = rowndx;
if (dtRI.getRowStatus(dt.Rows[intSelectedRowIndex][0].ToString()))
{
//here if row is enabled
cmforRows.MenuItems[0].Enabled = true;
cmforRows.MenuItems[1].Enabled = false; //cannot enable enabled row
}
else
{
//here if row is disabled
cmforRows.MenuItems[0].Enabled = false; //cannot disable disabled row
cmforRows.MenuItems[1].Enabled = true;
}
cmforRows.Show(dgv, new Point(me.X, me.Y));
}
}
示例6: OnDataGridMouseDown
private void OnDataGridMouseDown(DataGridView sender, MouseEventArgs e)
{
_row = sender.HitTest(e.X, e.Y).RowIndex;
if (_row != -1)
{
var dragSize = SystemInformation.DragSize;
_dragBox = new Rectangle(
new Point(e.X - dragSize.Width / 2, e.Y - dragSize.Height / 2),
dragSize
);
}
else
{
_dragBox = Rectangle.Empty;
}
}
示例7: DataGridView_Click
private void DataGridView_Click(object sender, EventArgs e)
{
DataGridView dgv2 = null;
DataGridView.HitTestInfo hit;
try
{
m_ClickedDGVArgs = (MouseEventArgs)e;
if(m_ClickedDGVArgs.Button == System.Windows.Forms.MouseButtons.Right)
{
m_ClickedDGV = (DataGridView)sender;
hit = m_ClickedDGV.HitTest(m_ClickedDGVArgs.X, m_ClickedDGVArgs.Y);
if (hit.Type == DataGridViewHitTestType.TopLeftHeader)
{
DataGridViewSettings Tool = new DataGridViewSettings();
if(m_ClickedDGV.Equals(dgvStation1))
dgv2 = dgvStation2;
else if(m_ClickedDGV.Equals(dgvStation2))
dgv2 = dgvStation1;
if(Tool.setVisibility(m_ClickedDGV) == DialogResult.OK)
{
m_GUIInterface.saveSetting(m_ClickedDGV);
if(dgv2 != null)
{
DataGridViewSettings.CloneSettings(ref m_ClickedDGV, ref dgv2);
m_GUIInterface.saveSetting(dgv2);
}
}
}
else if (hit.Type == DataGridViewHitTestType.Cell)
{
if(m_ClickedDGV.Equals(dgvStationToStationRoutes))
{
contextMenuStrip2.Show(m_ClickedDGV, m_ClickedDGVArgs.Location);
}
else if(m_ClickedDGV.Equals(dgvAllCommodities))
{
contextMenuStrip3.Show(m_ClickedDGV, m_ClickedDGVArgs.Location);
}
else
{
contextMenuStrip1.Show(m_ClickedDGV, m_ClickedDGVArgs.Location);
}
}
}
}
catch (Exception ex)
{
CErr.processError(ex, "Error while changing DataGridView settings");
}
}