J
Johno76
Guest
I have a polymorphic thread sized defined as below :
[Serializable]
public class MetricThreadSizeType : ThreadSizeType
{
private int diameter; // mm
public MetricThreadSizeType()
: base()
{
this.diameter = 4;
}
private MetricThreadSizeType(int diameter)
{
this.diameter = diameter;
}
public int Diameter
{
get { return diameter; }
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
MetricThreadSizeType that = (MetricThreadSizeType)obj;
return this.diameter == that.diameter;
}
public override string ToString()
{
return "M" + diameter;
}
public static MetricThreadSizeType[] Sizes
{
get
{
return new MetricThreadSizeType[]
{
M4,
M5,
M6,
M8,
M10,
M12,
M14,
M16,
M20,
M22,
M24
};
}
}
// constants
public static MetricThreadSizeType M4 = new MetricThreadSizeType(4);
public static MetricThreadSizeType M5 = new MetricThreadSizeType(5);
public static MetricThreadSizeType M6 = new MetricThreadSizeType(6);
public static MetricThreadSizeType M8 = new MetricThreadSizeType(8);
public static MetricThreadSizeType M10 = new MetricThreadSizeType(10);
public static MetricThreadSizeType M12 = new MetricThreadSizeType(12);
public static MetricThreadSizeType M14 = new MetricThreadSizeType(14);
public static MetricThreadSizeType M16 = new MetricThreadSizeType(16);
public static MetricThreadSizeType M20 = new MetricThreadSizeType(20);
public static MetricThreadSizeType M22 = new MetricThreadSizeType(22);
public static MetricThreadSizeType M24 = new MetricThreadSizeType(24);
}
andn
[Serializable]
public class ImperialThreadSizeType : ThreadSizeType
{
private Fraction diameter;
public ImperialThreadSizeType()
: base()
{ this.diameter = new Fraction(3, 16); }
private ImperialThreadSizeType(Fraction diameter)
{
this.diameter = diameter;
}
private ImperialThreadSizeType(int numerator, int denominator)
{
this.diameter = new Fraction(numerator, denominator);
}
public Fraction Diameter
{
get { return diameter; }
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
ImperialThreadSizeType that = (ImperialThreadSizeType)obj;
return this.diameter.Equals(that.diameter);
}
public override string ToString()
{
return diameter + "\"";
}
public static ImperialThreadSizeType[] Sizes
{
get
{
return new ImperialThreadSizeType[]
{
IN_3_16,
IN_1_4,
IN_5_16,
IN_3_8,
IN_7_16,
IN_1_2
};
}
}
// constants
public static ImperialThreadSizeType IN_3_16 = new ImperialThreadSizeType(3, 16);
public static ImperialThreadSizeType IN_1_4 = new ImperialThreadSizeType(1, 4);
public static ImperialThreadSizeType IN_5_16 = new ImperialThreadSizeType(5, 16);
public static ImperialThreadSizeType IN_3_8 = new ImperialThreadSizeType(3, 8);
public static ImperialThreadSizeType IN_7_16 = new ImperialThreadSizeType(7, 16);
public static ImperialThreadSizeType IN_1_2 = new ImperialThreadSizeType(1, 2);
}
OK. Then I have built this control which allows the user to select either metric or imperial threads
public partial class ThreadControl : UserControl
{
private bool useMetric;
public event EventHandler ValueChanged;
public ThreadControl()
{
InitializeComponent();
// imperial threads
cbImperialThread.DataSource = ImperialThreadSizeType.Sizes;
cbImperialThread.SelectedIndex = 0;
// metric threads
cbMetricThread.DataSource = MetricThreadSizeType.Sizes;
cbMetricThread.SelectedIndex = 0;
}
private void cbMetricThread_SelectedIndexChanged(object sender, EventArgs e)
{
useMetric = true;
cbImperialThread.BackColor = Color.LightGray;
cbMetricThread.BackColor = Color.White;
OnValueChanged(e);
}
private void cbImperialThread_SelectedIndexChanged(object sender, EventArgs e)
{
useMetric = false;
cbMetricThread.BackColor = Color.LightGray;
cbImperialThread.BackColor = Color.White;
OnValueChanged(e);
}
protected void OnValueChanged(EventArgs e)
{
if (ValueChanged != null)
ValueChanged(this, e);
}
public ThreadSizeType ThreadSize
{
get
{
if (useMetric == true)
return (MetricThreadSizeType)cbMetricThread.SelectedItem;
else
return (ImperialThreadSizeType)cbImperialThread.SelectedItem;
}
set
{
if (value != null)
{
if (value is MetricThreadSizeType)
{
cbMetricThread.SelectedItem = (MetricThreadSizeType)value;
}
else
cbImperialThread.SelectedItem = (ImperialThreadSizeType)value;
}
}
}
//public MetricThreadSizeType MetricThreadSize
//{
// get { return (MetricThreadSizeType)cbMetricThread.SelectedItem; }
// set { cbMetricThread.SelectedItem = value; }
//}
//public ImperialThreadSizeType ImperialThreadSize
//{
// get { return (ImperialThreadSizeType)cbImperialThread.SelectedItem; }
// set { cbImperialThread.SelectedItem = value; }
//}
}
AND
partial class ThreadControl
{
/// <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.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.cbMetricThread = new System.Windows.Forms.ComboBox();
this.cbImperialThread = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(180, 4);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(23, 13);
this.label2.TabIndex = 11;
this.label2.Text = "OR";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(1, 3);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 13);
this.label1.TabIndex = 10;
this.label1.Text = "Thread Size";
//
// cbMetricThread
//
this.cbMetricThread.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.cbMetricThread.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbMetricThread.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.cbMetricThread.FormattingEnabled = true;
this.cbMetricThread.Location = new System.Drawing.Point(104, 0);
this.cbMetricThread.Name = "cbMetricThread";
this.cbMetricThread.Size = new System.Drawing.Size(70, 21);
this.cbMetricThread.TabIndex = 9;
this.cbMetricThread.SelectedIndexChanged += new System.EventHandler(this.cbMetricThread_SelectedIndexChanged);
//
// cbImperialThread
//
this.cbImperialThread.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.cbImperialThread.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbImperialThread.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.cbImperialThread.FormattingEnabled = true;
this.cbImperialThread.Location = new System.Drawing.Point(209, 0);
this.cbImperialThread.Name = "cbImperialThread";
this.cbImperialThread.Size = new System.Drawing.Size(70, 21);
this.cbImperialThread.TabIndex = 8;
this.cbImperialThread.SelectedIndexChanged += new System.EventHandler(this.cbImperialThread_SelectedIndexChanged);
//
// ThreadControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.cbMetricThread);
this.Controls.Add(this.cbImperialThread);
this.Name = "ThreadControl";
this.Size = new System.Drawing.Size(279, 22);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox cbMetricThread;
private System.Windows.Forms.ComboBox cbImperialThread;
}
But when I add it to a form and run it I get this error:
Error Invalid Resx file. Could not load type TestFastenerSizes.MetricThreadSizeType, TestFastenerSizes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null which is used in the .RESX file. Ensure that the necessary references have been added to your project. Line 126, position 5. TestFastenerSizes E:\Dave's Documents\Visual C#\TestFastenerSizes\TestFastenerSizes\Form1.resx 126
Do I need default constructors for MetricThreadSizeType and/or ImperialThreadSizeTypes? What am I doing wrong that the control can't set itself up properly?
Thanks in advance. David
Continue reading...
[Serializable]
public class MetricThreadSizeType : ThreadSizeType
{
private int diameter; // mm
public MetricThreadSizeType()
: base()
{
this.diameter = 4;
}
private MetricThreadSizeType(int diameter)
{
this.diameter = diameter;
}
public int Diameter
{
get { return diameter; }
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
MetricThreadSizeType that = (MetricThreadSizeType)obj;
return this.diameter == that.diameter;
}
public override string ToString()
{
return "M" + diameter;
}
public static MetricThreadSizeType[] Sizes
{
get
{
return new MetricThreadSizeType[]
{
M4,
M5,
M6,
M8,
M10,
M12,
M14,
M16,
M20,
M22,
M24
};
}
}
// constants
public static MetricThreadSizeType M4 = new MetricThreadSizeType(4);
public static MetricThreadSizeType M5 = new MetricThreadSizeType(5);
public static MetricThreadSizeType M6 = new MetricThreadSizeType(6);
public static MetricThreadSizeType M8 = new MetricThreadSizeType(8);
public static MetricThreadSizeType M10 = new MetricThreadSizeType(10);
public static MetricThreadSizeType M12 = new MetricThreadSizeType(12);
public static MetricThreadSizeType M14 = new MetricThreadSizeType(14);
public static MetricThreadSizeType M16 = new MetricThreadSizeType(16);
public static MetricThreadSizeType M20 = new MetricThreadSizeType(20);
public static MetricThreadSizeType M22 = new MetricThreadSizeType(22);
public static MetricThreadSizeType M24 = new MetricThreadSizeType(24);
}
andn
[Serializable]
public class ImperialThreadSizeType : ThreadSizeType
{
private Fraction diameter;
public ImperialThreadSizeType()
: base()
{ this.diameter = new Fraction(3, 16); }
private ImperialThreadSizeType(Fraction diameter)
{
this.diameter = diameter;
}
private ImperialThreadSizeType(int numerator, int denominator)
{
this.diameter = new Fraction(numerator, denominator);
}
public Fraction Diameter
{
get { return diameter; }
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
ImperialThreadSizeType that = (ImperialThreadSizeType)obj;
return this.diameter.Equals(that.diameter);
}
public override string ToString()
{
return diameter + "\"";
}
public static ImperialThreadSizeType[] Sizes
{
get
{
return new ImperialThreadSizeType[]
{
IN_3_16,
IN_1_4,
IN_5_16,
IN_3_8,
IN_7_16,
IN_1_2
};
}
}
// constants
public static ImperialThreadSizeType IN_3_16 = new ImperialThreadSizeType(3, 16);
public static ImperialThreadSizeType IN_1_4 = new ImperialThreadSizeType(1, 4);
public static ImperialThreadSizeType IN_5_16 = new ImperialThreadSizeType(5, 16);
public static ImperialThreadSizeType IN_3_8 = new ImperialThreadSizeType(3, 8);
public static ImperialThreadSizeType IN_7_16 = new ImperialThreadSizeType(7, 16);
public static ImperialThreadSizeType IN_1_2 = new ImperialThreadSizeType(1, 2);
}
OK. Then I have built this control which allows the user to select either metric or imperial threads
public partial class ThreadControl : UserControl
{
private bool useMetric;
public event EventHandler ValueChanged;
public ThreadControl()
{
InitializeComponent();
// imperial threads
cbImperialThread.DataSource = ImperialThreadSizeType.Sizes;
cbImperialThread.SelectedIndex = 0;
// metric threads
cbMetricThread.DataSource = MetricThreadSizeType.Sizes;
cbMetricThread.SelectedIndex = 0;
}
private void cbMetricThread_SelectedIndexChanged(object sender, EventArgs e)
{
useMetric = true;
cbImperialThread.BackColor = Color.LightGray;
cbMetricThread.BackColor = Color.White;
OnValueChanged(e);
}
private void cbImperialThread_SelectedIndexChanged(object sender, EventArgs e)
{
useMetric = false;
cbMetricThread.BackColor = Color.LightGray;
cbImperialThread.BackColor = Color.White;
OnValueChanged(e);
}
protected void OnValueChanged(EventArgs e)
{
if (ValueChanged != null)
ValueChanged(this, e);
}
public ThreadSizeType ThreadSize
{
get
{
if (useMetric == true)
return (MetricThreadSizeType)cbMetricThread.SelectedItem;
else
return (ImperialThreadSizeType)cbImperialThread.SelectedItem;
}
set
{
if (value != null)
{
if (value is MetricThreadSizeType)
{
cbMetricThread.SelectedItem = (MetricThreadSizeType)value;
}
else
cbImperialThread.SelectedItem = (ImperialThreadSizeType)value;
}
}
}
//public MetricThreadSizeType MetricThreadSize
//{
// get { return (MetricThreadSizeType)cbMetricThread.SelectedItem; }
// set { cbMetricThread.SelectedItem = value; }
//}
//public ImperialThreadSizeType ImperialThreadSize
//{
// get { return (ImperialThreadSizeType)cbImperialThread.SelectedItem; }
// set { cbImperialThread.SelectedItem = value; }
//}
}
AND
partial class ThreadControl
{
/// <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.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.cbMetricThread = new System.Windows.Forms.ComboBox();
this.cbImperialThread = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(180, 4);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(23, 13);
this.label2.TabIndex = 11;
this.label2.Text = "OR";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(1, 3);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 13);
this.label1.TabIndex = 10;
this.label1.Text = "Thread Size";
//
// cbMetricThread
//
this.cbMetricThread.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.cbMetricThread.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbMetricThread.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.cbMetricThread.FormattingEnabled = true;
this.cbMetricThread.Location = new System.Drawing.Point(104, 0);
this.cbMetricThread.Name = "cbMetricThread";
this.cbMetricThread.Size = new System.Drawing.Size(70, 21);
this.cbMetricThread.TabIndex = 9;
this.cbMetricThread.SelectedIndexChanged += new System.EventHandler(this.cbMetricThread_SelectedIndexChanged);
//
// cbImperialThread
//
this.cbImperialThread.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.cbImperialThread.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbImperialThread.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.cbImperialThread.FormattingEnabled = true;
this.cbImperialThread.Location = new System.Drawing.Point(209, 0);
this.cbImperialThread.Name = "cbImperialThread";
this.cbImperialThread.Size = new System.Drawing.Size(70, 21);
this.cbImperialThread.TabIndex = 8;
this.cbImperialThread.SelectedIndexChanged += new System.EventHandler(this.cbImperialThread_SelectedIndexChanged);
//
// ThreadControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.cbMetricThread);
this.Controls.Add(this.cbImperialThread);
this.Name = "ThreadControl";
this.Size = new System.Drawing.Size(279, 22);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox cbMetricThread;
private System.Windows.Forms.ComboBox cbImperialThread;
}
But when I add it to a form and run it I get this error:
Error Invalid Resx file. Could not load type TestFastenerSizes.MetricThreadSizeType, TestFastenerSizes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null which is used in the .RESX file. Ensure that the necessary references have been added to your project. Line 126, position 5. TestFastenerSizes E:\Dave's Documents\Visual C#\TestFastenerSizes\TestFastenerSizes\Form1.resx 126
Do I need default constructors for MetricThreadSizeType and/or ImperialThreadSizeTypes? What am I doing wrong that the control can't set itself up properly?
Thanks in advance. David
Continue reading...