C# WinApp ช่วยดู inheritance BindingNavigator ให้หน่อยครับ
คือผมต้องการ เขียน Control โดย inheritance BindingNavigator ขึ้นมาใช้เอง(ไม่รู้ว่าใช้คำถูกไม๊นะครับ)
โดยที่ต้องการคืออยากให้ ToolStripComboBox อยู่ด้านหลัง
แต่กลับเป็นแบบนี้ครับ
โค้ดมีแค่นี้ครับ
Code (C#)
public class TorBindingNavigator : BindingNavigator
{
private ToolStripComboBox cmb;
public TorBindingNavigator()
{
cmb = new ToolStripComboBox();
Items.AddRange(new ToolStripItem[] { cmb });
}
}
ผมดูใน BindingNavigator ของ .net เอง
Code (VB.NET)
//------------------------------------------------------------------------------
// <copyright file="BindingNavigator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Windows.Forms {
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices;
[
ComVisible(true),
ClassInterface(ClassInterfaceType.AutoDispatch),
DefaultProperty("BindingSource"),
DefaultEvent("RefreshItems"),
Designer("System.Windows.Forms.Design.BindingNavigatorDesigner, " + AssemblyRef.SystemDesign),
SRDescription(SR.DescriptionBindingNavigator)
]
public class BindingNavigator : ToolStrip, ISupportInitialize {
private BindingSource bindingSource;
private ToolStripItem moveFirstItem;
private ToolStripItem movePreviousItem;
private ToolStripItem moveNextItem;
private ToolStripItem moveLastItem;
private ToolStripItem addNewItem;
private ToolStripItem deleteItem;
private ToolStripItem positionItem;
private ToolStripItem countItem;
private String countItemFormat = SR.GetString(SR.BindingNavigatorCountItemFormat);
private EventHandler onRefreshItems = null;
private bool initializing = false;
private bool addNewItemUserEnabled = true;
private bool deleteItemUserEnabled = true;
[EditorBrowsable(EditorBrowsableState.Never)]
public BindingNavigator() : this(false) {
}
public BindingNavigator(BindingSource bindingSource) : this(true) {
BindingSource = bindingSource;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public BindingNavigator(IContainer container) : this(false) {
if (container == null) {
throw new ArgumentNullException("container");
}
container.Add(this);
}
public BindingNavigator(bool addStandardItems) {
if (addStandardItems) {
AddStandardItems();
}
}
public void BeginInit() {
initializing = true;
}
public void EndInit() {
initializing = false;
RefreshItemsInternal();
}
/// <devdoc>
/// Unhooks the BindingNavigator from the BindingSource.
/// </devdoc>
protected override void Dispose(bool disposing) {
if (disposing) {
BindingSource = null; // ...unwires from events of any prior BindingSource
}
base.Dispose(disposing);
}
public virtual void AddStandardItems() {
//
// Create items
//
MoveFirstItem = new System.Windows.Forms.ToolStripButton();
MovePreviousItem = new System.Windows.Forms.ToolStripButton();
MoveNextItem = new System.Windows.Forms.ToolStripButton();
MoveLastItem = new System.Windows.Forms.ToolStripButton();
PositionItem = new System.Windows.Forms.ToolStripTextBox();
CountItem = new System.Windows.Forms.ToolStripLabel();
AddNewItem = new System.Windows.Forms.ToolStripButton();
DeleteItem = new System.Windows.Forms.ToolStripButton();
ToolStripSeparator separator1 = new System.Windows.Forms.ToolStripSeparator();
ToolStripSeparator separator2 = new System.Windows.Forms.ToolStripSeparator();
ToolStripSeparator separator3 = new System.Windows.Forms.ToolStripSeparator();
char ch = string.IsNullOrEmpty(Name) || char.IsLower(Name[0]) ? 'b' : 'B';
MoveFirstItem.Name = ch + "indingNavigatorMoveFirstItem";
MovePreviousItem.Name = ch + "indingNavigatorMovePreviousItem";
MoveNextItem.Name = ch + "indingNavigatorMoveNextItem";
MoveLastItem.Name = ch + "indingNavigatorMoveLastItem";
PositionItem.Name = ch + "indingNavigatorPositionItem";
CountItem.Name = ch + "indingNavigatorCountItem";
AddNewItem.Name = ch + "indingNavigatorAddNewItem";
DeleteItem.Name = ch + "indingNavigatorDeleteItem";
separator1.Name = ch + "indingNavigatorSeparator";
separator2.Name = ch + "indingNavigatorSeparator";
separator3.Name = ch + "indingNavigatorSeparator";
MoveFirstItem.Text = SR.GetString(SR.BindingNavigatorMoveFirstItemText);
MovePreviousItem.Text = SR.GetString(SR.BindingNavigatorMovePreviousItemText);
MoveNextItem.Text = SR.GetString(SR.BindingNavigatorMoveNextItemText);
MoveLastItem.Text = SR.GetString(SR.BindingNavigatorMoveLastItemText);
AddNewItem.Text = SR.GetString(SR.BindingNavigatorAddNewItemText);
DeleteItem.Text = SR.GetString(SR.BindingNavigatorDeleteItemText);
CountItem.ToolTipText = SR.GetString(SR.BindingNavigatorCountItemTip);
PositionItem.ToolTipText = SR.GetString(SR.BindingNavigatorPositionItemTip);
CountItem.AutoToolTip = false;
PositionItem.AutoToolTip = false;
PositionItem.AccessibleName = SR.GetString(SR.BindingNavigatorPositionAccessibleName);
//
// Set up images
//
Bitmap moveFirstImage = new Bitmap(typeof(BindingNavigator), "BindingNavigator.MoveFirst.bmp");
Bitmap movePreviousImage = new Bitmap(typeof(BindingNavigator), "BindingNavigator.MovePrevious.bmp");
Bitmap moveNextImage = new Bitmap(typeof(BindingNavigator), "BindingNavigator.MoveNext.bmp");
Bitmap moveLastImage = new Bitmap(typeof(BindingNavigator), "BindingNavigator.MoveLast.bmp");
Bitmap addNewImage = new Bitmap(typeof(BindingNavigator), "BindingNavigator.AddNew.bmp");
Bitmap deleteImage = new Bitmap(typeof(BindingNavigator), "BindingNavigator.Delete.bmp");
moveFirstImage.MakeTransparent(System.Drawing.Color.Magenta);
movePreviousImage.MakeTransparent(System.Drawing.Color.Magenta);
moveNextImage.MakeTransparent(System.Drawing.Color.Magenta);
moveLastImage.MakeTransparent(System.Drawing.Color.Magenta);
addNewImage.MakeTransparent(System.Drawing.Color.Magenta);
deleteImage.MakeTransparent(System.Drawing.Color.Magenta);
MoveFirstItem.Image = moveFirstImage;
MovePreviousItem.Image = movePreviousImage;
MoveNextItem.Image = moveNextImage;
MoveLastItem.Image = moveLastImage;
AddNewItem.Image = addNewImage;
DeleteItem.Image = deleteImage;
MoveFirstItem.RightToLeftAutoMirrorImage = true;
MovePreviousItem.RightToLeftAutoMirrorImage = true;
MoveNextItem.RightToLeftAutoMirrorImage = true;
MoveLastItem.RightToLeftAutoMirrorImage = true;
AddNewItem.RightToLeftAutoMirrorImage = true;
DeleteItem.RightToLeftAutoMirrorImage = true;
MoveFirstItem.DisplayStyle = ToolStripItemDisplayStyle.Image;
MovePreviousItem.DisplayStyle = ToolStripItemDisplayStyle.Image;
MoveNextItem.DisplayStyle = ToolStripItemDisplayStyle.Image;
MoveLastItem.DisplayStyle = ToolStripItemDisplayStyle.Image;
AddNewItem.DisplayStyle = ToolStripItemDisplayStyle.Image;
DeleteItem.DisplayStyle = ToolStripItemDisplayStyle.Image;
//
// Set other random properties
//
PositionItem.AutoSize = false;
PositionItem.Width = 50;
//
// Add items to strip
//
Items.AddRange(new ToolStripItem[] {
MoveFirstItem,
MovePreviousItem,
separator1,
PositionItem,
CountItem,
separator2,
MoveNextItem,
MoveLastItem,
separator3,
AddNewItem,
DeleteItem,
});
}
[
DefaultValue(null),
SRCategory(SR.CatData),
SRDescription(SR.BindingNavigatorBindingSourcePropDescr),
TypeConverter(typeof(ReferenceConverter)),
SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")
]
public BindingSource BindingSource {
get {
return bindingSource;
}
set {
WireUpBindingSource(ref bindingSource, value);
}
}
[
TypeConverter(typeof(ReferenceConverter)),
SRCategory(SR.CatItems),
SRDescription(SR.BindingNavigatorMoveFirstItemPropDescr)
]
public ToolStripItem MoveFirstItem {
get {
if (moveFirstItem != null && moveFirstItem.IsDisposed) {
moveFirstItem = null;
}
return moveFirstItem;
}
set {
WireUpButton(ref moveFirstItem, value, new EventHandler(OnMoveFirst));
}
}
[
TypeConverter(typeof(ReferenceConverter)),
SRCategory(SR.CatItems),
SRDescription(SR.BindingNavigatorMovePreviousItemPropDescr)
]
public ToolStripItem MovePreviousItem {
get {
if (movePreviousItem != null && movePreviousItem.IsDisposed) {
movePreviousItem = null;
}
return movePreviousItem;
}
set {
WireUpButton(ref movePreviousItem, value, new EventHandler(OnMovePrevious));
}
}
[
TypeConverter(typeof(ReferenceConverter)),
SRCategory(SR.CatItems),
SRDescription(SR.BindingNavigatorMoveNextItemPropDescr)
]
public ToolStripItem MoveNextItem {
get {
if (moveNextItem != null && moveNextItem.IsDisposed) {
moveNextItem = null;
}
return moveNextItem;
}
set {
WireUpButton(ref moveNextItem, value, new EventHandler(OnMoveNext));
}
}
[
TypeConverter(typeof(ReferenceConverter)),
SRCategory(SR.CatItems),
SRDescription(SR.BindingNavigatorMoveLastItemPropDescr)
]
public ToolStripItem MoveLastItem {
get {
if (moveLastItem != null && moveLastItem.IsDisposed) {
moveLastItem = null;
}
return moveLastItem;
}
set {
WireUpButton(ref moveLastItem, value, new EventHandler(OnMoveLast));
}
}
[
TypeConverter(typeof(ReferenceConverter)),
SRCategory(SR.CatItems),
SRDescription(SR.BindingNavigatorAddNewItemPropDescr)
]
public ToolStripItem AddNewItem {
get {
if (addNewItem != null && addNewItem.IsDisposed) {
addNewItem = null;
}
return addNewItem;
}
set {
if (addNewItem != value && value != null) {
value.InternalEnabledChanged += new System.EventHandler(OnAddNewItemEnabledChanged);
addNewItemUserEnabled = value.Enabled;
}
WireUpButton(ref addNewItem, value, new EventHandler(OnAddNew));
}
}
[
TypeConverter(typeof(ReferenceConverter)),
SRCategory(SR.CatItems),
SRDescription(SR.BindingNavigatorDeleteItemPropDescr)
]
public ToolStripItem DeleteItem {
get {
if (deleteItem != null && deleteItem.IsDisposed) {
deleteItem = null;
}
return deleteItem;
}
set {
if (deleteItem != value && value != null) {
value.InternalEnabledChanged += new System.EventHandler(OnDeleteItemEnabledChanged);
deleteItemUserEnabled = value.Enabled;
}
WireUpButton(ref deleteItem, value, new EventHandler(OnDelete));
}
}
[
TypeConverter(typeof(ReferenceConverter)),
SRCategory(SR.CatItems),
SRDescription(SR.BindingNavigatorPositionItemPropDescr)
]
public ToolStripItem PositionItem {
get {
if (positionItem != null && positionItem.IsDisposed) {
positionItem = null;
}
return positionItem;
}
set {
WireUpTextBox(ref positionItem, value, new KeyEventHandler(OnPositionKey), new EventHandler(OnPositionLostFocus));
}
}
[
TypeConverter(typeof(ReferenceConverter)),
SRCategory(SR.CatItems),
SRDescription(SR.BindingNavigatorCountItemPropDescr)
]
public ToolStripItem CountItem {
get {
if (countItem != null && countItem.IsDisposed) {
countItem = null;
}
return countItem;
}
set {
WireUpLabel(ref countItem, value);
}
}
[
SRCategory(SR.CatAppearance),
SRDescription(SR.BindingNavigatorCountItemFormatPropDescr)
]
public String CountItemFormat {
get {
return countItemFormat;
}
set {
if (countItemFormat != value) {
countItemFormat = value;
RefreshItemsInternal();
}
}
}
[
SRCategory(SR.CatBehavior),
SRDescription(SR.BindingNavigatorRefreshItemsEventDescr)
]
public event EventHandler RefreshItems {
add {
onRefreshItems += value;
}
remove {
onRefreshItems -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void RefreshItemsCore() {
int count, position;
bool allowNew, allowRemove;
// Get state info from the binding source (if any)
if (bindingSource == null) {
count = 0;
position = 0;
allowNew = false;
allowRemove = false;
}
else {
count = bindingSource.Count;
position = bindingSource.Position + 1;
allowNew = (bindingSource as IBindingList).AllowNew;
allowRemove = (bindingSource as IBindingList).AllowRemove;
}
// Enable or disable items (except when in design mode)
if (!DesignMode) {
if (MoveFirstItem != null) moveFirstItem.Enabled = (position > 1);
if (MovePreviousItem != null) movePreviousItem.Enabled = (position > 1);
if (MoveNextItem != null) moveNextItem.Enabled = (position < count);
if (MoveLastItem != null) moveLastItem.Enabled = (position < count);
if (AddNewItem != null) {
System.EventHandler handler = new System.EventHandler(OnAddNewItemEnabledChanged);
addNewItem.InternalEnabledChanged -= handler;
addNewItem.Enabled = (addNewItemUserEnabled && allowNew);
addNewItem.InternalEnabledChanged += handler;
}
if (DeleteItem != null) {
System.EventHandler handler = new System.EventHandler(OnDeleteItemEnabledChanged);
deleteItem.InternalEnabledChanged -= handler;
deleteItem.Enabled = (deleteItemUserEnabled && allowRemove && count > 0);
deleteItem.InternalEnabledChanged += handler;
}
if (PositionItem != null) positionItem.Enabled = (position > 0 && count > 0);
if (CountItem != null) countItem.Enabled = (count > 0);
}
// Update current position indicator
if (positionItem != null) {
positionItem.Text = position.ToString(CultureInfo.CurrentCulture);
}
// Update record count indicator
if (countItem != null) {
countItem.Text = DesignMode ? CountItemFormat : String.Format(CultureInfo.CurrentCulture, CountItemFormat, count);
}
}
protected virtual void OnRefreshItems() {
// Refresh all the standard items
RefreshItemsCore();
// Raise the public event
if (onRefreshItems != null) {
onRefreshItems(this, EventArgs.Empty);
}
}
public bool Validate() {
bool validatedControlAllowsFocusChange;
return this.ValidateActiveControl(out validatedControlAllowsFocusChange);
}
private void AcceptNewPosition() {
// If no position item or binding source, do nothing
if (positionItem == null || bindingSource == null) {
return;
}
// Default to old position, in case new position turns out to be garbage
int newPosition = bindingSource.Position;
try {
// Read new position from item text (and subtract one!)
newPosition = Convert.ToInt32(positionItem.Text, CultureInfo.CurrentCulture) - 1;
}
catch (System.FormatException) {
// Ignore bad user input
}
catch (System.OverflowException) {
// Ignore bad user input
}
if (newPosition != bindingSource.Position) {
bindingSource.Position = newPosition;
}
RefreshItemsInternal();
}
private void CancelNewPosition() {
RefreshItemsInternal();
}
private void OnMoveFirst(object sender, EventArgs e) {
if (Validate()) {
if (bindingSource != null) {
bindingSource.MoveFirst();
RefreshItemsInternal();
}
}
}
private void OnMovePrevious(object sender, EventArgs e) {
if (Validate()) {
if (bindingSource != null) {
bindingSource.MovePrevious();
RefreshItemsInternal();
}
}
}
private void OnMoveNext(object sender, EventArgs e) {
if (Validate()) {
if (bindingSource != null) {
bindingSource.MoveNext();
RefreshItemsInternal();
}
}
}
private void OnMoveLast(object sender, EventArgs e) {
if (Validate()) {
if (bindingSource != null) {
bindingSource.MoveLast();
RefreshItemsInternal();
}
}
}
private void OnAddNew(object sender, EventArgs e) {
if (Validate()) {
if (bindingSource != null) {
bindingSource.AddNew();
RefreshItemsInternal();
}
}
}
private void OnDelete(object sender, EventArgs e) {
if (Validate()) {
if (bindingSource != null) {
bindingSource.RemoveCurrent();
RefreshItemsInternal();
}
}
}
private void OnPositionKey(object sender, KeyEventArgs e) {
switch (e.KeyCode) {
case Keys.Enter:
AcceptNewPosition();
break;
case Keys.Escape:
CancelNewPosition();
break;
}
}
private void OnPositionLostFocus(object sender, EventArgs e) {
AcceptNewPosition();
}
private void OnBindingSourceStateChanged(object sender, EventArgs e) {
RefreshItemsInternal();
}
private void OnBindingSourceListChanged(object sender, ListChangedEventArgs e) {
RefreshItemsInternal();
}
private void RefreshItemsInternal() {
// Block all updates during initialization
if (initializing) {
return;
}
// Call method that updates the items (overridable)
OnRefreshItems();
}
private void ResetCountItemFormat() {
countItemFormat = SR.GetString(SR.BindingNavigatorCountItemFormat);
}
private bool ShouldSerializeCountItemFormat() {
return countItemFormat != SR.GetString(SR.BindingNavigatorCountItemFormat);
}
private void OnAddNewItemEnabledChanged(object sender, EventArgs e) {
if (AddNewItem != null) {
addNewItemUserEnabled = addNewItem.Enabled;
}
}
private void OnDeleteItemEnabledChanged(object sender, EventArgs e) {
if (DeleteItem != null) {
deleteItemUserEnabled = deleteItem.Enabled;
}
}
private void WireUpButton(ref ToolStripItem oldButton, ToolStripItem newButton, EventHandler clickHandler) {
if (oldButton == newButton) {
return;
}
if (oldButton != null) {
oldButton.Click -= clickHandler;
}
if (newButton != null) {
newButton.Click += clickHandler;
}
oldButton = newButton;
RefreshItemsInternal();
}
private void WireUpTextBox(ref ToolStripItem oldTextBox, ToolStripItem newTextBox, KeyEventHandler keyUpHandler, EventHandler lostFocusHandler) {
if (oldTextBox != newTextBox) {
ToolStripControlHost oldCtrl = oldTextBox as ToolStripControlHost;
ToolStripControlHost newCtrl = newTextBox as ToolStripControlHost;
if (oldCtrl != null) {
oldCtrl.KeyUp -= keyUpHandler;
oldCtrl.LostFocus -= lostFocusHandler;
}
if (newCtrl != null) {
newCtrl.KeyUp += keyUpHandler;
newCtrl.LostFocus += lostFocusHandler;
}
oldTextBox = newTextBox;
RefreshItemsInternal();
}
}
private void WireUpLabel(ref ToolStripItem oldLabel, ToolStripItem newLabel) {
if (oldLabel != newLabel) {
oldLabel = newLabel;
RefreshItemsInternal();
}
}
private void WireUpBindingSource(ref BindingSource oldBindingSource, BindingSource newBindingSource) {
if (oldBindingSource != newBindingSource) {
if (oldBindingSource != null) {
oldBindingSource.PositionChanged -= new EventHandler(OnBindingSourceStateChanged);
oldBindingSource.CurrentChanged -= new EventHandler(OnBindingSourceStateChanged);
oldBindingSource.CurrentItemChanged -= new EventHandler(OnBindingSourceStateChanged);
oldBindingSource.DataSourceChanged -= new EventHandler(OnBindingSourceStateChanged);
oldBindingSource.DataMemberChanged -= new EventHandler(OnBindingSourceStateChanged);
oldBindingSource.ListChanged -= new ListChangedEventHandler(OnBindingSourceListChanged);
}
if (newBindingSource != null) {
newBindingSource.PositionChanged += new EventHandler(OnBindingSourceStateChanged);
newBindingSource.CurrentChanged += new EventHandler(OnBindingSourceStateChanged);
newBindingSource.CurrentItemChanged += new EventHandler(OnBindingSourceStateChanged);
newBindingSource.DataSourceChanged += new EventHandler(OnBindingSourceStateChanged);
newBindingSource.DataMemberChanged += new EventHandler(OnBindingSourceStateChanged);
newBindingSource.ListChanged += new ListChangedEventHandler(OnBindingSourceListChanged);
}
oldBindingSource = newBindingSource;
RefreshItemsInternal();
}
}
}
}
พบว่ามี AddStandardItems()
เหมือนปัญหาจะเป็นเพราะว่า
เมื่อเราเรียกใช้ TorBindingNavigator
จะเกิด
Items.AddRange(new ToolStripItem[] { cmb });
ก่อนที่ BindingNavigator จะ
AddStandardItems() อีกที
แบบนี้เราจะแก้ยังไงให้
AddStandardItems() ก่อนแล้วค่อย Items.AddRange(new ToolStripItem[] { cmb });Tag : .NET, C#, VS 2012 (.NET 4.x)
Date :
2017-04-29 10:38:00
By :
lamaka.tor
View :
1381
Reply :
6
ปล.
Code (C#)
private static IList GetListFromType(Type type) {
IList list = null;
if (typeof(ITypedList).IsAssignableFrom(type) && typeof(IList).IsAssignableFrom(type)) {
list = CreateInstanceOfType(type) as IList;
}
else if (typeof(IListSource).IsAssignableFrom(type)) {
list = (CreateInstanceOfType(type) as IListSource).GetList();
}
else {
list = CreateBindingList(ListBindingHelper.GetListItemType(type));
}
return list;
}
ผมเข้าใจมาโดยตลอดว่าถ้ายัด static เข้าไปก็แสดงว่าเราเรียกใช้ได้แบบไม่ต้องประกาศตัวแปร
แล้ว private ก็คือให้ใช้ได้เฉพาะใน class นั้นๆไม่ถ่ายทอด
สรุป
private static
คือ
เรียกใช้ได้โดยไม่ต้องประกาศตัวแปร แต่ใช้ได้แค่ใน class เท่านั้นนะ ใช่แบบนี้รึป่าวครับ
Date :
2017-04-29 10:43:15
By :
lamaka.tor
Date :
2017-05-03 09:58:58
By :
lamaka.tor
เห็น Code ยาว ๆ แล้วไม่กล้ายุ่งเลย
Date :
2017-05-03 10:10:07
By :
fonfire
ลองแบบนี้ดูครับ
Code (C#)
public class TorBindingNavigator : BindingNavigator
{
private ToolStripComboBox cmb;
public TorBindingNavigator()
{
}
public override void AddStandardItems()
{
base.AddStandardItems();
cmb = new ToolStripComboBox();
Items.AddRange(new ToolStripItem[] { cmb });
}
}
Date :
2017-05-03 10:38:24
By :
fonfire
ได้เลยละครับ
แสดงว่า ถ้า override คือจะทำงานที่ตัวคลาสแม่ก่อน แล้วค่อยมาทำงานที่ตัวคลาสลูก ซินะครับ
ขอบคุณมากๆครับ
Date :
2017-05-03 12:19:51
By :
lamaka.tor
เข้าใจว่า override
จะเป็นการแทนที่ตัวเดิมเลยครับ
แต่สามารถเรียกคำสั่งเดิม ผ่านตัว base.AddStandardItems ได้ครับ
Date :
2017-05-03 13:32:52
By :
fonfire
Load balance : Server 00