C # - create invisible user control

I need to create a custom control in C # .Net that can be added to my application without visibility - just like FolderBrowserDialog. This is a new window that I will use often, so I think this is the right way to go. The window will open by running showDialog-Method as known from another dialog.

Any idea? Thanks and greetings,

Daniel

+2


a source to share


7 replies


Since all these "invisible" controls come from the Component class, you should start by reading the MSDN article on it: http://msdn.microsoft.com/en-us/library/system.componentmodel.component.aspx .



+5


a source


just install Visible

in false

or is this not what you are asking for?



+3


a source


A UserControl

by definition is not a Form

; I think what you really want Component

. However, could you just create a new class Form

that has the required functionality? Whenever you want to display it, create a new instance and call ShowDialog

. Or, if you want to keep state, add an instance as a class member to your parent form, call its method Show

whenever you want to display it, and add an event handler to its event FormClosing

to check:

if (e.CloseReason == CloseReason.UserClosing)

      

and if yes,

e.Cancel = true;
Hide();

      

(This last part is intended to prevent errors if the user closes the form and then tries to render again after deleting it.)

+3


a source


I think more information might be needed, but if you are creating a custom control, the control must have a .Visible property. Below is an example of how a button can be positioned on a form, but hidden from the user.

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

      

As long as the button is still on the form / control, the user will not interact with it. You can still perform programmatic control on a button, but it is not really user control as long as it is "hidden". If you want to have a hidden button that the user can click on, you will need to do something else to get that, but you don't need this to be what you want.

0


a source


This showing / hiding the thinking process sounds like the pain and confusion left over from classic VB. Old forms of show and hide methods, etc. Was confusing and often left me as a developer who might not know if the object existed or was simply invisible. And the check was trivial if you used On Error Goto to prevent a null reference. So my advice would be not to think in terms of visibility unless you are doing something with the web page and have to maintain space and state.

First, create a Windows Form and add it to your project, assuming this is the type of project you are describing. Decorate the form with the correct controls and, where applicable, create properties to allow public access to the control values. Also set the DialogResult property to a button that is either OK or Cancel the form. Give it the appropriate border style, either Fixed3D or FixedDialog. Maybe also set the property to where you want the form to appear in the original center, center screen, Windows default, etc. Event handlers for "OK" and "Cancel" should refer to this.Close (); to close the window.

From a calling point in the code, here is some hypothetical code to help you move in the right direction. Write something like this in the place where you want to bring up your dialogue.

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");
}

...

// 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.

Shortly speaking:

  • Scrap show / hide is not a good practice.
  • Save form data without using an invisible form to save it; what a great job.
  • If the UI requires a lot of tilting back and between windows, check your designs for other alternatives to address the original problem. Maybe a design pattern like MVC is for you, depending on the size and complexity of your application.

Sounds good?

0


a source


You can set this control on the panel. Set panel height = 0 visible = false when you don't want to show the control. And do the opposite when you want to show it.

0


a source


Try to get from Control

, not UserControl

, and in the constructor set Visible = false

.

Also create an event handler in the constructor.

VisibleChanged += new EventHandler(SetVisibleFalse);

      

Create a method named SetVisibleFalse

.

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

      

0


a source







All Articles