我正在尝试编写自定义 WinForms 组件,我编写了几个简单的验证器组件,用于与自动连接验证事件的 ErrorProvider 子类一起使用。得益于 IExtenderProvider,所有这些组件都可以添加到表单中并仅使用设计器进行连接。
现在,在尝试更上一层楼的过程中,我正在尝试获得一个可与设计器一起使用的复合验证器。我可以启动它并使用代码,但这真的很容易。我想让它以仅限设计师的方式工作。
我的困难在于公开一个属性,该属性是具有相同形式的其他验证器的集合。所有验证器都直接继承自 Component,并实现了一个 IControlValidator 接口(interface)。如果有帮助,我愿意改变它,让它们从 ValidatorComponent 基类继承。
我想到了几个解决方案,但要么我不喜欢它们,要么我无法让它们工作:
将验证器做成不可见的控件,并让复合验证器包含它们,类似于面板所做的;
我不喜欢这个,因为它更像是一种 hack,不得不在真正的控件中兼顾它们感觉不对;
像使用工具栏一样使用集合编辑器;
我环顾了整个网络,发现了几篇关于此的文章,但我无法让它发挥作用。至少不用构建我自己的编辑器表单,这对于实验项目来说太麻烦了。
我承认我没有花太多时间尝试这个,因为我意识到使用标准的 CollectionEditor 会限制我使用一组固定的验证器类型(会,不是吗? ).
我还考虑过创建一个简单的 ValidatorReference 类,它具有类型为 IControlValidator 的单个属性,并将其用作简单集合编辑器的元素类型。然后我将添加其中之一,并在其属性网格中将属性设置为现有的验证器组件。这个看起来很容易上手,但失去了吸引力,因为它太明显了。
还有人有其他想法吗?有没有我遗漏的东西,这实际上很简单?
最佳答案
为什么不创建一个编辑器来做到这一点??? 您认为这听起来有点矫枉过正,但实际上并非如此。
我将用示例进行演示。
在此示例中,我将创建一个名为 ButtonActivityControl 的控件,它能够使用名为 Buttons 的属性对同一表单中的其他控件进行多次引用,即是 Button 类型的数组(即 Button[])。
该属性标有自定义编辑器,可以轻松引用页面中的控件。编辑器显示一个由选中列表框组成的表单,用于选择同一表单中的多个控件。
1) 一个名为 ReferencesCollectionEditorForm 的表单
ReferencesCollectionEditorForm 代码:
public partial class ReferencesCollectionEditorForm : Form
{
public ReferencesCollectionEditorForm(Control[] available, Control[] selected)
{
this.InitializeComponent();
List<Control> sel = new List<Control>(selected);
this.available = available;
if (available != null)
foreach (var eachControl in available)
this.checkedListBox1.Items.Add(new Item(eachControl),
selected != null && sel.Contains(eachControl));
}
class Item
{
public Item(Control ctl) { this.control = ctl; }
public Control control;
public override string ToString()
{
return this.control.GetType().Name + ": " + this.control.Name;
}
}
Control[] available;
public Control[] Selected
{
get
{
List<Control> selected = new List<Control>(this.available.Length);
foreach (Item eachItem in this.checkedListBox1.CheckedItems)
selected.Add(eachItem.control);
return selected.ToArray();
}
}
}
2) 一个 UITypeEditor
ReferencesCollectionEditor 代码:
public class ReferencesCollectionEditor : UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
List<Control> available = new List<Control>();
ButtonActivityControl control = context.Instance as ButtonActivityControl;
IDesignerHost host = provider.GetService(typeof(IDesignerHost)) as IDesignerHost;
IComponent componentHost = host.RootComponent;
if (componentHost is ContainerControl)
{
Queue<ContainerControl> containers = new Queue<ContainerControl>();
containers.Enqueue(componentHost as ContainerControl);
while (containers.Count > 0)
{
ContainerControl container = containers.Dequeue();
foreach (Control item in container.Controls)
{
if (item != null && context.PropertyDescriptor.PropertyType.GetElementType().IsAssignableFrom(item.GetType()))
available.Add(item);
if (item is ContainerControl)
containers.Enqueue(item as ContainerControl);
}
}
}
// collecting buttons in form
Control[] selected = (Control[])value;
// show editor form
ReferencesCollectionEditorForm form = new ReferencesCollectionEditorForm(available.ToArray(), selected);
form.ShowDialog();
// save new value
Array result = Array.CreateInstance(context.PropertyDescriptor.PropertyType.GetElementType(), form.Selected.Length);
for (int it = 0; it < result.Length; it++)
result.SetValue(form.Selected[it], it);
return result;
}
}
3) 一个控件以相同的形式使用其他控件
自定义控件代码:
public class ButtonActivityControl : Control, ISupportInitialize
{
[Editor(typeof(ReferencesCollectionEditor), typeof(UITypeEditor))]
public Button[] Buttons { get; set; }
Dictionary<Button, bool> map = new Dictionary<Button, bool>();
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.White, e.ClipRectangle);
if (this.Site != null) return; // this code is needed otherwise designer crashes when closing
int h = e.ClipRectangle.Height / this.Buttons.Length;
int top = 0;
foreach (var button in this.Buttons)
{
e.Graphics.FillRectangle(map[button] ? Brushes.Black : Brushes.White, new Rectangle(0, top, e.ClipRectangle.Width, h));
top += h;
}
base.OnPaint(e);
}
void ISupportInitialize.BeginInit()
{
}
void ISupportInitialize.EndInit()
{
if (this.Site != null) return; // this is needed so that designer does not change the colors of the buttons in design-time
foreach (var button in this.Buttons)
{
button.Click += new EventHandler(button_Click);
button.ForeColor = Color.Blue;
map[button] = false;
}
}
void button_Click(object sender, EventArgs e)
{
map[(Button)sender] = !map[(Button)sender];
this.Invalidate();
}
}
现在创建一个包含自定义控件的窗体,在上面放置一些按钮,然后在上面放置一个 ButtonActivityControl。自定义控件有一个名为 Buttons 的属性,该属性是可编辑的。
就是这样!
没有理由害怕自定义编辑器……而且没那么复杂…… 半小时内完成。
我认为这就是答案……也就是说,我认为是! =) 也许我没有很好地理解这个问题……但这是最好的办法:努力帮助别人!
编辑
ReferencesCollectionEditor 中需要此代码:
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return false;
}
关于c# - 如何使设计器可以使用复合组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5642715/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co