c\#xptable NET 에서 가장 강력 하고 모든 기능 을 가 진 표 컨트롤

프로젝트 가 필요 하기 때문에 ListView 를 맞 춰 야 합 니 다.열 에 그림,드 롭 다운 상자,상하 로 조정 할 수 있 는 숫자,진도 바 등 을 삽입 해 야 합 니 다.그래서 본문 은...
[번역]Mathew Hall.저 XPtable-.NET ListView meets Java's JTable[소개]프로젝트 가 필요 하기 때문에 ListView 를 맞 춰 야 합 니 다.열 에 이미지,드 롭 다운 상자,상하 조절 가능 한 숫자,진도 바 등 을 삽입 해 야 합 니 다.자바 의 배경 이 있 기 때문에,나 는 간단하게 그 JTable 패 키 징 을 기반 으로 할 것 이다.
[기능]모든 맞 춤 형 시각 화 인터페이스 는 XP 스타일 을 지원 합 니 다.재 맞 춤 형 컨트롤 을 쉽게 추가 하면 열,열,단원 을 숨 길 수 있 습 니 다.각 단원,열 은 Tooltip 등 이 있 습 니 다.[XPtable]XPtable 은 다음 구성 요 소 를 포함 합 니 다.1.Table,2.Columnmodel 과 그의 Columns,3.TableModel 과 그의 Row 와 Cell,4.Renderer 5.Editor[컨트롤 사용]먼저 컨트롤 을 Toolbox 에 불 러 옵 니 다(Item 추가,XPtable.dll 참조).그리고 Table,ColumnModel,TableModel 을 Form 에 끌 어 다 놓 고 Table 의 ColumnModel 과 TableModel 속성 을 설정 하고 Column 을 ColumnModel 에 추가 하 며 Row 와 Cell 을 TableModel 에 추가 하거나 코드 설정 을 직접 사용 합 니 다.
Table table = new Table();
ColumnModel columnModel = new ColumnModel();
TableModel tableModel = new TableModel();

// set the Table's ColumModel and TableModel
table.ColumnModel = columnModel;
table.TableModel = tableModel;

// add some Columns to the ColumnModel
columnModel.Columns.Add(new TextColumn("Text"));
columnModel.Columns.Add(new CheckBoxColumn("CheckBox"));
columnModel.Columns.Add(new ButtonColumn("Button"));

// add some Rows and Cells to the TableModel
tableModel.Rows.Add(new Row());
tableModel.Rows[0].Cells.Add(new Cell("Text 1"));
tableModel.Rows[0].Cells.Add(new Cell("CheckBox 1", true));
tableModel.Rows[0].Cells.Add(new Cell("Button 1"));
tableModel.Rows.Add(new Row());
tableModel.Rows[1].Cells.Add(new Cell("Text 2"));
tableModel.Rows[1].Cells.Add(new Cell("CheckBox 2", false));
tableModel.Rows[1].Cells.Add(new Cell("Button 2"));

[color=green]Table[/color]

Table 은 간단 한 대상 입 니 다.사실 데 이 터 를 어떻게 표시 하 는 지 모 릅 니 다.ColumnModel 과 TableModel 제어 열,단원 등 을 각각 사용 합 니 다.Table 의 주요 역할 은 그림 그리 기 동작 을 관리 하고 이 벤트 를 Renderer 와 Editor 에 전달 하여 행동 을 제어 하 는 것 입 니 다.ColumnModel ColumnModel 은 열의 집합 을 포함 하고 있 으 며,이 열 들 은 Table 에 표 시 됩 니 다.지정 한 열 을 만 드 는 CellRenderer 나 CellEditor 를 추적 합 니 다.Table Model 은 곧 표 시 될 Row 집합 을 포함 합 니 다.Renderers 는 위 에서 말 한 것 처럼 Table 은 단원 이나 열 머리 를 어떻게 그 리 는 지 모른다.아이디어,Renderers 라 는 대상 을 사용 하여 이 를 그립 니 다.Table 은 두 가지 유형의 Render 를 사용 합 니 다.하 나 는 Renderers:CellRenderer 로 Cell 을 그립 니 다.또 하 나 는 HeaderRenderer 로 Column Header 를 그립 니 다.CellRenderers 아래 는 모든 XPtable 이 제공 하 는 CellRenderer:*ICellRenderer-Exposes common methods provided by Cell renderers.*CellRenderer-Base class for all Cell renderers.*TextCellRenderer-A CellRenderer that draws Cell contents as strings.*Button CellRenderer-A CellRenderer that draws Cell contents as Buttons.*Check Box CellRenderer-A CellRenderer that draws Cell contents as CheckBoxes.* ImageCellRenderer - A CellRenderer that draws Cell contents as Images.* NumberCellRenderer - A CellRenderer that draws Cell contents as numbers.* ProgressBarCellRenderer - A CellRenderer that draws Cell contents as a ProgressBar.* DropDownCellRenderer - Base class for CellRenderers that draw Cell contents like ComboBoxes.*ComboxCellRenderer-A CellRenderer that draws Cell contents as a Combox.*ColorCellRenderer-A CellRenderer that draws Cell contents as Colors.*DateCellRenderer-CellRenderer 콘 텐 츠 를 DateTime 으로 그 리 는 CellRenderer.각 CellRenderer 의 기본 출력 은 다음 과 같 습 니 다.derer,하 나 는 CellRenderer 를 계승 하여 OnPaint 와 OnPaint Background 방법 을 다시 쓰 는 것 입 니 다.또 다른 방법 은 ICellRenderer 를 실현 하 는 것 이다.다음은 Table 의 내장 TextCellRenderer 코드 입 니 다.
public class TextCellRenderer : CellRenderer
{
protected override void OnPaint(PaintCellEventArgs e)
{
base.OnPaint(e);

// don't bother going any further if the Cell is null
if (e.Cell == null)
{
return;
}

// make sure we have some text to draw
if (e.Cell.Text != null && e.Cell.Text.Length != 0)
{
// check whether the cell is enabled
if (e.Enabled)
{
e.Graphics.DrawString(e.Cell.Text, base.Font,
base.ForeBrush, base.ClientRectangle,
base.StringFormat);
}
else
{
e.Graphics.DrawString(e.Cell.Text, base.Font,
base.GrayTextBrush, base.ClientRectangle,
base.StringFormat);
}
}

// draw a focus rect around the cell if it is
// enabled and has focus
if (e.Focused && e.Enabled)
{
ControlPaint.DrawFocusRectangle(e.Graphics,
base.ClientRectangle);
}
}
}

HeaderRenderers 는 CellRenderer 와 달리 HeaderRenderer 는 모든 열 Header 를 그립 니 다.XPtable 에서 제공 하 는 HeaderRenderer 는 다음 과 같 습 니 다.*IHeaderRenderer-Exposes common methods provided by Column header renderers.*HeaderRenderer-Base class for Renderers that draw Column headers.*XPheader Renderer-A HeaderRenderer that draws Windows XP themed Column headers.*GradientHeaderRenderer-A HeaderRenderer that draws gradient Column headers.*FlatHeaderRenderer-A HeaderRenderer that draws flat Column headers.아래 그림 은 내 장 된 HeaderRenderer 동작 을 보 여 줍 니 다.아래 문 구 를 통 해 구체 적 으로 지정 할 수 있 습 니 다.
Code:
// get the table to use a FlatHeaderRenderer
// to draw the column headers
table.HeaderRenderer = new FlatHeaderRenderer();
맞 춤 형 HeaderRenderer 아래 는 내 장 된 XPheader Renderer 코드 입 니 다.
public class XPHeaderRenderer : HeaderRenderer
{
protected override void OnPaintBackground(PaintHeaderEventArgs e)
{
base.OnPaintBackground(e);

if (e.Column == null)
{
ThemeManager.DrawColumnHeader(e.Graphics, e.HeaderRect,
ColumnHeaderStates.Normal);
}
else
{
ThemeManager.DrawColumnHeader(e.Graphics, e.HeaderRect,
(ColumnHeaderStates) e.Column.ColumnState);
}
}


protected override void OnPaint(PaintHeaderEventArgs e)
{
base.OnPaint(e);

// don't bother if we don't have a column
if (e.Column == null)
{
return;
}

Rectangle textRect = base.ClientRectangle;
Rectangle imageRect = Rectangle.Empty;

// check whether we can draw an image on the column header
if (e.Column.Image != null)
{
imageRect = base.CalcImageRect();
textRect.Width -= imageRect.Width;
textRect.X += imageRect.Width;

if (e.Column.ImageOnRight)
{
imageRect.X = base.ClientRectangle.Right - imageRect.Width;
textRect.X = base.ClientRectangle.X;
}

// column headers that aren't themed and are pressed need
// their contents shifted down and to the right by 1 pixel
if (!ThemeManager.VisualStylesEnabled &&
e.Column.ColumnState == ColumnState.Pressed)
{
imageRect.X += 1;
imageRect.Y += 1;
}

base.DrawColumnHeaderImage(e.Graphics, e.Column.Image,
imageRect, e.Column.Enabled);
}

// column headers that aren't themed and are pressed need
// their contents shifted down and to the right by 1 pixel
if (!ThemeManager.VisualStylesEnabled &&
e.Column.ColumnState == ColumnState.Pressed)
{
textRect.X += 1;
textRect.Y += 1;
}

// check whether we need to draw a sort arrow
if (e.Column.SortOrder != SortOrder.None)
{
// work out where to draw it
Rectangle arrowRect = base.CalcSortArrowRect();

// adjust the textRect to take the arrow into account
arrowRect.X = textRect.Right - arrowRect.Width;
textRect.Width -= arrowRect.Width;

base.DrawSortArrow(e.Graphics, arrowRect, e.Column.SortOrder,
e.Column.Enabled);
}

// check whether we have any text to draw
if (e.Column.Text == null)
{
return;
}

if (e.Column.Text.Length > 0 && textRect.Width > 0)
{
if (e.Column.Enabled)
{
e.Graphics.DrawString(e.Column.Text,
base.Font, base.ForeBrush,
textRect, base.StringFormat);
}
else
{
using (SolidBrush brush =
new SolidBrush(SystemPens.GrayText.Color))
{
e.Graphics.DrawString(e.Column.Text,
base.Font, brush,
textRect, base.StringFormat);
}
}
}
}
}

Editors 5 개 내 장 된 편집 기 는 다음 과 같 습 니 다.*ICellEditor-Exposes common methods provided by Cell editors.*CellEditor-Base class for Cell editors.*TextCellEditor-편집 을 위 한 class that contained strings.*NumberCellEditor-숫자 를 포함 하 는 편집 을 위 한 class.*DropDownCellEditor-Base class for editing Cells that contained dropdown buttons.* ComboBoxCellEditor - A class for editing Cells that look like a ComboBox.* ColorCellEditor - A class for editing Cells that contain Colors.* DateTimeCellEditor - A class for editing Cells that contain DateTimes.* IEditorUsesRendererButtons - Specifies that a CellEditor uses the buttons provided by its counter-part CellRenderer during editing.다음 그림 참조:아래 코드 에 따라 단원 을 편집 합 니 다.
Code:
// start editing the cell at (0, 0)
table.EditCell(0, 0);
// stop editing the cell and commit any changes
table.StopEditing();
// or cancel editing and ignore any changes
table.CancelEditing();
맞 춤 형 CellEditor 다음은 내 장 된 TextCellEditor 의 코드 입 니 다.
public class TextCellEditor : CellEditor
{
public TextCellEditor() : base()
{
TextBox textbox = new TextBox();
textbox.AutoSize = false;
textbox.BorderStyle = BorderStyle.None;

base.Control = textbox;
}


// Sets the location and size of the CellEditor
protected override void SetEditLocation(Rectangle cellRect)
{
this.TextBox.Location = cellRect.Location;
this.TextBox.Size = new Size(cellRect.Width-1,
cellRect.Height-1);
}


// Sets the initial value of the
// editor based on the contents of
// the Cell being edited
protected override void SetEditValue()
{
this.TextBox.Text = base.EditingCell.Text;
}


// Sets the contents of the Cell
// being edited based on the value
// in the editor
protected override void SetCellValue()
{
base.EditingCell.Text = this.TextBox.Text;
}


// Starts editing the Cell
public override void StartEditing()
{
this.TextBox.KeyPress +=
new KeyPressEventHandler(OnKeyPress);
this.TextBox.LostFocus +=
new EventHandler(OnLostFocus);

base.StartEditing();

this.TextBox.Focus();
}


// Stops editing the Cell and commits any changes
public override void StopEditing()
{
this.TextBox.KeyPress -=
new KeyPressEventHandler(OnKeyPress);
this.TextBox.LostFocus -=
new EventHandler(OnLostFocus);

base.StopEditing();
}


// Stops editing the Cell and ignores any changes
public override void CancelEditing()
{
this.TextBox.KeyPress -=
new KeyPressEventHandler(OnKeyPress);
this.TextBox.LostFocus -=
new EventHandler(OnLostFocus);

base.CancelEditing();
}


// Gets the TextBox used to edit the Cells contents
public TextBox TextBox
{
get
{
return base.Control as TextBox;
}
}


// Handler for the editors TextBox.KeyPress event
protected virtual void OnKeyPress(object sender,
KeyPressEventArgs e)
{
// check whether we nned to stop or cancel editing
if (e.KeyChar == AsciiChars.CarriageReturn /*Enter*/)
{
if (base.EditingTable != null)
{
base.EditingTable.StopEditing();
}
}
else if (e.KeyChar == AsciiChars.Escape)
{
if (this.EditingTable != null)
{
base.EditingTable.CancelEditing();
}
}
}


// Handler for the editors TextBox.LostFocus event
protected virtual void OnLostFocus(object sender,
EventArgs e)
{
// if the textbox loses focus
// we should stop editing
if (base.EditingTable != null)
{
base.EditingTable.StopEditing();
}
}
}

스타일 아래 코드 는 CellStyle 과 Rowtyle 을 공유 하 는 방법 을 보 여 줍 니 다.
// create a new CellStyle object
CellStyle cellStyle = new CellStyle();
cellStyle.BackColor = Color.Blue;
cellStyle.ForeColor = Color.Red;
cellStyle.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);

// create a new RowStyle object
RowStyle rowStyle = new RowStyle();
rowStyle.BackColor = Color.Yello;
rowStyle.ForeColor = Color.Green;
rowStyle.Font = new Font("Arial", 8.25f, FontStyle.Italics);

for (int i=0; i<3; i++)
{
tableModel.Rows[i].RowStyle = rowStyle;

// only set the cellstyle for cells in the 3rd column
tableModel[i, 2].CellStyle = cellStyle;
}

정렬 정렬 은 열 을 기반 으로 합 니 다.Header 에서 누 르 면 실 행 됩 니 다.6 가지 내 장 된 비교 기 가 있 습 니 다.*ComparerBase-Base class for Cell comparer.*TextComparer-for comparing Cells based on the Text property.*CheckBoxComparer-for comparing Cells based on the Checked property.*NumberComparer-for comparing Cells that include numbers in the Data property.*ImageComparer-for comparing Cells based on the Image property.property.*ColorComparer-for comparing Cells that include Colors in the Data property.*DateComparer-for comparing Cells that include DateTimes in the Data property.네 가지 정렬 메커니즘:*InsertionSorter*MergeSorter*ShellSorter*HeapSorter 는 table 을 호출 하 는 Sort 방법 으로 열 을 정렬 할 수 있 습 니 다.
// sort the currently sorted column in the opposite direction
// to its currnent sort order, or if no columns are sorted, the
// column that has focus in ascending order
table.Sort();

// sort the currently sorted column in the opposite direction
// to its currnent sort order, or if no columns are sorted, the
// column that has focus in ascending order using an unstable
// sort method
table.Sort(false);

// sort the column at index 3 in the table's ColumnModel
// opposite to its current sort order, or in ascending order
// if the column is not sorted
table.Sort(3);

// sort the column at index 3 in the table's ColumnModel
// opposite to its current sort order, or in ascending order
//if the column is not sorted using a stable sort method
table.Sort(3, true);

// sort the column at index 3 in the table's ColumnModel
// in descending order
table.Sort(3, SortOrder.Descending);

// sort the column at index 3 in the table's ColumnModel
// in ascending order using an unstable sort method
table.Sort(3, SortOrder.Ascending, false);

속성 도 설정 할 수 있 습 니 다.정렬 을 허용 하지 않 습 니 다:
Code:
// disable sorting for a column
column.Sortable = false;
맞 춤 형 비교 기
public class TextComparer : ComparerBase
{
// Compares two objects and returns a
// value indicating whether one is less
// than, equal to or greater than the other
public override int Compare(object a, object b)
{
Cell cell1 = (Cell) a;
Cell cell2 = (Cell) b;

// check for null cells
if (cell1 == null && cell2 == null)
{
return 0;
}
else if (cell1 == null)
{
return -1;
}
else if (cell2 == null)
{
return 1;
}

// check for null data
if (cell1.Text == null && cell2.Text == null)
{
return 0;
}
else if (cell1.Text == null)
{
return -1;
}

// now that we know both cells contain valid data,
// use the frameworks built in string comparer
return cell1.Text.CompareTo(cell2.Text);
}
}

선 택 된 단원 Selections 에서 선 택 된 단원 은 보통 강조 되 어 있 습 니 다.이 스타일 을 설정 할 수 있 습 니 다:
Code:
// use grid style selection
table.SelectionStyle = SelectionStyle.Grid;
추 가 될 기능*Word wrapping for cells and column headers*Autosizing rows and columns*Variable height rows*LinkLabel cells*RichTextFormat cells*Dialog based CellEditors*ListView style icon mode*RightToLeft support*Cut and paste support*Drag and drop support*Data bing*Column re-ordering*Printing support*Export to HTML and XML* Serialization* Other stuff I've forgotten or haven't thought of yet

좋은 웹페이지 즐겨찾기