abstract class CustomControl : UserControl
{
protected abstract int DoStuff();
}
class DetailControl : CustomControl
{
protected override int DoStuff()
{
// do stuff
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
我在表单中删除了一个DetailControl.它在运行时正确呈现,但设计器显示错误并且无法打开,因为基本用户控件是抽象的.
目前,我正在考虑以下补丁,这对我来说似乎很不对,因为我希望子类被强制实现该方法.
class CustomControl : UserControl
{
protected virtual int DoStuff()
{
throw new InvalidOperationException("This method must be overriden.");
}
}
class DetailControl : CustomControl
{
protected override int DoStuff()
{
// do stuff
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都有更好的想法如何解决这个问题?
我在一个类似于Visual Studio的IDE上工作,为我们的本地客户开发自定义Winform代码.在我们的代码中,我们覆盖了用户控件以使我们的任务更容易,但我们的大多数控件都是从基本的C#Winform控件派生的.
我目前需要帮助实现所有控件周围的虚线边框,以及Visual Studio提供的抓点类型.
未选择的控件
选定的控件
此功能非常需要,因为它可以帮助对齐而无需补偿视觉指导.
我们目前在所有控件周围实现了一个黑色边框
this.BackColor = Color.Black;
this.Height = ComboBox.Height + 4;
Run Code Online (Sandbox Code Playgroud)
这会在生成的控件周围放置一个黑色边框,在上面的代码片段中是一个ComboBox.
一位成员指出我们使用边缘和填充,如Microsoft文档中所示:https://msdn.microsoft.com/library/3z3f9e8b(v=vs.110)
但这主要是理论,并没有多大帮助.到目前为止,解决此问题的最接近的事情是在线CodeProject链接:
public class MyGroupBox : GroupBox
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawBorder(e.Graphics, ClientRectangle,
Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset,
Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset,
Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset,
Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset);
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我惊讶地发现我的搜索没有找到一个匹配的匹配,也许我使用了错误的术语,因为我最近进入了这个领域的编程.
我相信,如果这个问题得到解决,未来的在线搜索将会受益.期待指针形成那些有这个问题经验的人.非常感谢这方面的任何帮助.
有两种形式.Form2源自Form1.
但是我在设计模式下遇到了Form2的问题,如下面的屏幕截图所示.
如果我对此发表评论this._presenter.Retrive();,它将正常工作.是怎么回事以及如何解决这个问题?
UPD: 如果我将删除throw new NotImplementedException(); 并将插入,例如,MessageBox.Show("Test");,每次我打开Form2时,MessageBox都会出现,就像我运行应用程序一样.
窗体2
namespace InheritanceDemo
{
public partial class Form2 : Form1
{
public Form2()
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Form1中
namespace InheritanceDemo
{
public partial class Form1 : Form
{
protected IPresenter _presenter;
public Form1()
{
InitializeComponent();
_presenter = new Presenters();
}
private void Form1_Load(object sender, EventArgs e)
{
this._presenter.Retrive();
}
}
public class Presenters : IPresenter
{
public void Retrive()
{
throw new NotImplementedException(); …Run Code Online (Sandbox Code Playgroud) 我的问题不是如何在设计时进行调试。我实际上想通过设计器中可用的事件进行调试。我知道表单有加载和其他类型的事件。Windows 窗体设计器中是否有任何事件,例如 init、load 等?
我已经通过用户控件在 ASP 中进行了类似的调试。它允许我们在将用户控件添加到设计器之前查看其输出 HTML。
我知道 Windows 窗体和 ASP 是不同的,但是在实际呈现控件之前应该有一些事件来检查控件的值。
我的表单需要很长时间才能在 VS 设计器中打开。因此,我将调试器附加到 VisualStudio (devenv.exe),在我的 Form 的 InitializeComponent 中设置一个断点以逐步查看问题所在。但是,断点没有被击中。
我有一个项目,其中包含从基本形式(称为frmBase)继承属性的表单.我遇到了一个令我困惑的问题:
我希望程序以用户屏幕为中心,所以我补充说
this.CenterToScreen();
到frmBase_Load().当我运行应用程序时,这很有效,但是,当我尝试设计从frmBase继承的任何表单时,它们都被移动到设计器屏幕的右下角,我必须使用滚动条来查看它们.
如果我搬家了
this.CenterToScreen();
对于frmBase()代码,app在运行时默认为屏幕的左上角,但设计者会正确地为我显示表单.知道发生了什么事吗?我搜索过,但似乎找不到类似的问题,虽然我知道我不能成为第一个发生这种情况的人.....
我有这些课程:
class Foo<T1, T2> : Form
where T1, T2 : EventArgs
class MiddleGoo : Foo<X,Y>
class Goo : MiddleGoo
Run Code Online (Sandbox Code Playgroud)
X,Y只是从EventArgs派生的简单类.
我在设计师中看到Goo,但我想在Foo和Goo之间创建一个类Boo,如下所示:
class Boo<T1, Y> : Foo<T1, Y>
where T1 : EventArgs
class MiddleGoo : Boo<X,Y>
class Goo : MiddleGoo
Run Code Online (Sandbox Code Playgroud)
中产阶级的解决方法不起作用,任何想法?
编辑:我的意思是Y和X是像YEventArgs和XEventArgs这样的类,我的问题是当我将Y定义为T2但仍希望通过T1保持通用时,在设计器类Boo中查看.
编辑2:我刚刚意识到我拼错了Y级的东西......
public class Foo<T1, T2> : Form
where T1 : EventArgs
where T2 : EventArgs
{
}
public class Boo<T1> : Foo<T1, MyEventArgs2>
where T1 : EventArgs
{
}
public class MiddleGoo : Boo<MyEventArgs1>
{
}
class Goo : …Run Code Online (Sandbox Code Playgroud) 我正在创建一个设计器表面并将控件加载到运行时。将控件反序列化/加载到运行时时遇到问题。
我尝试过的所有方法似乎都有某种类型的问题。
发行面临例如:
我在 git 上创建了一个示例项目:Surface Designer Test
有主要的代码片段:
从设计时序列化
private void LoadRuntime(int type)
{
var controls = surface.ComponentContainer.Components;
SerializationStore data = (SerializationStore)surface.
_designerSerializationService.Serialize(controls);
MemoryStream ms = new MemoryStream();
data.Save(ms);
SaveData.Data = ms.ToArray();
SaveData.LoadType = type;
new RuntimeForm().Show();
}
public object Serialize(System.Collections.ICollection objects)
{
ComponentSerializationService componentSerializationService =
_serviceProvider.GetService(typeof(ComponentSerializationService)) as
ComponentSerializationService;
SerializationStore returnObject = null;
using (SerializationStore serializationStore =
componentSerializationService.CreateStore())
{
foreach (object obj in objects)
{
if (obj is Control control)
{
componentSerializationService.SerializeAbsolute(serializationStore, obj);
}
returnObject …Run Code Online (Sandbox Code Playgroud) 我想为设计时可浏览的窗体创建一个自定义属性,但我的努力都没有成功.显而易见的解决方案似乎是将browsable属性设置为true:
[Browsable(true),
EditorBrowsable(EditorBrowsableState.Always),
Description("Custom Border Colour"),
Category("Custom")]
public Color BorderColour
{
get
{
return bCol;
}
set
{
bCol = value;
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我已经为自定义控件做了很多次,它就像一个魅力,事实上,我甚至不需要添加属性,因为默认是真的.这个代码项目文章似乎做了我想要的,这就是我上面所描述的.MSDN也是一个死胡同,或者我不知道要搜索什么.
我试图添加代码Form1.cs,From1.Designer.cs但没有任何作用.
是否有一些我缺少的东西,比如我需要为表格设置的某些属性,或者它是不可能的?
我正在使用Visual Studio Express 2013,如果这会以任何方式影响结果.
编辑:在Reza回答后的尝试:根据Reza的建议,在这个问题中提出了关于这个主题的更详细的问题.
c# ×8
winforms ×8
.net ×3
attributes ×1
border ×1
codedom ×1
debugging ×1
generics ×1
inheritance ×1
properties ×1