Disable buttons on MDI Parent Window when the child window is Shown - c#

I've created a Mdi window with a panel. On this panel i've placed some buttons which open different child forms. The problem i'm having is that I only want a user to be able to open one child form/window at a time. I have been disabling the buttons on the buttons click event to open the child window and enabling them again on the child form close event. But this is rather tedious as there are many buttons. Is there a easier way to do this???
Thanks

The first thing I'd do, would be to put the enable/disable functionality into a single method. The method would take a bool argument for enable/disable.
void HandleButtons(bool enable)
{
toolBarbutton1.Enabled = enable;
toolBarbutton2.Enabled = enable;
toolBarbutton3.Enabled = enable;
toolBarbutton4.Enabled = enable;
}

When I'm doing something similar, I will throw all of the buttons into a panel and just disable the entire panel.

Related

How to focus a control in a form which is added on a panel of its parent form by tabbing index

I'm working on winforms in c#. I have a form which loads other forms in its panel. Now my child forms have many textboxes.
I want to set focus on one of those textboxes by loading my children forms by setting tab index to zero.
But it isn't happening when i load my children forms. I have taken care of the tab stop properties & i also went through the tabbing order of the forms. But the problem is still there.
When i load children forms from startup it focuses zero indexed control. I guess my problem is that i am loading these forms in a panel of a parent form. Any solution to this problem?
The Windows Forms controls in the following list are not selectable. Controls derived from these controls are also not selectable.
Panel
GroupBox
PictureBox
ProgressBar
Splitter
Label
LinkLabel (when there is no link present in the control)
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
You can set focus to a control in a form which is added on a panel of its parent form by creating an instance of child form in the parent form and then setting focus to child form controls.
Make an instance of child form:
ChildForm formInstanceName = new ChildForm();
And then you can set the focus property for particular control as:
formInstanceName .controlname.Focus();
And before doing this make sure you add the child form to the respective panel.
Here is the snippet of the code I used:
Form1 childform1 = new Form1();//creating an instance of child form
private void btn_Click(object sender, EventArgs e)
{
Panel2.Controls.Clear();//To clear existing controls on panel
Panel2.Controls.Add(childform1); // To add child form controls on the panel.
childform1 .textbox1.Focus(); //To set focus to control of child form
}
I hope this helps you..

Why I got extra close button on mdi child window?

I got a strange problem. My mdi child form has 2 close buttons and 2 maximized buttons.
A screenshot of the problem:
I create the mdi child like this:
summaryForm.MdiParent = ContainerForm;
summaryForm.WindowState = FormWindowState.Maximized;
summaryForm.Show();
If I get rid of "summaryForm.WindowState = FormWindowState.Maximized;", the window style is correct. But I hope to make the mdi child form maximized when created.
It's a bug in Winforms. This will happen when the child is created by the parent's constructor. Move it to the Load event.
try this:
childform.ControlBox = false;

C# Control Parent

I have a winform project with a tabcontrol and several tabpages.
Within each tabpage there is some unique panel controls. However, when the user interacts with the form in each tab page, I want a generic picturebox to show. So I don't want to create a new picturebox for each tabpage, rather show the same one. Which control should I therefore have as a parent to the picturebox, and how can I change the parent once I drag the control onto the form?
Thanks.
The form has to be the parent. Which is not so easy to get done in the designer, the tab page will suck it in every opportunity it gets. One thing you can do is put it to the left of the tab control and move it in place by changing its Location property in the form's constructor, after the InitializeComponent() call
public Form1() {
InitializeComponent();
pictureBox1.Left = tabControl1.Left + 15;
}

c# Add HelpButton to a MDI child form at run time

i'd like the following code to work (where this. is a MDI child form)
this.HelpButton = true;
this.HelpButtonClicked += HandleHelpButtonClicked;
this.Refresh();
this code is being called after the mdi child form is displayed.
I wonder if i need to find some way of redrawing the title bar?
David
From the MSDN docs:
The value of the HelpButton property is ignored if the Maximize or Minimize buttons are shown.

How to do two things with one click in Windows Form

On my main form, there is another (floatable) window. This floatable window works sort of like a popupwindow in that it will close when the user clicks somewhere else outside of this window. This is handled by the Deactivate event. But what I want to do is, if the user clicks on a different control (say a button), I want to both close this float window and then activate that button with just one click. Currently, the user has to click twice (one to deactivate the window and once more to activate the desired button). Is there a way to do this with just one click?
foreach(Control c in parentForm.Controls)
{
c.Click += delegate(object sender, EventArgs e)
{
if(floatyWindow != null && floatyWindow.IsFloating)
{
floatyWindow.Close();
}
};
}
And then add your handlers as normal. This additional handler can close the floaty window.
Make sure you floaty window isn't a dialog too as this will not allow your parent form's controls to be clicked.
I had a slightly hacky solution. In your Deactivate event, fire another custom event to your main form. Then when you main form is handling the custom event, enumerate through your control(this.Controls) and locate the control under the mouse by checking all their bound then call Focus(). You might need to sort by the one with the smallest surface area, or you can have a separate list of "focus-able" control like button just for this purpose.
Another way might be to switch focus to your main form immediately after OnMouseLeave of the floatable window, or OnMouseHover of your main window, but keep the floatable windows on top, just no focus. Handle the global mouse down of your main form, and close the floatable window by then.
These are just theories, not tested.
I had an issue like this once too, when a customer wanted "floaty" windows all over there application. I used used an approach similar to the one described in this article:
http://www.vbaccelerator.com/home/NET/Code/Controls/Popup_Windows/Popup_Windows/article.asp
Code sample available here:
http://www.vbaccelerator.com/home/NET/Code/Controls/Popup_Windows/Popup_Windows/Popup_Form_Demonstration.asp
By extending this a bit we created "floaty" windows similar to the ones VS uses when you get a runtime error while debugging code.
At the very least reading the code may give you some insight, however, quarrelsome's response may be the more simple solution.

Categories