I've created a menu form with two checkboxes. When I select one of them, I want to disable another form. I tried to write this code:
void CheckBox1CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true){
checkBox1.Checked=true;
checkBox2.Checked=false;
classificheForm cf=new classificheForm();
cf.Enabled=false;
classificheJunioresForm cjf=new classificheJunioresForm();
cjf.Enabled=false;
}
}
but doesn't work, indeed forms aren't disable. How can I fix it?
You are creating a new Form and immediately disabling it:
classificheForm cf=new classificheForm();
cf.Enabled=false;
Instead you should use the reference to the Form you want to disable. For example if in the constructor you create the form:
classificheForm cf;
public SomeClassConstructor()
{
cf=new classificheForm();
}
Then in your event you would just do:
cf.Enabled=false;
Note that you can also hide and show forms like:
cf.Hide();
cf.Show();
Note that needing to disable a Form sounds like an XY problem. If you don't need the form anymore close it or hide it. Also only use Forms when they are needed etc...
If you need to disable a main Form whilst you use a dialog form it is best to call
dialog.ShowDialog();
as this will block users being able to interact with the main Form until they have closed the dialog one.
You can easily hide a Form
classificheForm cf=new classificheForm();
cf.Show();
classificheJunioresForm cjf=new classificheJunioresForm();
cjf.Hide();
Or what do you mean with "disable"?
Related
My apps use a timer that display a form after some seconds.
When I minimize my apps to do other stuff, the timer is still active (I'm ok with that) and the form bring to front over all my windows with focus on it (normal behavior).
I want that the new form open above my main apps but not above all my windows.
In the main form, I call the new form like this :
MRIS.EVENT_BOX form1 = new MRIS.EVENT_BOX();
form1.Owner = this;
form1.ShowDialog();
I manage to remove the focus problem by adding this in the EVENT_BOX :
protected override bool ShowWithoutActivation { get { return true; } }
I also check that TopMost is set to false in the new form.
But the new form is still show above all the others (without focus this time...).
I check some other questions but cannot manage to find something useful.
Some people talk about visible form ?
If you can help me ?
Thanks
You need to call form1.Show(); if you don't want to bring it to front
form1.Show();
//form1.BringToFront() You may need to call this if you want to being it to front
form1.ShowDialog() shows the form as dialog which means it will be shown on top of parent form. You can check more details on this here
I'm trying to create two Windows Forms with buttons in order to linking them to each other, using this code:
private void cmdInformation_Click(object sender, EventArgs e)
{
frmInformation information = new frmInformation();
information.Show();
}
(It's the same way for the second form)
When using this code you will have multiple windows of same form if you keep clicking the buttons. What can I do to stop this?
The best solution will be linking forms together without creating a new one, but I don't know any other way than one above.
Tested Hide() but it just hides the multiple windows and program remain open even if you close last shown window.
Tried Close() but when it close the main form whole program is ended.
Use a dialog.
information.ShowDialog();
You can also read up on dialog results and get responses from your form if that is needed.
Move the creation of the opposing form outside of the click event.
frmInformation information = new frmInformation();
private void cmdInformation_Click(object sender, EventArgs e)
{
information.Show();
}
This will re-display the same form over and over maintaining any data even when the user closes it.
An option would be to create a FormFactory that keeps track of the instances, this way if the form doesn't exist you can create a new instance and show it. If the form exists you can just show the current instance.
I want to access variables of a form from another form. On clicking a button inside my Main form, I want to set my Main form as Parent, then bring up another form (child form) wherein I will access variables of the Main form. My click handler is as follow:
private void btnSystem_Click(object sender, EventArgs e)
{
Form_EnterPassword EP = new Form_EnterPassword();
EP.Parent = this; //error: Top-level control cannot be added to a control
EP.ShowDialog();
}
It compiles fine without any error. However, when I run the Main form and click on the System button, it throws me an exception. I do something similar in another code (not mine) with the same button click, and encounter no error (just with setting Main form as Parent).
What am I doing wrong? Is there something in my Main code that cause this?
Best way would be to use EP.ShowDialog(this) and later use Owner property.
You need the EP.TopLevel property to be set to false. It will let you to set a parent to it.
Further reading.
In case you only want to access variables and controls of another form, then maybe you can reach it in other ways, not trough a Parent relationship.
OK,
apparently the way to do it is to call
Form_Child.ShowDialog(this)
and then I can call
FromParent_aVariable = ((Form_Parent)this.Owner).aVariable;
or if I define aVariable in the namespace Properties then
FromParent_aVariable = NameSpace.Properties.Settings.Default.aVariable;
there are two ways.
Form_EnterPassword EP = new Form_EnterPassword();
EP.MdiParent = this;
EP.Show();
try this way, it helps for me. you need to set principalform as isMdicontainer = true at the form properties
I had a similar situation recently.
I was attempting something similar but by controlling the Child Forms from a different class.
Note(s):
You're trying to set the Child Form(s) "TopMost" to something that does not allow it.
In this case the "MdiContainer".
To accomplish this:
• Disable MainForm "isMdiContainer" property (its use is kind of obsolete anyway).
• Set the Form(s) TopMost properties to true.
• You should now be able to accomplish your feature.
**Code Example:**
/* On your Main Form Class */
private void btnSystem_Click(object sender, EventArgs e)
{
// Instantiate the Form_EnterPassword by passing the MainForm
Form_EnterPassword EP = new Form_EnterPassword(this);
EP.Show(); // No longer as modal Form to display in front.
}
/* Under your EnterPassword Form Class */
// Do not create a new Instance of MyMainForm.
// You want to use the same thread as your MainForm
private MyMainForm mainForm;
/* Constructor */
public Form_EnterPassword(MyMainForm form)
{
mainForm = form;
this.Owner = mainForm; // "this" refers to the: EnterPassword Form.
}
Remarks:
The only additional thing that you (may) have to do, (to achieve perfection) is to check the MainForm > WindowState; and create a code block to minimize or bring the Forms to their specific state.
i.e:
if (WindowState == FormWindowState.Minimized)
{ /* Code to Minimize all the Child Forms. */ }
else { /* Code to bring all Forms to their "Normal" State */ }
Writing this way, made the dialog display on the center of the parent form.
Form_Child.StartPosition = FormStartPosition.CenterParent;
Form_Child.ShowDialog(this);
I need to create a user control in C#.Net, which can be added to the application without being visible - just like the 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.
Any Idea?
Thanks and regards,
Daniel
Since all these "invisible" controls derive from Component class, you should start by reading the MSDN article on it: http://msdn.microsoft.com/en-us/library/system.componentmodel.component.aspx.
simply set Visible to false or isn't this what you're asking for ?
A UserControl is by definition not a Form; I think what you really want is a Component. That said, couldn't you really just create a new Form class that has the functionality you want? Whenever you want to display it, create a new instance and call 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:
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. 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. 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. 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. 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. Give it an appropriate border style of either Fixed3D or 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(); 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.
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.
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. Maybe a design pattern such as MVC is for you, depending upon the size and complexity of your application.
Sound good?
You can put that control in a Panel. Set the panel height = 0 visible = false when you dont want to show the control.
And do the vice versa when you want to show it.
Derive 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;
}
Backdrop: There are two forms, the main application form and a form to edit various settings for the operations of main application.
What would be the proper way to show the frmSettings form and how to dispose it once clicked OK or Cancel in the actual frmSettings form?
Perhaps a Dialog would be better suited to your Settings "form." There are subtle differences between a dialog and a form that would make the dialog easier to handle. A return code indicating the button that was clicked makes dialogs useful.
Supposing that you used a dialog - a using statement could be used (off the top of my head):
using (DialogSettings dlgSettings = new DialogSettings)
{
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
}
}
If you insist on using a form then you would have to
Instance the form
show the form
record whether ok or cancel was clicked to a form level variable (from within the forms ok/cancel button click code)
hide the form
save the recorded value from the form
dispose of the form
make use of the saved ok/cancel value
fyi, using "frm" is not a recommended part of the C# coding guidelines. Microsoft prefers you don't use hungarian notation in .NET at all.
using (frmSettings s = new frmSettings() )
{
if( s.ShowDialog() == DialogResult.OK )
{
//do work
}
}
In the main application declare an instance and show it.
using(frmSettings settingsInstance = new frmSettings())
{
settingsInstance.Show(); //or ShowDialog()
}
Then just close the form when done...