简体   繁体   English

c# - 创建不可见的用户控件

[英]c# - create invisible user control

I need to create a user control in C#.Net, which can be added to the application without being visible - just like the FolderBrowserDialog. 我需要在C#.Net中创建一个用户控件,可以将其添加到应用程序中而不会显示 - 就像FolderBrowserDialog一样。 It's a new window which I'll be using often so I think this is the right way. 这是一个我将经常使用的新窗口,所以我认为这是正确的方法。 The window will be opened by envoking the showDialog-Method as known from the other dialog. 通过从其他对话框中获取showvialog-Method来打开窗口。

Any Idea? 任何想法? Thanks and regards, 谢谢并恭祝安康,

Daniel 丹尼尔

由于所有这些“不可见”控件都派生自Component类,因此您应该首先阅读它上面的MSDN文章: http//msdn.microsoft.com/en-us/library/system.componentmodel.component.aspx

只是将Visible设置为false或者这不是你要求的?

A UserControl is by definition not a Form ; 根据定义, UserControl不是Form ; I think what you really want is a Component . 我认为你真正想要的是一个Component That said, couldn't you really just create a new Form class that has the functionality you want? 那就是说,你真的不能创建一个具有你想要的功能的新Form类吗? Whenever you want to display it, create a new instance and call ShowDialog . 无论何时想要显示它,都要创建一个新实例并调用ShowDialog Or, if you want to preserve state, add an instance as a class member to your parent form, call its Show method whenever you want to display it, and add an event handler to its FormClosing event to check: 或者,如果要保留状态,请将实例作为类成员添加到父窗体,每当要显示它时调用其Show方法,并向其FormClosing事件添加事件处理程序以检查:

if (e.CloseReason == CloseReason.UserClosing)

and, if so, 如果是这样,

e.Cancel = true;
Hide();

(This last part is to prevent errors if the user closes the form and then tries to display again after it's been disposed.) (最后一部分是为了防止用户关闭表单时出错,然后在处理完毕后再次尝试显示。)

I think more information may be needed, but if your crating a custom user control, the control should have a .Visible property. 我认为可能需要更多信息,但如果您的自定义用户控件,则控件应具有.Visible属性。 The follow is an example of how a button can be located on the form but hidden from a user. 以下是按钮如何位于表单上但对用户隐藏的示例。

button.Visible = true;  // shows the button
button.Show(); // Shows the button
button.Visible = false; // hides the button
button.Hide(); // Hides the button

While the button may still be on the form/control, it will not be interactible by the user. 虽然按钮可能仍在表单/控件上,但用户无法进行交互。 You can still perform programmatic control on the button, but essentially it is not a user control while it is 'hidden'. 您仍然可以对按钮执行编程控制,但实际上它不是用户控件,而是“隐藏”。 If you want there to be a sort of hidden button that the user can click you will need to do other things to obtain this but It doesn't should like that is what you want. 如果你想要一个隐藏按钮,用户可以点击你需要做其他事情来获得这个,但它不应该是你想要的。

This show/hide thought process sounds a lot like pains and confusion leftover from classic VB. 这个显示/隐藏思维过程听起来很像经典VB中的痛苦和混乱。 The old form methods of show and hide, etc., were confusing and often left me as a developer in a position to not know whether an object existed or if was merely invisible. 陈旧和隐藏等的旧形式方法令人困惑,并且经常让我作为开发者处于不知道对象是否存在或者仅仅是不可见的位置。 And checking was only trivial if you used On Error Goto to prevent a null reference. 如果您使用On Error Goto来阻止空引用,则检查只是微不足道的。 So right off I would advise not to think in terms of visibility unless you are doing something with a web page and need to maintain space and state. 所以,我建议不要考虑可见性,除非你正在做一些网页,需要保持空间和状态。

First, create a Windows form and add it to your project, assuming that is the type of project that you are describing. 首先,创建一个Windows窗体并将其添加到您的项目中,假设这是您正在描述的项目类型。 Decorate the form with the proper controls, and where applicable, create properties to allow public access to the control values. 使用适当的控件装饰表单,并在适用的情况下创建属性以允许公共访问控件值。 Also set the DialogResult property of the buttons that either "OK" or "Cancel" the form. 还要设置“OK”或“取消”表单的按钮的DialogResult属性。 Give it an appropriate border style of either Fixed3D or FixedDialog. 给它一个适当的Border3D或FixedDialog边框样式。 Maybe also set the property for where you want the form to appear on startup -- center parent, center screen, Windows default, etc. The event handlers for both "OK" and "Cancel" should invoke this.Close(); 也许还设置了你希望表单在启动时出现的位置 - 中心父,中心屏幕,Windows默认等等。“OK”和“Cancel”的事件处理程序都应该调用this.Close(); to close the window. 关上窗户。

From the calling point in the code, here's some hypothetical code to get you going in the right direction. 从代码中的调用点开始,这里有一些假设的代码可以帮助您朝着正确的方向前进。 Write something like this in the place where you want to invoke your Dialog. 在你想要调用Dialog的地方写下这样的东西。

int intResult = 0;
string strResult = null;

MyDialogForm frm = new MyDialogForm();
frm.Title = "Select an Item";
frm.SomeProperty = 0;
frm.SomeOtherProperty = true;
if (frm.ShowDialog() == DialogResult.OK)
{
intResult = frm.Result;
strResult = frm.StringResult;
}
else if (frm.ShowDialog() == DialogResult.Cancel)
{
// User clicked the cancel button. Nothing to do except maybe display a message.
MessageBox.Show("Canceled Task");
}
...

MyDialogForm frm = new MyDialogForm();
frm.Title = "Select an Item";
frm.SomeProperty = 0;
frm.SomeOtherProperty = true;
if (frm.ShowDialog() == DialogResult.OK)
{
intResult = frm.Result;
strResult = frm.StringResult;
}
else if (frm.ShowDialog() == DialogResult.Cancel)
{
// User clicked the cancel button. Nothing to do except maybe display a message.
MessageBox.Show("Canceled Task");
}
...

// Somewhere further on down, but within scope, simply repeat
// what you just did, but without having to reinstantiate the
// form Window. But if you make it that far within the same
// scope, this method might be too busy and may need to be
// factored down.

So in short: 简而言之:

  • Scrap show/hide -- its not a good practice. 废料展示/隐藏 - 这不是一个好习惯。
  • Save the form data without using an invisible form to save it; 保存表单数据而不使用不可见的表单来保存它; that's the class's job. 这是班级的工作。
  • If the UI requires a lot of flipping back and forth between windows, check your design for other alternatives for solving the original problem. 如果UI需要在窗口之间来回翻转,请检查您的设计是否有其他替代方案来解决原始问题。 Maybe a design pattern such as MVC is for you, depending upon the size and complexity of your application. 也许MVC等设计模式适合您,具体取决于应用程序的大小和复杂程度。

Sound good? 听起来不错?

You can put that control in a Panel. 您可以将该控件放在Panel中。 Set the panel height = 0 visible = false when you dont want to show the control. 当您不想显示控件时,设置面板高度= 0 visible = false。 And do the vice versa when you want to show it. 当你想要展示它时,反之亦然。

Derive from Control , not UserControl , and in the constructor set Visible = false . 派生自Control ,而不是UserControl ,并在构造函数集Visible = false派生。

Also create an event handler in the constructor. 还要在构造函数中创建一个事件处理程序。

VisibleChanged += new EventHandler(SetVisibleFalse);

Create a method named SetVisibleFalse . 创建名为SetVisibleFalse的方法。

private void SetVisibleFalse(object sender, EventArgs e)
{
    if (Visible) Visible = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM