|
This works GREAT and is SUPER easy to use
I wish I could do this but with premade filters as well as with the columns!
5/5!!!!!!
|
|
|
|
|
Fiwel,
how can i keep the changes like when i run and uncheck the popup then when i run it again must keep those changes i made when i run first
|
|
|
|
|
Something like that: (I did not compile the code - there might be errors...)
void Save(string csVisable)
{
// save to registry, file or database
}
string Load()
{
// load from registry, file or database
return ...;
}
OnClose()
{
string csVisable = "";
foreach (DataGridViewColumn c in pDataGridView.Columns)
{
csVisable += c.Visible ? "1" : "0";
}
Save(csVisable);
}
OnLoad()
{
string csVisable = Load();
if (csVisable.Length != pDataGridView.Columns.Count) return;
int i = 0;
foreach (char ch in csVisable)
{
pDataGridView.Columns[i].Visible = ch == '1';
i++;
}
}
|
|
|
|
|
check user.config, it might help you in this regard
It is never too late to be what you might have been.
|
|
|
|
|
if you want using other ContextMenuStrip. I mean using two popup menu.
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right && e.RowIndex == -1 && e.ColumnIndex == -1)
{
dataGridView1.ContextMenuStrip = null;
pUserControl1.Initialize(dataGridView1);
}
else
{
dataGridView1.ContextMenuStrip = contextMenuStrip2;
}
}
Vasya-Pupkin
|
|
|
|
|
Fiwel,
Do you know how to implement a 'Select All' option in the Menu Style - in order to re-select all the hidden columns??
|
|
|
|
|
Simple and dirty way (you can compare wit horiginal, to see what changed)
1. Changed MenuControl class:
<br />
class MenuControl<br />
{<br />
private static int m_iImageColumnWidth = 24;<br />
private static int m_iExtraWidth = 15;<br />
private class MenuCommand<br />
{<br />
public enum eType<br />
{<br />
Column,<br />
CheckAll,<br />
CheckNone,<br />
Done<br />
}<br />
<br />
public int Height { get { return Separator ? 5 : 21; } }<br />
public bool Separator { get { return m_csText == "-"; } }<br />
<br />
private string m_csText;<br />
private bool m_bChecked;<br />
private eType m_eType;<br />
public string Text { get { return m_csText; } }<br />
public eType MenuCommandType { get { return m_eType; } }<br />
public bool Checked { get { return m_bChecked; } set { m_bChecked = value; } }<br />
public MenuCommand(string csText, bool bChecked)<br />
: this(csText, bChecked, eType.Column)<br />
{<br />
}<br />
public MenuCommand(string csText)<br />
: this(csText, false, eType.Column)<br />
{<br />
}<br />
public MenuCommand(string csText, bool bChecked, eType peType)<br />
{<br />
m_csText = csText;<br />
m_bChecked = bChecked;<br />
m_eType = peType;<br />
}<br />
}<br />
<br />
private MenuCommand m_pTracMenuItem = null;<br />
private List<MenuCommand> m_pMenuCommands = new List<MenuCommand>();<br />
<br />
private Bitmap m_pMemBitmap;
private Graphics m_pMemGraphics;<br />
<br />
public int Width { get { return m_pMemBitmap.Width; } }<br />
public int Height { get { return m_pMemBitmap.Height; } }<br />
<br />
public bool Done<br />
{<br />
get<br />
{<br />
return m_pTracMenuItem != null && m_pTracMenuItem.MenuCommandType == MenuCommand.eType.Done;<br />
}<br />
}<br />
public bool CheckAll<br />
{<br />
get<br />
{<br />
return m_pTracMenuItem != null && m_pTracMenuItem.MenuCommandType == MenuCommand.eType.CheckAll;<br />
}<br />
}<br />
public bool CheckNone<br />
{<br />
get<br />
{<br />
return m_pTracMenuItem != null && m_pTracMenuItem.MenuCommandType == MenuCommand.eType.CheckNone;<br />
}<br />
}<br />
public int HitIndex<br />
{<br />
get<br />
{<br />
return m_pMenuCommands.IndexOf(m_pTracMenuItem);<br />
}<br />
}<br />
<br />
public bool ChangeChecked(int iIndex, Graphics g)<br />
{<br />
m_pMenuCommands[iIndex].Checked = !m_pMenuCommands[iIndex].Checked;<br />
Draw(g);<br />
return m_pMenuCommands[iIndex].Checked;<br />
}<br />
<br />
public void Add(string csText, bool bChecked)<br />
{<br />
m_pMenuCommands.Add(new MenuCommand(csText, bChecked));<br />
}<br />
<br />
public void CheckAllMenus(bool bCheck, Graphics g)<br />
{<br />
foreach (MenuCommand pMenuCommand in m_pMenuCommands)<br />
{<br />
if (pMenuCommand.MenuCommandType == MenuCommand.eType.Column)<br />
{<br />
pMenuCommand.Checked = bCheck;<br />
}<br />
}<br />
Draw(g);<br />
}<br />
<br />
public void Prepare(Graphics g)<br />
{<br />
m_pMenuCommands.Add(new MenuCommand("-"));<br />
m_pMenuCommands.Add(new MenuCommand("All", false, MenuCommand.eType.CheckAll));<br />
m_pMenuCommands.Add(new MenuCommand("None", false, MenuCommand.eType.CheckNone));<br />
m_pMenuCommands.Add(new MenuCommand("-"));<br />
m_pMenuCommands.Add(new MenuCommand("Done", false, MenuCommand.eType.Done));<br />
<br />
int iHeight = 4;
float fWidth = 0;<br />
foreach (MenuCommand pMenuCommand in m_pMenuCommands)<br />
{<br />
iHeight += pMenuCommand.Height;<br />
SizeF pSizeF = g.MeasureString(pMenuCommand.Text, SystemInformation.MenuFont);<br />
fWidth = Math.Max(fWidth, pSizeF.Width);<br />
}<br />
int iWidth = (int)fWidth + m_iImageColumnWidth + m_iExtraWidth;<br />
<br />
m_pMemBitmap = new Bitmap(iWidth, iHeight);<br />
m_pMemGraphics = Graphics.FromImage(m_pMemBitmap);<br />
}<br />
<br />
private MenuCommand HitTest(int X, int Y)<br />
{<br />
if (X < 0 || X > Width || Y < 0 || Y > Height)<br />
{<br />
return null;<br />
}<br />
<br />
int iHeight = 2;<br />
foreach (MenuCommand pMenuCommand in m_pMenuCommands)<br />
{<br />
if (Y > iHeight && Y < iHeight + pMenuCommand.Height)<br />
{<br />
return pMenuCommand.Separator ? null : pMenuCommand;<br />
}<br />
iHeight += pMenuCommand.Height;<br />
}<br />
return null;<br />
}<br />
<br />
public bool HitTestMouseMove(int X, int Y)<br />
{<br />
MenuCommand pMenuCommand = HitTest(X, Y);<br />
if (pMenuCommand != m_pTracMenuItem)<br />
{<br />
m_pTracMenuItem = pMenuCommand;<br />
return true;<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}<br />
public bool HitTestMouseDown(int X, int Y)<br />
{<br />
MenuCommand pMenuCommand = HitTest(X, Y);<br />
return pMenuCommand != null;<br />
}<br />
<br />
public void Draw(Graphics g)<br />
{<br />
Rectangle area = new Rectangle(0, 0, m_pMemBitmap.Width, m_pMemBitmap.Height);<br />
<br />
m_pMemGraphics.Clear(SystemColors.Control);<br />
<br />
DrawBackground(m_pMemGraphics, area);<br />
<br />
DrawAllCommands(m_pMemGraphics);<br />
<br />
g.DrawImage(m_pMemBitmap, area, area, GraphicsUnit.Pixel);<br />
}<br />
<br />
private void DrawBackground(Graphics g, Rectangle rectWin)<br />
{<br />
Rectangle main = new Rectangle(0, 0, rectWin.Width, rectWin.Height);<br />
<br />
<br />
int xStart = 1;<br />
int yStart = 2;<br />
int yHeight = main.Height - yStart - 1;<br />
<br />
using (Brush backBrush = new SolidBrush(Color.FromArgb(249, 248, 247)))<br />
g.FillRectangle(backBrush, main);<br />
<br />
using (Pen mainBorder = new Pen(Color.FromArgb(102, 102, 102)))<br />
g.DrawRectangle(mainBorder, main);<br />
<br />
Rectangle imageRect = new Rectangle(xStart, yStart, m_iImageColumnWidth, yHeight);<br />
<br />
using (Brush openBrush = new LinearGradientBrush(imageRect, Color.FromArgb(248, 247, 246), Color.FromArgb(215, 211, 204), 0f))<br />
g.FillRectangle(openBrush, imageRect);<br />
<br />
int rightLeft = main.Right + 1;<br />
int rightTop = main.Top + 4;<br />
int rightBottom = main.Bottom + 1;<br />
int leftLeft = main.Left + 4;<br />
int xExcludeStart = main.Left;<br />
int xExcludeEnd = main.Left;<br />
}<br />
<br />
private void DrawAllCommands(Graphics g)<br />
{<br />
int iTop = 2;<br />
foreach (MenuCommand pMenuCommand in m_pMenuCommands)<br />
{<br />
DrawSingleCommand(g, ref iTop, pMenuCommand, pMenuCommand == m_pTracMenuItem);<br />
}<br />
}<br />
<br />
private void DrawSingleCommand(Graphics g, ref int iTop, MenuCommand pMenuCommand, bool hotCommand)<br />
{<br />
int iHeight = pMenuCommand.Height;<br />
Rectangle drawRect = new Rectangle(1, iTop, Width, iHeight);<br />
iTop += iHeight;<br />
<br />
int textGapLeft = 4;<br />
int imageLeft = 4;<br />
<br />
int imageColWidth = 24;<br />
<br />
if (pMenuCommand.Separator)<br />
{<br />
Rectangle imageCol = new Rectangle(drawRect.Left, drawRect.Top, imageColWidth, drawRect.Height);<br />
<br />
using (Brush openBrush = new LinearGradientBrush(imageCol, Color.FromArgb(248, 247, 246), Color.FromArgb(215, 211, 204), 0f))<br />
g.FillRectangle(openBrush, imageCol);<br />
<br />
using (Pen separatorPen = new Pen(Color.FromArgb(166, 166, 166)))<br />
{<br />
g.DrawLine(separatorPen,<br />
drawRect.Left + imageColWidth + textGapLeft, drawRect.Top + 2,<br />
drawRect.Right - 7,<br />
drawRect.Top + 2);<br />
}<br />
}<br />
else<br />
{<br />
int leftPos = drawRect.Left + imageColWidth + textGapLeft;<br />
<br />
if (hotCommand)<br />
{<br />
Rectangle selectArea = new Rectangle(drawRect.Left + 1, drawRect.Top, drawRect.Width - 9, drawRect.Height - 1);<br />
<br />
using (SolidBrush selectBrush = new SolidBrush(Color.FromArgb(182, 189, 210)))<br />
g.FillRectangle(selectBrush, selectArea);<br />
<br />
using (Pen selectPen = new Pen(Color.FromArgb(10, 36, 106)))<br />
g.DrawRectangle(selectPen, selectArea);<br />
}<br />
else<br />
{<br />
Rectangle imageCol = new Rectangle(drawRect.Left, drawRect.Top, imageColWidth, drawRect.Height);<br />
<br />
using (Brush backBrush = new SolidBrush(Color.FromArgb(249, 248, 247)))<br />
g.FillRectangle(backBrush, new Rectangle(drawRect.Left + 1, drawRect.Top, drawRect.Width - 9, drawRect.Height));<br />
<br />
using (Brush openBrush = new LinearGradientBrush(imageCol, Color.FromArgb(248, 247, 246), Color.FromArgb(215, 211, 204), 0f))<br />
g.FillRectangle(openBrush, imageCol);<br />
}<br />
<br />
Rectangle strRect = new Rectangle(<br />
leftPos,<br />
drawRect.Top,<br />
Width - imageColWidth - textGapLeft - 5,<br />
drawRect.Height);<br />
<br />
StringFormat format = new StringFormat();<br />
format.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.NoWrap;<br />
format.Alignment = StringAlignment.Near;<br />
format.LineAlignment = StringAlignment.Center;<br />
format.HotkeyPrefix = HotkeyPrefix.Show;<br />
<br />
SolidBrush textBrush = new SolidBrush(SystemColors.MenuText);<br />
g.DrawString(pMenuCommand.Text, SystemInformation.MenuFont, textBrush, strRect, format);<br />
<br />
int imageTop = drawRect.Top + (drawRect.Height - 16) / 2;<br />
<br />
if (pMenuCommand.Checked)<br />
{<br />
Pen boxPen = new Pen(Color.FromArgb(10, 36, 106));<br />
Brush boxBrush;<br />
<br />
if (hotCommand)<br />
boxBrush = new SolidBrush(Color.FromArgb(133, 146, 181));<br />
else<br />
boxBrush = new SolidBrush(Color.FromArgb(212, 213, 216));<br />
<br />
Rectangle boxRect = new Rectangle(imageLeft - 1, imageTop - 1, 16 + 2, 16 + 2);<br />
<br />
g.FillRectangle(boxBrush, boxRect);<br />
<br />
g.DrawRectangle(boxPen, boxRect);<br />
<br />
boxPen.Dispose();<br />
boxBrush.Dispose();<br />
<br />
Pen pPen = new Pen(Color.Black, 1);<br />
g.DrawLine(pPen, new Point(imageLeft + 5, imageTop + 8), new Point(imageLeft + 5 + 2, imageTop + 8 + 2));<br />
g.DrawLine(pPen, new Point(imageLeft + 5, imageTop + 9), new Point(imageLeft + 5 + 2, imageTop + 9 + 2));<br />
g.DrawLine(pPen, new Point(imageLeft + 5 + 2, imageTop + 8 + 2), new Point(imageLeft + 5 + 2 + 4, imageTop + 8 + 2 - 4));<br />
g.DrawLine(pPen, new Point(imageLeft + 5 + 2, imageTop + 9 + 2), new Point(imageLeft + 5 + 2 + 4, imageTop + 9 + 2 - 4));<br />
}<br />
}<br />
}<br />
}<br />
2. Changed UserControlMenu class:
<br />
public partial class UserControlMenu : UserControl<br />
{<br />
public EventHandler DoneEvent;<br />
public EventHandler CheckAllEvent;<br />
public EventHandler CheckNoneEvent;<br />
public delegate void CheckedChanged(int iIndex, bool bChecked);<br />
public event CheckedChanged CheckedChangedEnent;<br />
public virtual void OnCheckedChanged(int iIndex, bool bChecked)<br />
{<br />
if (CheckedChangedEnent != null)<br />
CheckedChangedEnent(iIndex, bChecked);<br />
}<br />
public virtual void OnDone()<br />
{<br />
if (DoneEvent != null)<br />
DoneEvent(this, EventArgs.Empty);<br />
}<br />
public virtual void OnCheckAll()<br />
{<br />
if (CheckAllEvent != null)<br />
CheckAllEvent(this, EventArgs.Empty);<br />
}<br />
public virtual void OnCheckNone()<br />
{<br />
if (CheckNoneEvent != null)<br />
CheckNoneEvent(this, EventArgs.Empty);<br />
}<br />
<br />
MenuControl m_pMenuControl = new MenuControl();<br />
<br />
public UserControlMenu()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
private void buttonDone_Click(object sender, EventArgs e)<br />
{<br />
Parent.Focus();<br />
}<br />
<br />
public void Initialize(DataGridView pDataGridView)<br />
{<br />
m_pMenuControl = new MenuControl();<br />
<br />
foreach (DataGridViewColumn c in pDataGridView.Columns)<br />
{<br />
m_pMenuControl.Add(c.HeaderText, c.Visible);<br />
}<br />
<br />
m_pMenuControl.Prepare(CreateGraphics());<br />
<br />
Width = m_pMenuControl.Width;<br />
Height = m_pMenuControl.Height;<br />
<br />
timer1.Enabled = true;<br />
<br />
}<br />
<br />
private void UserControlMenu_Paint(object sender, PaintEventArgs e)<br />
{<br />
m_pMenuControl.Draw(e.Graphics);<br />
}<br />
<br />
private void UserControlMenu_MouseMove(object sender, MouseEventArgs e)<br />
{<br />
if (m_pMenuControl.HitTestMouseMove(e.X, e.Y))<br />
{<br />
m_pMenuControl.Draw(CreateGraphics());<br />
}<br />
}<br />
<br />
private void UserControlMenu_MouseDown(object sender, MouseEventArgs e)<br />
{<br />
if (m_pMenuControl.HitTestMouseDown(e.X, e.Y))<br />
{<br />
if (m_pMenuControl.Done)<br />
{<br />
OnDone();<br />
}<br />
else if (m_pMenuControl.CheckAll)<br />
{<br />
OnCheckAll();<br />
m_pMenuControl.CheckAllMenus(true, CreateGraphics());<br />
}<br />
else if (m_pMenuControl.CheckNone)<br />
{<br />
OnCheckNone();<br />
m_pMenuControl.CheckAllMenus(false, CreateGraphics());<br />
}<br />
else<br />
{<br />
int iHitIndex = m_pMenuControl.HitIndex;<br />
if (iHitIndex != -1)<br />
{<br />
bool bChecked = m_pMenuControl.ChangeChecked(iHitIndex, CreateGraphics());<br />
OnCheckedChanged(iHitIndex, bChecked);<br />
}<br />
}<br />
}<br />
}<br />
<br />
private void timer1_Tick(object sender, EventArgs e)<br />
{<br />
Point pPoint = PointToClient(Cursor.Position);<br />
if (m_pMenuControl.HitTestMouseMove(pPoint.X, pPoint.Y))<br />
{<br />
m_pMenuControl.Draw(CreateGraphics());<br />
}<br />
}<br />
}<br />
3. Changed DataGridViewColumnSelector class:
<br />
class DataGridViewColumnSelector<br />
{<br />
private DataGridView mDataGridView = null;<br />
private CheckedListBox mCheckedListBox;<br />
private ToolStripDropDown mPopup;<br />
<br />
public int MaxHeight = 300;<br />
public int Width = 200;<br />
<br />
public DataGridView DataGridView<br />
{<br />
get { return mDataGridView; }<br />
set { <br />
if (mDataGridView != null) mDataGridView.CellMouseClick -= new DataGridViewCellMouseEventHandler(mDataGridView_CellMouseClick);<br />
mDataGridView = value;<br />
if (mDataGridView!=null) mDataGridView.CellMouseClick += new DataGridViewCellMouseEventHandler(mDataGridView_CellMouseClick);<br />
}<br />
}<br />
<br />
void mDataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)<br />
{<br />
if (e.Button == MouseButtons.Right && e.RowIndex==-1 && e.ColumnIndex==-1) {<br />
pUserControl1.Initialize(mDataGridView);<br />
mPopup.Show(mDataGridView.PointToScreen(new Point (e.X,e.Y)));<br />
}<br />
}<br />
<br />
UserControlMenu pUserControl1 = new UserControlMenu();<br />
public DataGridViewColumnSelector() {<br />
<br />
pUserControl1.DoneEvent += new EventHandler(OnDone);<br />
pUserControl1.CheckAllEvent += new EventHandler(OnCheckAll);<br />
pUserControl1.CheckNoneEvent += new EventHandler(OnCheckNone);<br />
pUserControl1.CheckedChangedEnent += new UserControlMenu.CheckedChanged(CheckedChangedEnent);<br />
ToolStripControlHost mControlHost = new ToolStripControlHost(pUserControl1);<br />
mControlHost.Padding = Padding.Empty;<br />
mControlHost.Margin = Padding.Empty;<br />
mControlHost.AutoSize = false;<br />
<br />
mPopup = new ToolStripDropDown();<br />
mPopup.Padding = Padding.Empty;<br />
mPopup.AutoClose = true;<br />
mPopup.Items.Add(mControlHost);<br />
}<br />
<br />
void CheckedChangedEnent(int iIndex, bool bChecked)<br />
{<br />
mDataGridView.Columns[iIndex].Visible = bChecked;<br />
}<br />
<br />
private void OnCheckAll(object sender, EventArgs e)<br />
{<br />
CheckAll(true);<br />
}<br />
private void OnCheckNone(object sender, EventArgs e)<br />
{<br />
CheckAll(false);<br />
}<br />
private void CheckAll(bool bChecked)<br />
{<br />
foreach (DataGridViewColumn pColumn in mDataGridView.Columns)<br />
{<br />
pColumn.Visible = bChecked;<br />
}<br />
}<br />
<br />
private void OnDone(object sender, EventArgs e)<br />
{<br />
mPopup.AutoClose = false;<br />
mPopup.Close();<br />
mPopup.AutoClose = true;<br />
}<br />
<br />
public DataGridViewColumnSelector(DataGridView dgv) : this() {<br />
this.DataGridView = dgv;<br />
}<br />
<br />
void mCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e){<br />
mDataGridView.Columns[e.Index].Visible = (e.NewValue == CheckState.Checked);<br />
}<br />
}<br />
|
|
|
|
|
Ok Fiwel,
I'm gonna try this new class you posted. Thanks for your quick response. I'll let you know if it worked
Thanks again!
|
|
|
|
|
Hey Fiwel,
The new menu option - 'Select Al' worked. Thanks a lot man!!
|
|
|
|
|
Nice !!!! This is what I was looking for...
Do You have a VB version too ??? I'm not very expert with C#
Thanks
|
|
|
|
|
I done some VB but converting is not something I enjoy doing .
There are bunch of "C# to VB" tools on-line, like the one I rendomly picked:
http://www.developerfusion.com/tools/convert/csharp-to-vb
it producesed the following result. Try it...
Imports System
Imports System.Collections
Imports System.Text
Imports System.Drawing.Imaging
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Namespace DGVColumnSelector
Class MenuControl
Private Shared m_iImageColumnWidth As Integer = 24
Private Shared m_iExtraWidth As Integer = 15
Private Class MenuCommand
Public ReadOnly Property Height() As Integer
Get
Return If(Separator, 5, 21)
End Get
End Property
Public ReadOnly Property Separator() As Boolean
Get
Return m_csText = "-"
End Get
End Property
Private m_csText As String
'private int m_iIndex;
Private m_bChecked As Boolean
Private m_bDone As Boolean
Public ReadOnly Property Text() As String
Get
Return m_csText
End Get
End Property
'public int Index { get { return m_iIndex; } }
Public ReadOnly Property Done() As Boolean
Get
Return m_bDone
End Get
End Property
Public Property Checked() As Boolean
Get
Return m_bChecked
End Get
Set(ByVal value As Boolean)
m_bChecked = value
End Set
End Property
'public MenuCommand(string csText, int iIndex, bool bChecked)
Public Sub New(ByVal csText As String, ByVal bChecked As Boolean)
Me.New(csText, bChecked, False)
End Sub
Public Sub New(ByVal csText As String)
Me.New(csText, False, False)
End Sub
Public Sub New(ByVal csText As String, ByVal bChecked As Boolean, ByVal bDone As Boolean)
m_csText = csText
'm_iIndex = iIndex;
m_bChecked = bChecked
m_bDone = bDone
End Sub
End Class
Private m_pTracMenuItem As MenuCommand = Nothing
Private m_pMenuCommands As New ArrayList()
Private m_pMemBitmap As Bitmap
' = new Bitmap(panel1.Width, panel1.Height, PixelFormat.Format32bppArgb);
Private m_pMemGraphics As Graphics
Public ReadOnly Property Width() As Integer
Get
Return m_pMemBitmap.Width
End Get
End Property
Public ReadOnly Property Height() As Integer
Get
Return m_pMemBitmap.Height
End Get
End Property
Public ReadOnly Property Done() As Boolean
Get
Return m_pTracMenuItem IsNot Nothing AndAlso m_pTracMenuItem.Done
End Get
End Property
Public ReadOnly Property HitIndex() As Integer
Get
Return m_pMenuCommands.IndexOf(m_pTracMenuItem)
End Get
End Property
Public Function ChangeChecked(ByVal iIndex As Integer, ByVal g As Graphics) As Boolean
Dim pMenuCommand As MenuCommand = TryCast(m_pMenuCommands(iIndex), MenuCommand)
pMenuCommand.Checked = Not pMenuCommand.Checked
Draw(g)
Return pMenuCommand.Checked
End Function
Public Sub Add(ByVal csText As String, ByVal bChecked As Boolean)
m_pMenuCommands.Add(New MenuCommand(csText, bChecked))
End Sub
Public Sub Prepare(ByVal g As Graphics)
m_pMenuCommands.Add(New MenuCommand("-"))
Dim pDone As New MenuCommand("Done", False, True)
m_pMenuCommands.Add(pDone)
Dim iHeight As Integer = 4
'(2 + 2 top + bottom);
Dim fWidth As Single = 0
For Each pMenuCommand As MenuCommand In m_pMenuCommands
iHeight += pMenuCommand.Height
Dim pSizeF As SizeF = g.MeasureString(pMenuCommand.Text, SystemInformation.MenuFont)
fWidth = Math.Max(fWidth, pSizeF.Width)
Next
Dim iWidth As Integer = CInt(fWidth) + m_iImageColumnWidth + m_iExtraWidth
m_pMemBitmap = New Bitmap(iWidth, iHeight)
m_pMemGraphics = Graphics.FromImage(m_pMemBitmap)
End Sub
Private Function HitTest(ByVal X As Integer, ByVal Y As Integer) As MenuCommand
If X < 0 OrElse X > Width OrElse Y < 0 OrElse Y > Height Then
Return Nothing
End If
Dim iHeight As Integer = 2
For Each pMenuCommand As MenuCommand In m_pMenuCommands
If Y > iHeight AndAlso Y < iHeight + pMenuCommand.Height Then
Return If(pMenuCommand.Separator, Nothing, pMenuCommand)
End If
iHeight += pMenuCommand.Height
Next
Return Nothing
End Function
Public Function HitTestMouseMove(ByVal X As Integer, ByVal Y As Integer) As Boolean
Dim pMenuCommand As MenuCommand = HitTest(X, Y)
If pMenuCommand <> m_pTracMenuItem Then
m_pTracMenuItem = pMenuCommand
Return True
Else
Return False
End If
End Function
Public Function HitTestMouseDown(ByVal X As Integer, ByVal Y As Integer) As Boolean
Dim pMenuCommand As MenuCommand = HitTest(X, Y)
Return pMenuCommand IsNot Nothing
End Function
Public Sub Draw(ByVal g As Graphics)
Dim area As New Rectangle(0, 0, m_pMemBitmap.Width, m_pMemBitmap.Height)
m_pMemGraphics.Clear(SystemColors.Control)
' Draw the background area
DrawBackground(m_pMemGraphics, area)
' Draw the actual menu items
DrawAllCommands(m_pMemGraphics)
g.DrawImage(m_pMemBitmap, area, area, GraphicsUnit.Pixel)
End Sub
Private Sub DrawBackground(ByVal g As Graphics, ByVal rectWin As Rectangle)
Dim main As New Rectangle(0, 0, rectWin.Width, rectWin.Height)
Dim xStart As Integer = 1
Dim yStart As Integer = 2
Dim yHeight As Integer = main.Height - yStart - 1
' Paint the main area background
Using backBrush As Brush = New SolidBrush(Color.FromArgb(249, 248, 247))
g.FillRectangle(backBrush, main)
End Using
' Draw single line border around the main area
Using mainBorder As New Pen(Color.FromArgb(102, 102, 102))
g.DrawRectangle(mainBorder, main)
End Using
Dim imageRect As New Rectangle(xStart, yStart, m_iImageColumnWidth, yHeight)
' Draw the first image column
Using openBrush As Brush = New LinearGradientBrush(imageRect, Color.FromArgb(248, 247, 246), Color.FromArgb(215, 211, 204), 0F)
g.FillRectangle(openBrush, imageRect)
End Using
End Sub
Private Sub DrawAllCommands(ByVal g As Graphics)
Dim iTop As Integer = 2
For Each pMenuCommand As MenuCommand In m_pMenuCommands
DrawSingleCommand(g, iTop, pMenuCommand, pMenuCommand = m_pTracMenuItem)
Next
End Sub
Private Sub DrawSingleCommand(ByVal g As Graphics, ByRef iTop As Integer, ByVal pMenuCommand As MenuCommand, ByVal hotCommand As Boolean)
Dim iHeight As Integer = pMenuCommand.Height
Dim drawRect As New Rectangle(1, iTop, Width, iHeight)
iTop += iHeight
' Remember some often used values
Dim textGapLeft As Integer = 4
Dim imageLeft As Integer = 4
' Calculate some common values
Dim imageColWidth As Integer = 24
' Is this item a separator?
If pMenuCommand.Separator Then
' Draw the image column background
Dim imageCol As New Rectangle(drawRect.Left, drawRect.Top, imageColWidth, drawRect.Height)
' Draw the image column
Using openBrush As Brush = New LinearGradientBrush(imageCol, Color.FromArgb(248, 247, 246), Color.FromArgb(215, 211, 204), 0F)
g.FillRectangle(openBrush, imageCol)
End Using
' Draw a separator
Using separatorPen As New Pen(Color.FromArgb(166, 166, 166))
' Draw the separator as a single line
g.DrawLine(separatorPen, drawRect.Left + imageColWidth + textGapLeft, drawRect.Top + 2, drawRect.Right - 7, drawRect.Top + 2)
End Using
Else
Dim leftPos As Integer = drawRect.Left + imageColWidth + textGapLeft
' Should the command be drawn selected?
If hotCommand Then
Dim selectArea As New Rectangle(drawRect.Left + 1, drawRect.Top, drawRect.Width - 9, drawRect.Height - 1)
Using selectBrush As New SolidBrush(Color.FromArgb(182, 189, 210))
g.FillRectangle(selectBrush, selectArea)
End Using
Using selectPen As New Pen(Color.FromArgb(10, 36, 106))
g.DrawRectangle(selectPen, selectArea)
End Using
Else
Dim imageCol As New Rectangle(drawRect.Left, drawRect.Top, imageColWidth, drawRect.Height)
' Paint the main background color
Using backBrush As Brush = New SolidBrush(Color.FromArgb(249, 248, 247))
g.FillRectangle(backBrush, New Rectangle(drawRect.Left + 1, drawRect.Top, drawRect.Width - 9, drawRect.Height))
End Using
Using openBrush As Brush = New LinearGradientBrush(imageCol, Color.FromArgb(248, 247, 246), Color.FromArgb(215, 211, 204), 0F)
g.FillRectangle(openBrush, imageCol)
End Using
End If
' Calculate text drawing rectangle
Dim strRect As New Rectangle(leftPos, drawRect.Top, Width - imageColWidth - textGapLeft - 5, drawRect.Height)
' Left align the text drawing on a single line centered vertically
' and process the & character to be shown as an underscore on next character
Dim format As New StringFormat()
format.FormatFlags = StringFormatFlags.NoClip Or StringFormatFlags.NoWrap
format.Alignment = StringAlignment.Near
format.LineAlignment = StringAlignment.Center
format.HotkeyPrefix = HotkeyPrefix.Show
Dim textBrush As New SolidBrush(SystemColors.MenuText)
g.DrawString(pMenuCommand.Text, SystemInformation.MenuFont, textBrush, strRect, format)
' The image offset from top of cell is half the space left after
' subtracting the height of the image from the cell height
Dim imageTop As Integer = drawRect.Top + (drawRect.Height - 16) / 2
' Should a check mark be drawn?
If pMenuCommand.Checked Then
Dim boxPen As New Pen(Color.FromArgb(10, 36, 106))
Dim boxBrush As Brush
If hotCommand Then
boxBrush = New SolidBrush(Color.FromArgb(133, 146, 181))
Else
boxBrush = New SolidBrush(Color.FromArgb(212, 213, 216))
End If
Dim boxRect As New Rectangle(imageLeft - 1, imageTop - 1, 16 + 2, 16 + 2)
' Fill the checkbox area very slightly
g.FillRectangle(boxBrush, boxRect)
' Draw the box around the checkmark area
g.DrawRectangle(boxPen, boxRect)
boxPen.Dispose()
boxBrush.Dispose()
Dim pPen As New Pen(Color.Black, 1)
g.DrawLine(pPen, New Point(imageLeft + 5, imageTop + 8), New Point(imageLeft + 5 + 2, imageTop + 8 + 2))
g.DrawLine(pPen, New Point(imageLeft + 5, imageTop + 9), New Point(imageLeft + 5 + 2, imageTop + 9 + 2))
g.DrawLine(pPen, New Point(imageLeft + 5 + 2, imageTop + 8 + 2), New Point(imageLeft + 5 + 2 + 4, imageTop + 8 + 2 - 4))
g.DrawLine(pPen, New Point(imageLeft + 5 + 2, imageTop + 9 + 2), New Point(imageLeft + 5 + 2 + 4, imageTop + 9 + 2 - 4))
End If
End If
End Sub
End Class
End Namespace
|
|
|
|
|
why not on column header? that's the most common way
Regards,
unruledboy_at_gmail_dot_com
http://www.xnlab.com
|
|
|
|
|
It is matter of preference - that was a decision of the original author.
In my project, I drop it down when Row index is -1 (ignore column index – so it drops down on any click on the header). You can ignore row index as well, and drop it down anywhere in the grid view...
|
|
|
|
|
Of cause, you will need to replace:
if (mDataGridView != null) mDataGridView.CellMouseClick += new DataGridViewCellMouseEventHandler(mDataGridView_CellMouseClick);
with:
if (mDataGridView != null) mDataGridView.MouseClick += new MouseEventHandler(mDataGridView_MouseClick);
And implement:
void mDataGridView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
pUserControl1.Initialize(mDataGridView);
mPopup.Show(mDataGridView.PointToScreen(new Point(e.X, e.Y)));
}
}
Otherwise your drop down will be in a wrong place...
|
|
|
|
|
Hi,
Put this code in dataGridView1_Paint to indicate that have filter
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Blue, 0, 0, 40, 20);
Font newFont = new Font("Verdana", 12, FontStyle.Regular, GraphicsUnit.Pixel);
e.Graphics.DrawString("Filter", newFont, Brushes.Brown, 4, 4);
newFont.Dispose();
}
|
|
|
|
|
A good advice, but it is a bit more complicated.
40x20 is not always true - it is defined by the user.
Plus, if you allow resize of the header height and header column width, your filter indicator will not look good.
If you wish to do filter indicator do it more generically, based on the size of the upper-left cell.
Also, because resizing does not cause redraw, add events for RowHeadersWidthChanged and ColumnHeadersHeightChanged to cause redraw of the filter indicator:
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
DrawFilterIndicator(e.Graphics);
}
private void DrawFilterIndicator(Graphics g)
{
Rectangle pRect = new Rectangle(0, 0, dataGridView1.RowHeadersWidth, dataGridView1.ColumnHeadersHeight);
g.DrawRectangle(Pens.Blue, pRect);
Font newFont = new Font("Verdana", 12, FontStyle.Regular, GraphicsUnit.Pixel);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
pRect.X = 2;
pRect.Y = 2;
g.DrawString("Filter", newFont, Brushes.Brown, pRect, format);
newFont.Dispose();
}
private void dataGridView1_RowHeadersWidthChanged(object sender, EventArgs e)
{
DrawFilterIndicator(dataGridView1.CreateGraphics());
}
private void dataGridView1_ColumnHeadersHeightChanged(object sender, EventArgs e)
{
DrawFilterIndicator(dataGridView1.CreateGraphics());
}
|
|
|
|
|