WinApp C# มาอีกแล้ว ครับ ปัญหา รอบดึก เกี่ยวกับ การเขียน User control ครับ
ผมนั่งเขียน user control ประมาณนี้ครับ
โค้ด
Code (C#)
namespace TORServices.FormsTor
{
[
ComVisible(true),
ClassInterface(ClassInterfaceType.AutoDispatch),
LookupBindingProperties("DataSource", "DisplayMember", "ValueMember", "SelectedValue")
]
public partial class Textbox2AndButton : UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSearch = new System.Windows.Forms.Button();
this.txtID = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.txtDetail = new System.Windows.Forms.TextBox();
this.panel2 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// btnSearch
//
this.btnSearch.Dock = System.Windows.Forms.DockStyle.Right;
this.btnSearch.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.btnSearch.Image = global::TORServices.Properties.Resources.search_button32x32;
this.btnSearch.Location = new System.Drawing.Point(315, 0);
this.btnSearch.Margin = new System.Windows.Forms.Padding(4);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(28, 30);
this.btnSearch.TabIndex = 0;
this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// txtID
//
this.txtID.Dock = System.Windows.Forms.DockStyle.Left;
this.txtID.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.txtID.Location = new System.Drawing.Point(0, 0);
this.txtID.Margin = new System.Windows.Forms.Padding(4);
this.txtID.Name = "txtID";
this.txtID.Size = new System.Drawing.Size(68, 29);
this.txtID.TabIndex = 1;
this.txtID.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtID_KeyDown);
this.txtID.Leave += new System.EventHandler(this.txtID_Leave);
//
// panel1
//
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.panel1.Location = new System.Drawing.Point(68, 0);
this.panel1.Margin = new System.Windows.Forms.Padding(4);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(5, 30);
this.panel1.TabIndex = 3;
//
// txtDetail
//
this.txtDetail.BackColor = System.Drawing.SystemColors.Window;
this.txtDetail.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtDetail.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.txtDetail.Location = new System.Drawing.Point(73, 0);
this.txtDetail.Margin = new System.Windows.Forms.Padding(4);
this.txtDetail.Name = "txtDetail";
this.txtDetail.ReadOnly = true;
this.txtDetail.Size = new System.Drawing.Size(237, 29);
this.txtDetail.TabIndex = 4;
//
// panel2
//
this.panel2.Dock = System.Windows.Forms.DockStyle.Right;
this.panel2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.panel2.Location = new System.Drawing.Point(310, 0);
this.panel2.Margin = new System.Windows.Forms.Padding(4);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(5, 30);
this.panel2.TabIndex = 5;
//
// Textbox2AndButton
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.txtDetail);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.txtID);
this.Controls.Add(this.btnSearch);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Textbox2AndButton";
this.Size = new System.Drawing.Size(343, 30);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.TextBox txtID;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox txtDetail;
private DataTable tblSource;
private string _DisplayMember;
private string _ValueMember;
private Panel panel2;
private string stSelectedValue;
public Textbox2AndButton()
{
InitializeComponent();
}
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("TextID")]
public string TextID
{
get
{
return this.txtID.Text;
}
set
{
this.txtID.Text = value;
}
}
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("TextDetail")]
public string TextDetail
{
get
{
return this.txtDetail.Text;
}
set
{
this.txtDetail.Text = value;
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("ImageButton")]
public Image ImageButton
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return btnSearch.Image;
}
set
{
btnSearch.Image = value; Invalidate();
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Color text ID")]
public Color ColortextID
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return txtID.BackColor;
}
set
{
txtID.BackColor = value; Invalidate();
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Color text ID")]
public Color ColortextDetail
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return txtDetail.BackColor;
}
set
{
txtDetail.BackColor = value; Invalidate();
}
}
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
txtDetail.Font = Font;
txtID.Font = Font;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.Height = txtID.Height;
btnSearch.Width = btnSearch.Height;
}
public event EventHandler TextBoxTextChanged
{
add
{
txtID.TextChanged += value;
}
remove
{
txtID.TextChanged -= value;
}
}
public event EventHandler TextBoxDetailTextChanged
{
add
{
txtDetail.TextChanged += value;
}
remove
{
txtDetail.TextChanged -= value;
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
RefreshProperties(RefreshProperties.All),
ParenthesizePropertyName(true)
]
public ControlBindingsCollection DataBindings
{
get
{
ControlBindingsCollection bindings = (ControlBindingsCollection)this.txtID.DataBindings;
if (bindings == null)
{
bindings = new ControlBindingsCollection(this);
}
return bindings;
}
}
[Bindable(true)]
[Category("Data")]
[Description("Set DataSource")]
public DataTable DataSource
{
get
{
return tblSource;
}
set
{
tblSource = value;
}
}
[Category("Data")]
[Editor(typeof(Textbox2AndButtonCaller), typeof(UITypeEditor))]
[Description("Set field name in DisplayMember")]
public string DisplayMember
{
get
{
return _DisplayMember;
}
set
{
_DisplayMember = value;
}
}
[Category("Data")]
[Editor(typeof(Textbox2AndButtonCaller), typeof(UITypeEditor))]
[Description("Set field name in ValueMember")]
public string ValueMember
{
get
{
return _ValueMember;
}
set
{
_ValueMember = value;
}
}
[Browsable(false)]
[DefaultValue("")]
public string SelectedValue
{
get { return stSelectedValue; }
set { stSelectedValue = value; }
}
private void btnSearch_Click(object sender, EventArgs e)
{
Form parent = this.FindForm();
Textbox2AndButtonChoseGrid popup = new Textbox2AndButtonChoseGrid(this);
popup.Location = new Point(parent.Location.X + this.Location.X + 4, parent.Location.Y + this.Location.Y + this.Height + 30);
popup.ShowDialog();
}
private void txtID_Leave(object sender, EventArgs e)
{
IDSearch();
}
private void IDSearch()
{
try
{
DataRow[] drs = tblSource.Select(ValueMember + " = " + txtID.Text);
if (drs.Length >= 0)
txtDetail.Text = drs[0][DisplayMember].ToString();
}
catch { MessageBox.Show("Error! \n Data not found"); }
}
private void txtID_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
IDSearch();
}
}
public class Textbox2AndButtonCaller : UITypeEditor
{
#region Variables declaration
private IWindowsFormsEditorService edSvc = null;
private ListBox cboTableNames = null;
#endregion
#region Override Methods
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
{
return UITypeEditorEditStyle.DropDown;
}
return base.GetEditStyle(context);
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context != null && context.Instance != null && provider != null)
{
Textbox2AndButton control = (Textbox2AndButton)context.Instance;
edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
cboTableNames = new ListBox();
cboTableNames.BorderStyle = BorderStyle.FixedSingle;
for (int i = 0; i < control.DataSource.Columns.Count; i++)
cboTableNames.Items.Add(control.DataSource.Columns[i].ColumnName);
cboTableNames.SelectedIndexChanged += new EventHandler(cboTableNames_SelectedIndexChanged);
edSvc.DropDownControl(cboTableNames);
return cboTableNames.SelectedItem;
}
}
return base.EditValue(context, provider, value);
}
#endregion
#region Events
private void cboTableNames_SelectedIndexChanged(object sender, EventArgs e)
{
cboTableNames.Text = (string)cboTableNames.SelectedItem;
edSvc.CloseDropDown();
}
#endregion
}
}
คือ ผมอยากให้ เลือก DataSource แล้ว ให้สร้าง BindingSource / Dataset/TableAdapter ลงในฟอร์มเลย ครับ
ผมงมอยู่ตั้งนานก็ยังหาไม่เจอว่าต้องเขียนยังไง
มาได้แค่นี้ครับ
Tag : .NET, Win (Windows App), C#
Date :
2019-06-28 00:34:26
By :
lamaka.tor
View :
2083
Reply :
55
Code (C#)
using System.Windows.Forms;
namespace CS
{
[System.ComponentModel.DefaultBindingProperty("PhoneNumber")]
public partial class PhoneNumberBox : UserControl
{
public string PhoneNumber
{
get{ return maskedTextBox1.Text; }
set{ maskedTextBox1.Text = value; }
}
public PhoneNumberBox()
{
InitializeComponent();
}
}
}
Date :
2019-06-28 07:17:27
By :
xxx
คุณเคยถามว่า แล้วยุคใหม่เขาเขียนกันอย่างไร? ยกตัวอย่างคร่าว <Table("Users")> คือตารางในฐานข้อมูลจริง
ทางเลือกคือ EF/Dapper แต่ผมเลือก Dapper
Code (VB.NET)
<Table("Users")>
Public Class Users
<Key, Required, Column("UserId")>
Public Property Id As Integer 'PK เขียนลง Database และเขียนลงคอลัมภ์ UserId
<Column("UserName")>
Public Property FirstName As String 'เขียนลง Database และให้เขียนลงคอลัมภ์ UserName
<NotMapped>
Public Property LastName As String 'ไม่เขียนลง Database
<Key>
Public Property AutoIncrementId As Integer 'ไม่เขียนลง Database
<Dapper.ReadOnly(True)>
Public Property CreatedDate As DateTime 'ไม่เขียนลง Database
End Class
sexyDatabase.ConfigDatabase(SimpleCRUD.Dialect.SQLite) 'รองรับหลากหลาย ไม่พอก็เขียนเพิ่ม
Using cn = SexyConnection()
cn.Open() 'Open Connection
Using trans = cn.BeginTransaction()
Try
Dim objUser As New Users() With {.Id = 6969, .FirstName = "นางสาว", .LastName = "นมโต"}
'LastName มันไม่จัดเก็บลงฐานข้อมูล (ต้องสั่งได้ดั่งใจนึก)
Dim success = cn.Insert(Of Users)(objUser)
trans.Commit()
Catch ex As ArgumentException
trans.Rollback()
End Try
End Using
cn.Close() 'Close Connection
End Using
Date :
2019-06-28 07:35:52
By :
xxx
จาก #NO1 ถ้าคุณยังไม่ได้คำตอบ ก็ผ่า Source Code ของ .NET ออกมาดู (Textbox Binding)
--- ผมเชื่อว่าคุณทำได้
ผมเห็นคำถามหลากหลาย เกี่ยวกับ การเขียนคำสั่ง Insert/Update/Delete/Query อทิเช่น
Insert into table (Field1, Field2) values (value1, value2)
Update table set field1 = value1, field2 = value2 where 1=1
Delete table Where 1=1
ผมก็ยังงงงง ว่า มหาวิทยาลัยของไทย ทำไมมันสอนย่ำอยู่กับที่ องค์ความรู้คับแคบยิ่งนัก
Date :
2019-06-28 08:04:12
By :
xxx
ถ้าลงโค้ดมันก็ได้อยู่ครับ
แต่ใน Combobox คือ มันสร้าง BindingSource / Dataset/TableAdapter มาให้เราเลย โดยที่เราแค่ คลิกเลือกที่ data เองครับ
ผมอยากได้แบบนั้น แมะ (วันนี้รันเครื่อง แล้ว จะลองลุยต่ออีกรอบ)
Date :
2019-06-28 08:17:09
By :
lamaka.tor
@TOR_CHEMISTRY
www.thaicreate.com โชคดีที่มีคนอย่างคุณ ขออนุญาติเอาคำอ้างของท่านอื่นมาเล่าอีกครั้ง
--- คุณเป็นคนที่ซุกซนในภาษาโปรแกรมมิ่ง หายาก
Date :
2019-06-28 08:58:05
By :
xxx
user control คุณจะ inherit จากอะไรครับ
ก็แค่เพิ่ม property dataset เข้าไป เขียน get / set เพื่อใช้ อ้างอิง ในการทำ binding
โดยกำหนด ตัวแปร dataset มารับ ค่าจาก Property
และให้ component ใน user control นั้น อ้างอิง จาก ตัวแปร dataset
ประวัติการแก้ไข 2019-06-28 09:03:56
Date :
2019-06-28 09:02:02
By :
Chaidhanan
็Happy Birth day มหาเทพท่านหน้าฮี 28/06/2019
Date :
2019-06-28 09:07:47
By :
xxx
มหาเทพ Chaidhanan
เคยเขึยน Script Shell บน UNIX ไหมครับ ถ้าเคย พอมีตัวอย่างไหมครับ
ขอบพระคุณมากครับ
Date :
2019-06-28 09:24:08
By :
xxx
Error แบบนี้ เราลบ มันออกเลยได้ไม๊ครับ มีผลกับ โค้ดอื่นๆ ไม๊
ใน .net ที่โหลดมาก็ Error เหมือนกัน เลยไม่รู้ว่ามันอ้างอิงจากตรงไหนมาครับ
Date :
2019-06-28 10:09:38
By :
lamaka.tor
ท่านหน้าฮีเคยเล่าให้ผมฟังว่า ลองไปเล่น Blazor ดูซิ
ผมก็ได้แย้งท่านไปว่า ผมกลัวมันจะซ้ำรอยเดิม (ของดีแต่ไม่มีอนาคต)
--- Flash VS Blend สุดท้ายไม่ตายก็เหมือนตาย ทั้งคู่ (กอดคอกันตาย)
--- Windows Phone/นึกชื่อไม่ออก สมัย VS2005 -VS2008 มา VS2010 หายไปเลย
ท่านใช้ความเงียบแทนคำตอบ แต่ท่านพยักหน้า
--- และท่านก็รำพึงรำพัน "ก็ว่ากันไป ก็เมียกูหนีตามชู้ไป แล้วจะให้ทำอะไรได้"
ผมอดคิดสงสารไม่ได้ แต่ก็ไม่ได้รำพึงรำพันอะไร?
Date :
2019-06-28 10:37:32
By :
xxx
กระผมไม่รู้จริงฯ (ท่านหน้าฮี บอกผมว่าเอาไอ้นี่ไปทำ)
--- เอกสารหน้าเดียวก็ไม่มี มีแต่เอาไอ้นี่ไปทำ
งง งง งง
แต่ผมก็ไม่กล้าโต้แย้งอะไร?
Date :
2019-06-28 10:59:24
By :
xxx
แก้ครบ ไม่มี Error แล้ว แต่พอลากลง ฟอร์ม เจอเต็มๆ
โค้ด ครับ
Code (C#)
#region _DataBindings
private static readonly object EVENT_DATASOURCECHANGED = new object();
private static readonly object EVENT_DISPLAYMEMBERCHANGED = new object();
private static readonly object EVENT_VALUEMEMBERCHANGED = new object();
private static readonly object EVENT_SELECTEDVALUECHANGED = new object();
private object dataSource;
private CurrencyManager dataManager;
private BindingMemberInfo displayMember;
private BindingMemberInfo valueMember;
// Formatting stuff
private string formatString = String.Empty;
private static readonly object EVENT_FORMAT = new object();
private TypeConverter displayMemberConverter = null;
private bool isDataSourceInitialized;
private bool isDataSourceInitEventHooked;
private bool inSetDataConnection = false;
[
DefaultValue(null),
RefreshProperties(RefreshProperties.Repaint),
AttributeProvider(typeof(IListSource))
]
public object DataSource
{
get
{
return dataSource;
}
set
{
if (value != null && !(value is IList || value is IListSource))
throw new ArgumentException("BadDataSourceForComplexBinding");
if (dataSource == value)
return;
try
{
SetDataConnection(value, displayMember, false);
}
catch
{
DisplayMember = "";
}
if (value == null)
DisplayMember = "";
}
}
public event EventHandler DataSourceChanged
{
add
{
Events.AddHandler(EVENT_DATASOURCECHANGED, value);
}
remove
{
Events.RemoveHandler(EVENT_DATASOURCECHANGED, value);
}
}
protected CurrencyManager DataManager
{
get
{
return this.dataManager;
}
}
[
DefaultValue("")//,
//TypeConverterAttribute("System.Windows.Forms.Design.DataMemberFieldConverter, " ),
// Editor("System.Windows.Forms.Design.DataMemberFieldEditor, " , typeof(System.Drawing.Design.UITypeEditor))
]
public string DisplayMember
{
get
{
return displayMember.BindingMember;
}
set
{
BindingMemberInfo oldDisplayMember = displayMember;
try
{
SetDataConnection(dataSource, new BindingMemberInfo(value), false);
}
catch
{
displayMember = oldDisplayMember;
}
}
}
public event EventHandler DisplayMemberChanged
{
add
{
Events.AddHandler(EVENT_DISPLAYMEMBERCHANGED, value);
}
remove
{
Events.RemoveHandler(EVENT_DISPLAYMEMBERCHANGED, value);
}
}
private TypeConverter DisplayMemberConverter
{
get
{
if (this.displayMemberConverter == null &&
this.DataManager != null &&
this.displayMember != null)
{
PropertyDescriptorCollection props = this.DataManager.GetItemProperties();
if (props != null)
{
PropertyDescriptor displayMemberProperty = props.Find(this.displayMember.BindingField, true);
if (displayMemberProperty != null)
{
this.displayMemberConverter = displayMemberProperty.Converter;
}
}
}
return this.displayMemberConverter;
}
}
private bool BindingMemberInfoInDataManager(BindingMemberInfo bindingMemberInfo)
{
if (dataManager == null)
return false;
PropertyDescriptorCollection props = dataManager.GetItemProperties();
int propsCount = props.Count;
for (int i = 0; i < propsCount; i++)
{
if (typeof(IList).IsAssignableFrom(props[i].PropertyType))
continue;
if (props[i].Name.Equals(bindingMemberInfo.BindingField))
{
return true;
}
}
for (int i = 0; i < propsCount; i++)
{
if (typeof(IList).IsAssignableFrom(props[i].PropertyType))
continue;
if (String.Compare(props[i].Name, bindingMemberInfo.BindingField, true, CultureInfo.CurrentCulture) == 0)
{
return true;
}
}
return false;
}
/* [
DefaultValue(""),
Editor("System.Windows.Forms.Design.DataMemberFieldEditor, " + AssemblyRef.SystemDesign, typeof(System.Drawing.Design.UITypeEditor))
]*/
[
DefaultValue("")/*,
Editor("System.Windows.Forms.Design.DataMemberFieldEditor, ", typeof(System.Drawing.Design.UITypeEditor))*/
]
public string ValueMember
{
get
{
return valueMember.BindingMember;
}
set
{
if (value == null)
value = "";
BindingMemberInfo newValueMember = new BindingMemberInfo(value);
BindingMemberInfo oldValueMember = valueMember;
if (!newValueMember.Equals(valueMember))
{
if (DisplayMember.Length == 0)
SetDataConnection(DataSource, newValueMember, false);
if (this.dataManager != null && value != null && value.Length != 0)
if (!BindingMemberInfoInDataManager(newValueMember))
{
throw new ArgumentException("ListControlWrongValueMember ");
}
valueMember = newValueMember;
OnValueMemberChanged(EventArgs.Empty);
OnSelectedValueChanged(EventArgs.Empty);
}
}
}
public event EventHandler ValueMemberChanged
{
add
{
Events.AddHandler(EVENT_VALUEMEMBERCHANGED, value);
}
remove
{
Events.RemoveHandler(EVENT_VALUEMEMBERCHANGED, value);
}
}
protected virtual bool AllowSelection
{
get
{
return true;
}
}
protected object FilterItemOnProperty(object item)
{
return FilterItemOnProperty(item, displayMember.BindingField);
}
protected object FilterItemOnProperty(object item, string field)
{
if (item != null && field.Length > 0)
{
try
{
// if we have a dataSource, then use that to display the string
PropertyDescriptor descriptor;
if (this.dataManager != null)
descriptor = this.dataManager.GetItemProperties().Find(field, true);
else
descriptor = TypeDescriptor.GetProperties(item).Find(field, true);
if (descriptor != null)
{
item = descriptor.GetValue(item);
}
}
catch
{
}
}
return item;
}
//We use this to prevent getting the selected item when mouse is hovering over the dropdown.
//
internal bool BindingFieldEmpty
{
get
{
return (displayMember.BindingField.Length > 0 ? false : true);
}
}
protected override bool IsInputKey(Keys keyData)
{
if ((keyData & Keys.Alt) == Keys.Alt) return false;
switch (keyData & Keys.KeyCode)
{
case Keys.PageUp:
case Keys.PageDown:
case Keys.Home:
case Keys.End:
return true;
}
return base.IsInputKey(keyData);
}
protected override void OnBindingContextChanged(EventArgs e)
{
SetDataConnection(dataSource, displayMember, true);
base.OnBindingContextChanged(e);
}
protected virtual void OnDataSourceChanged(EventArgs e)
{
EventHandler eh = Events[EVENT_DATASOURCECHANGED] as EventHandler;
if (eh != null)
{
eh(this, e);
}
}
protected virtual void OnDisplayMemberChanged(EventArgs e)
{
EventHandler eh = Events[EVENT_DISPLAYMEMBERCHANGED] as EventHandler;
if (eh != null)
{
eh(this, e);
}
}
protected virtual void OnFormat(ListControlConvertEventArgs e)
{
ListControlConvertEventHandler eh = Events[EVENT_FORMAT] as ListControlConvertEventHandler;
if (eh != null)
eh(this, e);
}
protected virtual void OnSelectedIndexChanged(EventArgs e)
{
OnSelectedValueChanged(EventArgs.Empty);
}
protected virtual void OnValueMemberChanged(EventArgs e)
{
EventHandler eh = Events[EVENT_VALUEMEMBERCHANGED] as EventHandler;
if (eh != null)
{
eh(this, e);
}
}
protected virtual void OnSelectedValueChanged(EventArgs e)
{
EventHandler eh = Events[EVENT_SELECTEDVALUECHANGED] as EventHandler;
if (eh != null)
{
eh(this, e);
}
}
//protected abstract void RefreshItem(int index);
protected virtual void RefreshItems()
{
}
private void DataSourceDisposed(object sender, EventArgs e)
{
Debug.Assert(sender == this.dataSource, "how can we get dispose notification for anything other than our dataSource?");
SetDataConnection(null, new BindingMemberInfo(""), true);
}
private void DataSourceInitialized(object sender, EventArgs e)
{
ISupportInitializeNotification dsInit = (this.dataSource as ISupportInitializeNotification);
Debug.Assert(dsInit != null, "ListControl: ISupportInitializeNotification.Initialized event received, but current DataSource does not support ISupportInitializeNotification!");
Debug.Assert(dsInit.IsInitialized, "ListControl: DataSource sent ISupportInitializeNotification.Initialized event but before it had finished initializing.");
SetDataConnection(this.dataSource, this.displayMember, true);
}
private void SetDataConnection(object newDataSource, BindingMemberInfo newDisplayMember, bool force)
{
bool dataSourceChanged = dataSource != newDataSource;
bool displayMemberChanged = !displayMember.Equals(newDisplayMember);
if (inSetDataConnection)
{
return;
}
try
{
if (force || dataSourceChanged || displayMemberChanged)
{
inSetDataConnection = true;
IList currentList = this.DataManager != null ? this.DataManager.List : null;
bool currentManagerIsNull = this.DataManager == null;
UnwireDataSource();
dataSource = newDataSource;
displayMember = newDisplayMember;
WireDataSource();
if (isDataSourceInitialized)
{
CurrencyManager newDataManager = null;
if (newDataSource != null && BindingContext != null && !(newDataSource == Convert.DBNull))
{
newDataManager = (CurrencyManager)BindingContext[newDataSource, newDisplayMember.BindingPath];
}
if (dataManager != newDataManager)
{
dataManager = newDataManager;
}
if (dataManager != null && (displayMemberChanged || dataSourceChanged) && displayMember.BindingMember != null && displayMember.BindingMember.Length != 0)
{
if (!BindingMemberInfoInDataManager(displayMember))
throw new ArgumentException("ListControlWrongDisplayMember ");
}
}
this.displayMemberConverter = null;
}
if (dataSourceChanged)
{
OnDataSourceChanged(EventArgs.Empty);
}
if (displayMemberChanged)
{
OnDisplayMemberChanged(EventArgs.Empty);
}
}
finally
{
inSetDataConnection = false;
}
}
private void UnwireDataSource()
{
// If the source is a component, then unhook the Disposed event
if (this.dataSource is IComponent)
{
((IComponent)this.dataSource).Disposed -= new EventHandler(DataSourceDisposed);
}
ISupportInitializeNotification dsInit = (this.dataSource as ISupportInitializeNotification);
if (dsInit != null && isDataSourceInitEventHooked)
{
dsInit.Initialized -= new EventHandler(DataSourceInitialized);
isDataSourceInitEventHooked = false;
}
}
private void WireDataSource()
{
if (this.dataSource is IComponent)
{
((IComponent)this.dataSource).Disposed += new EventHandler(DataSourceDisposed);
}
ISupportInitializeNotification dsInit = (this.dataSource as ISupportInitializeNotification);
if (dsInit != null && !dsInit.IsInitialized)
{
dsInit.Initialized += new EventHandler(DataSourceInitialized);
isDataSourceInitEventHooked = true;
isDataSourceInitialized = false;
}
else
{
isDataSourceInitialized = true;
}
}
// protected abstract void SetItemsCore(IList items);
protected virtual void SetItemCore(int index, object value) { }
#endregion
ผมละนึกถึงเพลงนี้เลย
https://www.youtube.com/watch?v=7YiuUaHUqSg
แบบนี้ ผมต้องเขียน DataMemberFieldEditor หรือ ตัว Editor เองใช่ไม๊ครับ
ถ้าใช่ พอจะมีตัวอย่างให้แกะไม๊ครับ
Date :
2019-06-28 11:01:30
By :
lamaka.tor
ผมอดทนอดกลั้น และกล้าถามท่านหน้าฮีว่า ท่านจะให้ผมทำอะไรกับสิ่งที่ผมไม่รู้
ท่านหน้าฮีตอบว่า : กูก็ไม่รู้ เหมือนกัน
คำตอบเดียวที่ทำให้ผมหัวเราะได้ และคลายความกังวล
Date :
2019-06-28 11:09:07
By :
xxx
แจ่มแมว ไอ้ xxx
มันต้องอย่างนี้ซิ
Date :
2019-06-28 11:15:48
By :
หน้าฮี
@Chaidhanan
กรุณาส่ง Profile มาให้กระผมหน่อยครับ ผมขอร้อง
4000 x 30
[email protected]
Date :
2019-06-28 11:19:17
By :
หน้าฮี
ขี้เกียจอธิบายยาวเกิน เลยไปหามาให้
https://mystou.com/java-abstract-class/
แม้ไม่ใช้ c# แต่หลักการเหมือนกัน pascal ก็เหมือนกัน หลักการเขียน oop เหมือนกัน
ที่นี้ abstract class สามารถ สร้าง component แบบมีรูปได้
แต่ ผู้ที่จะ contain มัน ( parent class ) จะต้องมี รูป แค่ต้องกำหนด ให้มัน จะอยู่แบบลอยๆ ไม่ได้
เริ่มต้นคลาสแบบมีรูป อย่าทำแบบ abstract แบบง่ายที่สุดให้ inherit จาก component แบบมีรูป เช่น form หรือ panel
เมื่อก่อนผมก็คิดจะเขียน class component แบบมีรูป เอง อั๋ยยะ โคตรจะยุ่งยากเลย เอาวิธีง่ายๆ inherit เอาง่ายกว่าเยอะ
Date :
2019-06-28 11:24:18
By :
Chaidhanan
ขอบคุณครับ มหาเทพ Chaidhanan และผมขอบคุณแทนเจ้า xxx
อย่าลืมส่งของมาให้ผมนะครับ ผมขอร้อง
ผมไม่ได้สนใจ Delphi/C#/VB.NET/etc.. เขียนไม่เป็นยิ่งดี
ขอบคุณครับ มหาเทพของผม
Date :
2019-06-28 11:35:49
By :
หน้าฮี
@TOR_CHEMISTRY
ผมคิดว่าคุณงง และหลายฯคพวกของคุณ (มึง) ก็งง
ผมเห็น Source Code นิดเดียว ทำไมผมถึงเข้าใจ
ระดับนี้แล้ว กวาดสายตาดูแป๊บ ก็รู้แล้ว ผมซื้อหวยไม่เคยถูก
แต่ผมเสี่ยงทาย โปรแกรมเมอร์ ไม่เคยผิด
Date :
2019-06-28 12:01:53
By :
หน้าฮี
@Chaidhanan
ผมจริงฯเลยนะ วัยอายุของคุณ ทำไมหัวล้านวัยจัง
Date :
2019-06-28 12:20:11
By :
หน้าฮี
เอารูปพ่อมาลงหรือเปล่า? (ผมแค่สงสัย) +55555
Date :
2019-06-28 12:22:18
By :
หน้าฮี
หลังจากงม คลาสมาซักพัก ก็พอมีหวังครับ
มันมาแล้ว
Date :
2019-06-28 12:25:16
By :
lamaka.tor
@TOR_CHEMISTRY
เยี่ยม
Date :
2019-06-28 12:45:35
By :
หน้าฮี
ผมละเกิด ไอเดีย จะเขียน control จาก ListControl เหมือน ComboBox
แต่ ผมจะเขียนเป็นรูปนี้
ถ้า เริ่มจาก ListControl เราจะเขียนให้มันมี ปุ่ม มี textbox แบบนี้ ได้ยังไงครับ
อ่อ.... ลืมอีกอย่างครับ
เราจะเอา internal class ของ .net มาใช้ได้ยังไง ครับ
ประวัติการแก้ไข 2019-06-28 17:02:09
Date :
2019-06-28 17:01:06
By :
lamaka.tor
กระผมกราบ กระผมรับรู้ได้ นี่คืออาจารย์ของผม
กระผมขอบพระคุณมากครับ
Date :
2019-06-28 18:54:42
By :
หน้าฮี
หน้าฮีไม่เคยกราบใครง่ายฯ
Date :
2019-06-28 19:01:02
By :
หน้าฮี
ผมเคยบอกแล้ว เด็กอายุุน้อยกว่า 50 ปี อย่ามาคุยกับผม
ยิ่งแก่ยิ่งอายุมากผมยิ่งชอบ ครับอาจารย์ของผม
+55555
Date :
2019-06-28 19:18:03
By :
หน้าฮี
กะรผมหวังแต่เพียงว่า อาจารย์จะ กรุณาผม
(ไม่มีอะไรให้เราจดจำอีกแล้ว สำเร็จ/ล้มเหลว)
ไม่ต้องพะวง
ขอบคุณครับ
Date :
2019-06-28 19:22:27
By :
หน้าฮี
โค้ดล่าสุดครับ เผื่อใครจะเอาไปใช้
Code (C#)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using TORServices.ExtensionTor;
namespace TORServices.FormsTor
{
[
ComVisible(true),
ClassInterface(ClassInterfaceType.AutoDispatch),
LookupBindingProperties("DataSource", "DisplayMember", "ValueMember", "SelectedValue")
]
public partial class Textbox2AndButton : UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSearch = new System.Windows.Forms.Button();
this.txtID = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.txtDetail = new System.Windows.Forms.TextBox();
this.panel2 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// btnSearch
//
this.btnSearch.Dock = System.Windows.Forms.DockStyle.Right;
this.btnSearch.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.btnSearch.Image = global::TORServices.Properties.Resources.search_button32x32;
this.btnSearch.Location = new System.Drawing.Point(355, 0);
this.btnSearch.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(31, 31);
this.btnSearch.TabIndex = 0;
this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// txtID
//
this.txtID.Dock = System.Windows.Forms.DockStyle.Left;
this.txtID.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.txtID.Location = new System.Drawing.Point(0, 0);
this.txtID.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
this.txtID.Name = "txtID";
this.txtID.Size = new System.Drawing.Size(76, 29);
this.txtID.TabIndex = 1;
this.txtID.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtID_KeyDown);
this.txtID.Leave += new System.EventHandler(this.txtID_Leave);
//
// panel1
//
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.panel1.Location = new System.Drawing.Point(76, 0);
this.panel1.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(6, 31);
this.panel1.TabIndex = 3;
//
// txtDetail
//
this.txtDetail.BackColor = System.Drawing.SystemColors.Window;
this.txtDetail.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtDetail.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.txtDetail.Location = new System.Drawing.Point(82, 0);
this.txtDetail.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
this.txtDetail.Name = "txtDetail";
this.txtDetail.ReadOnly = true;
this.txtDetail.Size = new System.Drawing.Size(267, 29);
this.txtDetail.TabIndex = 4;
//
// panel2
//
this.panel2.Dock = System.Windows.Forms.DockStyle.Right;
this.panel2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.panel2.Location = new System.Drawing.Point(349, 0);
this.panel2.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(6, 31);
this.panel2.TabIndex = 5;
//
// Textbox2AndButton
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.txtDetail);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.txtID);
this.Controls.Add(this.btnSearch);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
this.Name = "Textbox2AndButton";
this.Size = new System.Drawing.Size(386, 31);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.TextBox txtID;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox txtDetail;
private DataTable tblSource;
private string _DisplayMember;
private string _ValueMember;
private Panel panel2;
private string stSelectedValue;
public Textbox2AndButton()
{
InitializeComponent();
}
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("TextID")]
public string TextID
{
get
{
return this.txtID.Text;
}
set
{
this.txtID.Text = value;
}
}
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("TextDetail")]
public string TextDetail
{
get
{
return this.txtDetail.Text;
}
set
{
this.txtDetail.Text = value;
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("ImageButton")]
public Image ImageButton
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return btnSearch.Image;
}
set
{
btnSearch.Image = value; Invalidate();
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Color text ID")]
public Color ColortextID
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return txtID.BackColor;
}
set
{
txtID.BackColor = value; Invalidate();
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Color text ID")]
public Color ColortextDetail
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return txtDetail.BackColor;
}
set
{
txtDetail.BackColor = value; Invalidate();
}
}
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
txtDetail.Font = Font;
txtID.Font = Font;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.Height = txtID.Height;
btnSearch.Width = btnSearch.Height;
}
public event EventHandler TextBoxTextChanged
{
add
{
txtID.TextChanged += value;
}
remove
{
txtID.TextChanged -= value;
}
}
public event EventHandler TextBoxDetailTextChanged
{
add
{
txtDetail.TextChanged += value;
}
remove
{
txtDetail.TextChanged -= value;
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
RefreshProperties(RefreshProperties.All),
ParenthesizePropertyName(true)
]
public ControlBindingsCollection DataBindings
{
get
{
ControlBindingsCollection bindings = (ControlBindingsCollection)this.txtID.DataBindings;
if (bindings == null)
{
bindings = new ControlBindingsCollection(this);
}
return bindings;
}
}
private object dataSource;
[
DefaultValue(null),
RefreshProperties(RefreshProperties.Repaint),
AttributeProvider(typeof(IListSource))
]
public object DataSource
{
get
{
return dataSource;
}
set
{
if (value != null && !(value is IList || value is IListSource))
throw new ArgumentException("SR.GetString(SR.BadDataSourceForComplexBinding)");
if (dataSource == value)
return;
}
}
/* [Bindable(true)]
[Category("Data")]
[Description("Set DataSource")]
public DataTable DataSource
{
get
{
return tblSource;
}
set
{
tblSource = value;
}
}*/
[Category("Data")]
[Editor(typeof(Textbox2AndButtonCaller), typeof(UITypeEditor))]
[Description("Set field name in DisplayMember")]
public string DisplayMember
{
get
{
return _DisplayMember;
}
set
{
_DisplayMember = value;
}
}
[Category("Data")]
[Editor(typeof(Textbox2AndButtonCaller), typeof(UITypeEditor))]
[Description("Set field name in ValueMember")]
public string ValueMember
{
get
{
return _ValueMember;
}
set
{
_ValueMember = value;
}
}
[Browsable(false)]
[DefaultValue("")]
public string SelectedValue
{
get { return stSelectedValue; }
set { stSelectedValue = value; }
}
private void btnSearch_Click(object sender, EventArgs e)
{
Form parent = this.FindForm();
Textbox2AndButtonChoseGrid popup = new Textbox2AndButtonChoseGrid(this);
popup.Location = new Point(parent.Location.X + this.Location.X + 4, parent.Location.Y + this.Location.Y + this.Height + 30);
popup.ShowDialog();
}
private void txtID_Leave(object sender, EventArgs e)
{
IDSearch();
}
private void IDSearch()
{
try
{
DataRow[] drs = tblSource.Select(ValueMember + " = " + txtID.Text);
if (drs.Length >= 0)
txtDetail.Text = drs[0][DisplayMember].ToString();
}
catch { MessageBox.Show("Error! \n Data not found"); }
}
private void txtID_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
IDSearch();
}
}
public class Textbox2AndButtonCaller : UITypeEditor
{
#region Variables declaration
private IWindowsFormsEditorService edSvc = null;
private ListBox cboTableNames = null;
#endregion
#region Override Methods
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
{
return UITypeEditorEditStyle.DropDown;
}
return base.GetEditStyle(context);
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context != null && context.Instance != null && provider != null)
{
Textbox2AndButton control = (Textbox2AndButton)context.Instance;
edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
cboTableNames = new ListBox();
cboTableNames.BorderStyle = BorderStyle.FixedSingle;
DataTable tbl = control.DataSource as DataTable;
for (int i = 0; i < tbl.Columns.Count; i++)
cboTableNames.Items.Add(tbl.Columns[i].ColumnName);
/* for (int i = 0; i < control.DataSource.Columns.Count; i++)
cboTableNames.Items.Add(control.DataSource.Columns[i].ColumnName);*/
cboTableNames.SelectedIndexChanged += new EventHandler(cboTableNames_SelectedIndexChanged);
edSvc.DropDownControl(cboTableNames);
return cboTableNames.SelectedItem;
}
}
return base.EditValue(context, provider, value);
}
#endregion
#region Events
private void cboTableNames_SelectedIndexChanged(object sender, EventArgs e)
{
cboTableNames.Text = (string)cboTableNames.SelectedItem;
edSvc.CloseDropDown();
}
#endregion
}
}
เป็นเรื่องที่น่าขันมาก ผมนั่งแก้เป็นวัน แต่สุดท้ายมันก็แก้จาก โค้ด เดิมนิดเดียว
แค่
เปลี่ยนจาก
ซึ่งเป็นโค้ดเดิมของคำถาม มาเป็น
แก้แค่เนี๊ยะ นั่งทั้งวัน 5555
คลิกแล้ว BindingSource / Dataset/TableAdapter มาเหมือน combobox เด๊ะๆ
แล้วก็เพิ่มโค้ด Fill เหมือน combobox เด๊ะๆ อีกเช่นกัน
ชอบใจมากกกก
แต่มันก็ยังติดอยู่อีกนั่นแหละครับ
พอเปลี่ยน DataSource จาก DataTable มาเป็น object
ปรากฎว่า โค้ดในการ สร้าง ListBox ของพวก DisplayMember และ ValueMember มีปัญหาคือมันไม่ขึ้นมาครับ
ตอนนี้ ขอความกรุณา ขอวิธี convert object to DataTable หรือ ดึง List ออกมาจาก object ที่เป็น DataSource ทีครับ
Date :
2019-06-29 09:36:52
By :
lamaka.tor
ตอนนี้แก้ ปัญหา เรียบร้อยแล้วครับ
โค้ดสมบูรณ์ ครับ แต่ขาดโค้ด popup นะครับ ใครอยากได้เดี๋ยวเอามาลงให้ครับ
Code (C#)
namespace TORServices.FormsTor
{
[
ComVisible(true),
ClassInterface(ClassInterfaceType.AutoDispatch),
LookupBindingProperties("DataSource", "DisplayMember", "ValueMember", "SelectedValue")
]
public partial class Text2BoxButton : UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtDetail = new System.Windows.Forms.TextBox();
this.panel2 = new System.Windows.Forms.Panel();
this.panel1 = new System.Windows.Forms.Panel();
this.txtID = new System.Windows.Forms.TextBox();
this.btnSearch = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtDetail
//
this.txtDetail.BackColor = System.Drawing.SystemColors.Window;
this.txtDetail.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtDetail.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.txtDetail.Location = new System.Drawing.Point(141, 0);
this.txtDetail.Margin = new System.Windows.Forms.Padding(7);
this.txtDetail.Name = "txtDetail";
this.txtDetail.ReadOnly = true;
this.txtDetail.Size = new System.Drawing.Size(310, 29);
this.txtDetail.TabIndex = 9;
//
// panel2
//
this.panel2.Dock = System.Windows.Forms.DockStyle.Right;
this.panel2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.panel2.Location = new System.Drawing.Point(451, 0);
this.panel2.Margin = new System.Windows.Forms.Padding(7);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(16, 31);
this.panel2.TabIndex = 10;
//
// panel1
//
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.panel1.Location = new System.Drawing.Point(121, 0);
this.panel1.Margin = new System.Windows.Forms.Padding(7);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(20, 31);
this.panel1.TabIndex = 8;
//
// txtID
//
this.txtID.Dock = System.Windows.Forms.DockStyle.Left;
this.txtID.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.txtID.Location = new System.Drawing.Point(0, 0);
this.txtID.Margin = new System.Windows.Forms.Padding(7);
this.txtID.Name = "txtID";
this.txtID.Size = new System.Drawing.Size(121, 29);
this.txtID.TabIndex = 7;
//
// btnSearch
//
this.btnSearch.Dock = System.Windows.Forms.DockStyle.Right;
this.btnSearch.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.btnSearch.Image = global::TORServices.Properties.Resources.search32x32;
this.btnSearch.Location = new System.Drawing.Point(467, 0);
this.btnSearch.Margin = new System.Windows.Forms.Padding(7);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(37, 31);
this.btnSearch.TabIndex = 6;
this.btnSearch.UseVisualStyleBackColor = true;
//
// Text2BoxButton
//
this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.txtDetail);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.txtID);
this.Controls.Add(this.btnSearch);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.Margin = new System.Windows.Forms.Padding(6);
this.Name = "Text2BoxButton";
this.Size = new System.Drawing.Size(504, 31);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtDetail;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox txtID;
private System.Windows.Forms.Button btnSearch;
public Text2BoxButton()
{
InitializeComponent();
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
this.txtID.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtID_KeyDown);
this.txtID.Leave += new System.EventHandler(this.txtID_Leave);
}
#region _databinding
private static readonly object EVENT_DATASOURCECHANGED = new object();
private static readonly object EVENT_DISPLAYMEMBERCHANGED = new object();
private static readonly object EVENT_VALUEMEMBERCHANGED = new object();
private static readonly object EVENT_SELECTEDVALUECHANGED = new object();
private static readonly object EVENT_FORMATINFOCHANGED = new object();
private static readonly object EVENT_FORMATSTRINGCHANGED = new object();
private static readonly object EVENT_FORMATTINGENABLEDCHANGED = new object();
private object dataSource;
private CurrencyManager dataManager;
private BindingMemberInfo displayMember;
private BindingMemberInfo valueMember;
// Formatting stuff
private string formatString = String.Empty;
private static readonly object EVENT_FORMAT = new object();
private TypeConverter displayMemberConverter = null;
private bool isDataSourceInitialized;
private bool isDataSourceInitEventHooked;
private bool inSetDataConnection = false;
[
DefaultValue(null),
RefreshProperties(RefreshProperties.Repaint),
AttributeProvider(typeof(IListSource))
]
public object DataSource
{
get
{
return dataSource;
}
set
{
if (value != null && !(value is IList || value is IListSource))
throw new ArgumentException("SR.GetString(SR.BadDataSourceForComplexBinding)");
if (dataSource == value)
return;
// When we change the dataSource to null, we should reset
// the displayMember to "". See ASURT 85662.
try
{
SetDataConnection(value, displayMember, false);
}
catch
{
DisplayMember = "";
}
if (value == null)
DisplayMember = "";
}
}
public event EventHandler DataSourceChanged
{
add
{
Events.AddHandler(EVENT_DATASOURCECHANGED, value);
}
remove
{
Events.RemoveHandler(EVENT_DATASOURCECHANGED, value);
}
}
/// <include file='doc\ListControl.uex' path='docs/doc[@for="ListControl.DataManager"]/*' />
protected CurrencyManager DataManager
{
get
{
return this.dataManager;
}
}
[
DefaultValue(""),
TypeConverterAttribute("System.Windows.Forms.Design.DataMemberFieldConverter"),
Editor("System.Windows.Forms.Design.DataMemberFieldEditor", typeof(System.Drawing.Design.UITypeEditor))
]
public string DisplayMember
{
get
{
return displayMember.BindingMember;
}
set
{
BindingMemberInfo oldDisplayMember = displayMember;
try
{
SetDataConnection(dataSource, new BindingMemberInfo(value), false);
}
catch
{
displayMember = oldDisplayMember;
}
}
}
public event EventHandler DisplayMemberChanged
{
add
{
Events.AddHandler(EVENT_DISPLAYMEMBERCHANGED, value);
}
remove
{
Events.RemoveHandler(EVENT_DISPLAYMEMBERCHANGED, value);
}
}
// Cached type converter of the property associated with the display member
private TypeConverter DisplayMemberConverter
{
get
{
if (this.displayMemberConverter == null &&
this.DataManager != null &&
this.displayMember != null)
{
PropertyDescriptorCollection props = this.DataManager.GetItemProperties();
if (props != null)
{
PropertyDescriptor displayMemberProperty = props.Find(this.displayMember.BindingField, true);
if (displayMemberProperty != null)
{
this.displayMemberConverter = displayMemberProperty.Converter;
}
}
}
return this.displayMemberConverter;
}
}
private bool BindingMemberInfoInDataManager(BindingMemberInfo bindingMemberInfo)
{
if (dataManager == null)
return false;
PropertyDescriptorCollection props = dataManager.GetItemProperties();
int propsCount = props.Count;
for (int i = 0; i < propsCount; i++)
{
if (typeof(IList).IsAssignableFrom(props[i].PropertyType))
continue;
if (props[i].Name.Equals(bindingMemberInfo.BindingField))
{
return true;
}
}
for (int i = 0; i < propsCount; i++)
{
if (typeof(IList).IsAssignableFrom(props[i].PropertyType))
continue;
if (String.Compare(props[i].Name, bindingMemberInfo.BindingField, true, CultureInfo.CurrentCulture) == 0)
{
return true;
}
}
return false;
}
[
DefaultValue(""),
Editor("System.Windows.Forms.Design.DataMemberFieldEditor", typeof(System.Drawing.Design.UITypeEditor))
]
public string ValueMember
{
get
{
return valueMember.BindingMember;
}
set
{
if (value == null)
value = "";
BindingMemberInfo newValueMember = new BindingMemberInfo(value);
BindingMemberInfo oldValueMember = valueMember;
if (!newValueMember.Equals(valueMember))
{
// If the displayMember is set to the EmptyString, then recreate the dataConnection
//
if (DisplayMember.Length == 0)
SetDataConnection(DataSource, newValueMember, false);
// See if the valueMember is a member of
// the properties in the dataManager
if (this.dataManager != null && value != null && value.Length != 0)
if (!BindingMemberInfoInDataManager(newValueMember))
{
throw new ArgumentException("SR.GetString(SR.ListControlWrongValueMember), value");
}
valueMember = newValueMember;
OnValueMemberChanged(EventArgs.Empty);
OnSelectedValueChanged(EventArgs.Empty);
}
}
}
public event EventHandler ValueMemberChanged
{
add
{
Events.AddHandler(EVENT_VALUEMEMBERCHANGED, value);
}
remove
{
Events.RemoveHandler(EVENT_VALUEMEMBERCHANGED, value);
}
}
protected virtual bool AllowSelection
{
get
{
return true;
}
}
public int SelectedIndex
{
get;
set;
}
private string stSelectedValue;
[Browsable(false)]
[DefaultValue("")]
public string SelectedValue
{
get { return stSelectedValue; }
set { stSelectedValue = value; }
}
protected object FilterItemOnProperty(object item)
{
return FilterItemOnProperty(item, displayMember.BindingField);
}
protected object FilterItemOnProperty(object item, string field)
{
if (item != null && field.Length > 0)
{
try
{
// if we have a dataSource, then use that to display the string
PropertyDescriptor descriptor;
if (this.dataManager != null)
descriptor = this.dataManager.GetItemProperties().Find(field, true);
else
descriptor = TypeDescriptor.GetProperties(item).Find(field, true);
if (descriptor != null)
{
item = descriptor.GetValue(item);
}
}
catch
{
}
}
return item;
}
protected virtual void OnDataSourceChanged(EventArgs e)
{
EventHandler eh = Events[EVENT_DATASOURCECHANGED] as EventHandler;
if (eh != null)
{
eh(this, e);
}
}
protected virtual void OnDisplayMemberChanged(EventArgs e)
{
EventHandler eh = Events[EVENT_DISPLAYMEMBERCHANGED] as EventHandler;
if (eh != null)
{
eh(this, e);
}
}
protected virtual void OnValueMemberChanged(EventArgs e)
{
EventHandler eh = Events[EVENT_VALUEMEMBERCHANGED] as EventHandler;
if (eh != null)
{
eh(this, e);
}
}
protected virtual void OnSelectedValueChanged(EventArgs e)
{
EventHandler eh = Events[EVENT_SELECTEDVALUECHANGED] as EventHandler;
if (eh != null)
{
eh(this, e);
}
}
private void DataSourceDisposed(object sender, EventArgs e)
{
Debug.Assert(sender == this.dataSource, "how can we get dispose notification for anything other than our dataSource?");
SetDataConnection(null, new BindingMemberInfo(""), true);
}
private void DataSourceInitialized(object sender, EventArgs e)
{
ISupportInitializeNotification dsInit = (this.dataSource as ISupportInitializeNotification);
Debug.Assert(dsInit != null, "Text2BoxButton: ISupportInitializeNotification.Initialized event received, but current DataSource does not support ISupportInitializeNotification!");
Debug.Assert(dsInit.IsInitialized, "Text2BoxButton: DataSource sent ISupportInitializeNotification.Initialized event but before it had finished initializing.");
SetDataConnection(this.dataSource, this.displayMember, true);
}
private void SetDataConnection(object newDataSource, BindingMemberInfo newDisplayMember, bool force)
{
bool dataSourceChanged = dataSource != newDataSource;
bool displayMemberChanged = !displayMember.Equals(newDisplayMember);
if (inSetDataConnection)
{
return;
}
try
{
if (force || dataSourceChanged || displayMemberChanged)
{
inSetDataConnection = true;
IList currentList = this.DataManager != null ? this.DataManager.List : null;
bool currentManagerIsNull = this.DataManager == null;
UnwireDataSource();
dataSource = newDataSource;
displayMember = newDisplayMember;
WireDataSource();
if (isDataSourceInitialized)
{
CurrencyManager newDataManager = null;
if (newDataSource != null && BindingContext != null && !(newDataSource == Convert.DBNull))
{
newDataManager = (CurrencyManager)BindingContext[newDataSource, newDisplayMember.BindingPath];
}
if (dataManager != null && (displayMemberChanged || dataSourceChanged) && displayMember.BindingMember != null && displayMember.BindingMember.Length != 0)
{
if (!BindingMemberInfoInDataManager(displayMember))
throw new ArgumentException("SR.GetString(SR.ListControlWrongDisplayMember), newDisplayMember");
}
}
this.displayMemberConverter = null;
}
if (dataSourceChanged)
{
OnDataSourceChanged(EventArgs.Empty);
}
if (displayMemberChanged)
{
OnDisplayMemberChanged(EventArgs.Empty);
}
}
finally
{
inSetDataConnection = false;
}
}
private void UnwireDataSource()
{
// If the source is a component, then unhook the Disposed event
if (this.dataSource is IComponent)
{
((IComponent)this.dataSource).Disposed -= new EventHandler(DataSourceDisposed);
}
ISupportInitializeNotification dsInit = (this.dataSource as ISupportInitializeNotification);
if (dsInit != null && isDataSourceInitEventHooked)
{
dsInit.Initialized -= new EventHandler(DataSourceInitialized);
isDataSourceInitEventHooked = false;
}
}
private void WireDataSource()
{
if (this.dataSource is IComponent)
{
((IComponent)this.dataSource).Disposed += new EventHandler(DataSourceDisposed);
}
ISupportInitializeNotification dsInit = (this.dataSource as ISupportInitializeNotification);
if (dsInit != null && !dsInit.IsInitialized)
{
dsInit.Initialized += new EventHandler(DataSourceInitialized);
isDataSourceInitEventHooked = true;
isDataSourceInitialized = false;
}
else
{
isDataSourceInitialized = true;
}
}
partial void SetItemsCore(IList items);
protected virtual void SetItemCore(int index, object value) { }
#endregion
#region Textbox
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("TextID")]
public string TextID
{
get
{
return this.txtID.Text;
}
set
{
this.txtID.Text = value;
}
}
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("TextDetail")]
public string TextDetail
{
get
{
return this.txtDetail.Text;
}
set
{
this.txtDetail.Text = value;
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("ImageButton")]
public Image ImageButton
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return btnSearch.Image;
}
set
{
btnSearch.Image = value; Invalidate();
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Color text ID")]
public Color ColortextID
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return txtID.BackColor;
}
set
{
txtID.BackColor = value; Invalidate();
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Color text ID")]
public Color ColortextDetail
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return txtDetail.BackColor;
}
set
{
txtDetail.BackColor = value; Invalidate();
}
}
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
txtDetail.Font = Font;
txtID.Font = Font;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.Height = txtID.Height;
btnSearch.Width = btnSearch.Height;
}
public event EventHandler TextBoxTextChanged
{
add
{
txtID.TextChanged += value;
}
remove
{
txtID.TextChanged -= value;
}
}
public event EventHandler TextBoxDetailTextChanged
{
add
{
txtDetail.TextChanged += value;
}
remove
{
txtDetail.TextChanged -= value;
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
RefreshProperties(RefreshProperties.All),
ParenthesizePropertyName(true)
]
public ControlBindingsCollection DataBindings
{
get
{
ControlBindingsCollection bindings = (ControlBindingsCollection)this.txtID.DataBindings;
if (bindings == null)
{
bindings = new ControlBindingsCollection(this);
}
return bindings;
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
Form parent = this.FindForm();
Text2ButtonChoseGrid popup = new Text2ButtonChoseGrid(this);
popup.Location = new Point(parent.Location.X + this.Location.X + 4, parent.Location.Y + this.Location.Y + this.Height + 30);
popup.ShowDialog();
}
private void txtID_Leave(object sender, EventArgs e)
{
IDSearch();
}
private void IDSearch()
{
try
{
BindingSource lst = (BindingSource)dataSource ;
DataRow[] drs = lst.ToDataTable().Select(ValueMember + " = " + txtID.Text);
if (drs.Length >= 0)
txtDetail.Text = drs[0][DisplayMember].ToString();
}
catch { MessageBox.Show("Error! \n Data not found"); }
}
private void txtID_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
IDSearch();
}
#endregion
}
}
ได้ตามต้องการเด๊ะๆ
ตัว BindingSource ก็เรียบร้อยแล้ว
กะว่าจะแก้ Popup ให้มันสวยๆ หน่อย ก็น่าจะใช้ได้แล้วครับ
Date :
2019-06-29 16:36:00
By :
lamaka.tor
Load balance : Server 02