-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.cs
158 lines (135 loc) · 5.55 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Forms;
using Syncfusion.Diagnostics;
using Syncfusion.Windows.Forms.Grid;
using Syncfusion.GridHelperClasses;
using System.IO;
namespace CellGrid
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.gridControl1.DefaultColWidth = 120;
this.gridControl1.DefaultRowHeight = 35;
// Add the custom cell type to the CellModels of the GridControl.
// The name we give at the argument is used to set the CellType name.
this.gridControl1.CellModels.Add("FolderBrowser", new FolderBrowserCellModel(gridControl1.Model));
// Set the cell type to "FolderBrowser"
this.gridControl1[2, 3].Text = "Browse here";
this.gridControl1[2, 3].CellType = "FolderBrowser";
}
}
#region Browse Button
// Draws the button with "..." style
public class BrowseButton : GridCellButton
{
static GridIconPaint icon;
static BrowseButton()
{
icon = new GridIconPaint("InteractiveCellDemo.", typeof(BrowseButton).Assembly);
}
public BrowseButton(GridTextBoxCellRenderer control)
:base(control)
{}
public override void Draw(Graphics g, int rowIndex, int colIndex, bool bActive, GridStyleInfo style)
{
TraceUtil.TraceCurrentMethodInfo(rowIndex, colIndex, bActive, style.CellValue);
base.Draw(g, rowIndex, colIndex, bActive, style);
bool hovering = IsHovering(rowIndex, colIndex);
bool mouseDown = IsMouseDown(rowIndex, colIndex);
bool disabled = !style.Clickable;
ButtonState buttonState = ButtonState.Normal;
if (disabled)
buttonState |= ButtonState.Inactive | ButtonState.Flat;
else if (!hovering && !mouseDown)
buttonState |= ButtonState.Flat;
Point ptOffset = Point.Empty;
if (mouseDown)
{
ptOffset = new Point(1, 1);
buttonState |= ButtonState.Pushed;
}
DrawButton(g, Bounds, buttonState, style);
Bitmap bitmapName = new Bitmap(Image.FromFile(@"..\..\Browse.bmp"));
icon.PaintIcon(g, Bounds, ptOffset, bitmapName, Color.White);
}
}
#endregion
#region FolderBrowserCellModel
//Deriving GridTextBoxCellModel
public class FolderBrowserCellModel : GridTextBoxCellModel
{
protected FolderBrowserCellModel(SerializationInfo info, StreamingContext context)
: base(info, context)
{
//Set the button bar size.
base.ButtonBarSize = new Size(20, 20);
}
//Constructor for the Derived Class
public FolderBrowserCellModel(GridModel grid)
: base(grid)
{ }
//Override the CreateRenderer() in the Base class.
public override GridCellRendererBase CreateRenderer(GridControlBase control)
{
//Return the Custom Renderer Object
return new FolderBrowserCellRenderer(control, this);
}
}
#endregion
#region FolderBrowserCellRenderer
//Deriving the GridTextBoxCellRenderer class
public class FolderBrowserCellRenderer : GridTextBoxCellRenderer
{
//FolderBrowser object decleration.
private System.Windows.Forms.OpenFileDialog folderBrowser1;
public FolderBrowserCellRenderer(GridControlBase grid, GridTextBoxCellModel cellModel)
: base(grid, cellModel)
{
AddButton(new BrowseButton(this));
//Initialize the folderBrowser1 object.
this.folderBrowser1 = new System.Windows.Forms.OpenFileDialog();
}
#region [overrides]
protected override void OnButtonClicked(int rowIndex, int colIndex, int button)
{
base.OnButtonClicked(rowIndex, colIndex, button);
if(folderBrowser1.ShowDialog() == DialogResult.OK)
{
string filePath = folderBrowser1.FileName;
}
}
/// <override/>
protected override Rectangle OnLayout(int rowIndex, int colIndex, GridStyleInfo style, Rectangle innerBounds, Rectangle[] buttonsBounds)
{
// Trace the properties of the cell
TraceUtil.TraceCurrentMethodInfo(rowIndex, colIndex, style, innerBounds, buttonsBounds);
Rectangle rightArea;
if (this.Grid.IsRightToLeft())
{
rightArea = Rectangle.FromLTRB(innerBounds.Left, innerBounds.Top, innerBounds.Left + 20, innerBounds.Bottom);
innerBounds.X += 20;
}
else
{
// Here you specify where the button should be drawn within the cell.
rightArea = Rectangle.FromLTRB(innerBounds.Right - 20, innerBounds.Top, innerBounds.Right, innerBounds.Bottom);
}
innerBounds.Width -= 20;
buttonsBounds[0] = GridUtil.CenterInRect(rightArea, new Size(20, 20));
// Here you return the area where the text should be drawn/edited.
return innerBounds;
}
#endregion
}
#endregion
}