如何访问继承表单上的控件?
我在我的 winforms 应用程序中使用 DockPanel Suite。 DockContent 类派生自 System.Windows.Forms.Form 类,我的两个窗体,dockRounds 和dockToolbox,继承自 DockContent 类。
这是我第一次这样做,这可能是一个愚蠢的问题,但在运行时,如何访问dockRounds和dockToolbox表单的控件?
以下是应用程序首次运行时加载两个表单的方法:
publicpartial class frmMainNew : Form
clsMWDockPanel mapPanel;
dockToolbox dockT = new dockToolbox();
dockRounds dockR = new dockRounds();
public frmMainNew()
{
InitializeComponent();
dockPanel = new DockPanel();
SuspendLayout();
dockPanel.Parent = panelMain;
dockPanel.Dock = DockStyle.Fill;
dockPanel.DefaultFloatWindowSize = new Size(108, 528);
dockPanel.BringToFront();
dockPanel.BackColor = Color.Transparent;
dockPanel.DocumentStyle = DocumentStyle.DockingSdi;
ResumeLayout();
string error = "Errors:\r\n";
try
{
loadRounds();
loadToolbox();
}
catch (Exception)
{
error = error + "The Toolbox and/or Rounds menu could not be created\r\n";
}
}
public void loadToolbox()
{
dockT.CloseButton = false;
dockT.ShowHint = DockState.Float;
dockT.Text = "Toolbox";
dockT.BackColor = Color.WhiteSmoke;
dockT.Icon = this.Icon;
dockT.Show(dockPanel);
}
public void loadRounds()
{
if (mapPanel == null)
{
CreateMapPanel().Show(dockPanel, DockState.Document);
}
mapMain.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
//mapMain.BringToFront();
dockR.CloseButton = false;
dockR.ShowHint = DockState.DockRightAutoHide;
dockR.Text = "Rounds Menu";
dockR.BackColor = Color.WhiteSmoke;
dockR.Icon = this.Icon;
dockR.Show(dockPanel);
}
DockContent CreateMapPanel()
{
mapPanel = new clsMWDockPanel();
mapPanel.ShowHint = DockState.Document;
mapPanel.Controls.Add(mapMain);
return mapPanel;
}
提前非常感谢
leddy
I'm using the DockPanel Suite in my winforms app. The DockContent class is derived from System.Windows.Forms.Form class and my two forms, dockRounds and dockToolbox, inherit from the DockContent class.
This is the first time I've done this and this is probably a stupid question, but in runtime, how do I access the controls of the dockRounds and dockToolbox forms?
Here is how I load the two forms when the app first runs:
public partial class frmMainNew : Form
clsMWDockPanel mapPanel;
dockToolbox dockT = new dockToolbox();
dockRounds dockR = new dockRounds();
public frmMainNew()
{
InitializeComponent();
dockPanel = new DockPanel();
SuspendLayout();
dockPanel.Parent = panelMain;
dockPanel.Dock = DockStyle.Fill;
dockPanel.DefaultFloatWindowSize = new Size(108, 528);
dockPanel.BringToFront();
dockPanel.BackColor = Color.Transparent;
dockPanel.DocumentStyle = DocumentStyle.DockingSdi;
ResumeLayout();
string error = "Errors:\r\n";
try
{
loadRounds();
loadToolbox();
}
catch (Exception)
{
error = error + "The Toolbox and/or Rounds menu could not be created\r\n";
}
}
public void loadToolbox()
{
dockT.CloseButton = false;
dockT.ShowHint = DockState.Float;
dockT.Text = "Toolbox";
dockT.BackColor = Color.WhiteSmoke;
dockT.Icon = this.Icon;
dockT.Show(dockPanel);
}
public void loadRounds()
{
if (mapPanel == null)
{
CreateMapPanel().Show(dockPanel, DockState.Document);
}
mapMain.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
//mapMain.BringToFront();
dockR.CloseButton = false;
dockR.ShowHint = DockState.DockRightAutoHide;
dockR.Text = "Rounds Menu";
dockR.BackColor = Color.WhiteSmoke;
dockR.Icon = this.Icon;
dockR.Show(dockPanel);
}
DockContent CreateMapPanel()
{
mapPanel = new clsMWDockPanel();
mapPanel.ShowHint = DockState.Document;
mapPanel.Controls.Add(mapMain);
return mapPanel;
}
Many thanks in advance
leddy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用多种策略来实现不同表单上的对象之间的通信/链接。注意:我在这里的回复不会解决与 DockPanelSuite 特别相关的任何问题,也不会考虑“辅助”表单是“独立”的(即它们不属于 MainForm“拥有”)还是被主表单“拥有”之间的区别。制作 MainForm 的子窗体。这是一个有意识的选择,其基础是相信您所询问的内容独立于实施中可能的变化。
在 Form2 中定义一个属性,如下所示:
现在在主窗体的 Load 事件中,假设这是创建 Form2 实例的地方,您可以订阅 Form2 上按钮的 Click 事件:
并且您可以轻松地想象在 Form3 中您有一个 TextBox,您已使用公共属性公开了该文本框,就像您在 Form2 上公开按钮一样。
因此,您可以为 Form2 上的按钮单击实现 MainForm 的事件处理程序:
... 其他策略 ...
在您的辅助表单中,例如,按下按钮可以引发主表单(或任何其他暴露于 Form2 的实体)都可以订阅,然后将适当的内容分派到适当的目标。
您可以在更高级别将一般消息传递抽象为单个(可能是静态)类,发布者在其中发送消息,并将消息分派给注册的侦听器。
最后,您可能会对这里的讨论感兴趣:
将一种表单的控件使用到另一种表单中
最好,
There are several strategies you can use to achieve communication/linkage between objects on different Forms. Note : My reply here is not going to address any issues specifically related to DockPanelSuite, and is not going to consider the difference between the "secondary" forms being "independent" (i.e., they are not "owned" by the MainForm) or being made child Forms of the MainForm. This is a conscious choice made on the basis of believing that what you are asking about is independent of those possible variations in implementation.
In Form2 define a property like :
Now in the Load event of your Main Form, assuming that's where an instance of Form2 is created, you can subscribe to the Click event of the Button on Form2 :
And you can easily imagine that in Form3 you have a TextBox that you have exposed with a Public Property in the same way you exposed the Button on Form2.
So you can implement the MainForm's event handler like this for the Button click on Form2 :
... other strategies ...
in your secondary form, for example, a button press can raise a Public Event which the Main Form (or any other entity to which Form2 is exposed) could subscribe to and then dispatch the appropriate whatever to the appropriate target.
you can abstract message-passing in general at a higher level into a single (perhaps static) class where publishers send messages, and the messages are dispatched to registered listeners.
Finally, the discussion here may be of interest to you :
Using The Controls Of One Form Into Another
best,
您的类、dockRounds 和dockToolbox 应公开您想要访问的任何属性/事件。因此,如果您想连接到控件的事件,请将其路由到公共事件。
Your classes, dockRounds and dockToolbox should expose any properties/events that you want to access. So if you want to hook up to a control's event, route it to a public event.
您可以在控件上设置访问修饰符,以使其可以随心所欲地访问。默认值为“私有”,这就是您无法从主窗体访问控件的原因。
在 Visual Studio 中的“属性”选项卡上,有一个“修饰符”属性,用于设置在生成的设计器文件中使用的访问修饰符。
将其设置为“公共”,您就可以从主窗体访问该控件。
当我从另一种形式继承一种形式时,我刚刚使用了它。通过将修饰符设置为“受保护”(或“受保护内部”),我能够访问基类中定义的控件。
You can set the access modifier on a control to make it as accessible as you like. The default is "Private" which is why you can't access the controls from the main form.
In Visual Studio, on the Properties tab, there is a Modifiers property which sets the access modifier that is used in the generated designer file.
Set this to "Public" and you be able to access the control from the main form.
I've just used this when I inherited one form from another. By setting the modifier to "Protected" (or "Protected Internal") I was able to access the controls defined in the base class.
您是否尝试过访问控件财产?
Have you tried accessing the Controls property?