Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 KB | None | 0 0
  1.         private class DataGridViewAllocationCell : DataGridViewButtonCell
  2.         {
  3.             public void Initialize(ReleaseSetupData releaseSetupData, string initialFieldName = NOTHING_SELECTED_TEXT, AllocationFieldType initialFieldType = AllocationFieldType.None)
  4.             {
  5.                 FieldName = initialFieldName;
  6.                 FieldType = initialFieldType;
  7.  
  8.                 contextMenu = new ContextMenuStrip();
  9.  
  10.                 ToolStripMenuItem clearSelectionItem = RegisterMenuItem(NOTHING_SELECTED_TEXT);
  11.                 clearSelectionItem.Click += (object sender, EventArgs e) => MenuItem_OnClick(sender, e, NOTHING_SELECTED_TEXT, AllocationFieldType.None);
  12.  
  13.                 contextMenu.Items.Add(new ToolStripSeparator());
  14.  
  15.                 ToolStripMenuItem menuItemIndexField = RegisterMenuItem("Indexfelder");
  16.                 foreach (IndexField field in releaseSetupData.IndexFields)
  17.                 {
  18.                     RegisterSubMenuItem(menuItemIndexField, field.Name, AllocationFieldType.IndexField);
  19.                 }
  20.  
  21.                 ToolStripMenuItem menuItemBatchField = RegisterMenuItem("Batchfelder");
  22.                 foreach (BatchField field in releaseSetupData.BatchFields)
  23.                 {
  24.                     RegisterSubMenuItem(menuItemBatchField, field.Name, AllocationFieldType.BatchField);
  25.                 }
  26.  
  27.                 ToolStripMenuItem menuItemKofaxValue = RegisterMenuItem("KofaxValues");
  28.                 foreach (dynamic field in releaseSetupData.BatchVariableNames)
  29.                 {
  30.                     RegisterSubMenuItem(menuItemKofaxValue, field, AllocationFieldType.KofaxValue);
  31.                 }
  32.             }
  33.  
  34.             private ContextMenuStrip contextMenu;
  35.  
  36.             private const string BUTTON_TEXT = "...";
  37.             private const string NOTHING_SELECTED_TEXT = "[ Kein Eintrag ]";
  38.  
  39.             public string FieldName { get; private set; }
  40.             public AllocationFieldType FieldType { get; private set; }
  41.  
  42.             private DataGridViewAllocationColumn ParentColumn { get { return OwningColumn as DataGridViewAllocationColumn; } }
  43.  
  44.             private int LabelWidth { get { return TextRenderer.MeasureText(FieldName, ParentColumn.DefaultCellStyle.Font).Width; } }
  45.  
  46.             private ToolStripMenuItem RegisterMenuItem(string itemName)
  47.             {
  48.                 return contextMenu.Items.Add(itemName) as ToolStripMenuItem;
  49.             }
  50.  
  51.             private void RegisterSubMenuItem(ToolStripMenuItem menuItem, string fieldName, AllocationFieldType fieldType)
  52.             {
  53.                 menuItem.DropDownItems.Add(fieldName, null, (object sender, EventArgs e) => MenuItem_OnClick(sender, e, fieldName, fieldType));
  54.             }
  55.  
  56.             private void MenuItem_OnClick(object sender, EventArgs e, string fieldName, AllocationFieldType fieldType)
  57.             {
  58.                 FieldName = fieldName;
  59.                 FieldType = fieldType;
  60.                 DataGridView.Refresh();
  61.             }
  62.  
  63.             protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
  64.             {
  65.                 base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground & ~DataGridViewPaintParts.ContentForeground);
  66.                 Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(ParentColumn.Index, rowIndex, false);
  67.                 Rectangle cellRectangle = GetContentBounds(rowIndex);
  68.                 Rectangle labelRectangle = new Rectangle(displayRectangle.Location, new Size(LabelWidth, displayRectangle.Height));
  69.                 cellRectangle.Offset(displayRectangle.Location);
  70.                 base.Paint(graphics, clipBounds, cellRectangle, rowIndex, elementState, value, BUTTON_TEXT, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All);
  71.                 TextRenderer.DrawText(graphics, FieldName, cellStyle.Font, labelRectangle, cellStyle.ForeColor);
  72.             }
  73.  
  74.             protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
  75.             {
  76.                 Rectangle rectangle = base.GetContentBounds(graphics, cellStyle, rowIndex);
  77.                 return new Rectangle(rectangle.Width - rectangle.Height, rectangle.Top, rectangle.Height, rectangle.Height);
  78.             }
  79.  
  80.             protected override void OnContentClick(DataGridViewCellEventArgs e)
  81.             {
  82.                 base.OnContentClick(e);
  83.                 Rectangle contentRectangle = GetContentBounds(e.RowIndex);
  84.                 Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
  85.                 Point location = new Point(displayRectangle.Left + contentRectangle.Left, displayRectangle.Top + contentRectangle.Bottom);
  86.                 contextMenu.Show(DataGridView, location);
  87.             }
  88.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement