C# Control Parent - c#

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

Related

Devexpress unload usercontrol after save form

i have a main form which is containing a menu.
when i click a menu button there is an usercontrol is loading panel control in main form:
splitContainerControl1.Panel2.Controls.Add(new Moduller.userControlStokListesi() {
Dock = DockStyle.Fill
});
And there is a form on UserControl. when a user fill that form and hit the save button i want to remove that user control form from panel control.
How can i do that?
So if I understand your question correct, you want to remove the instance of Moduller.userControlStokListesi from the Panel2 ?
There are several ways to achieve this. You can remove all controls from a panel this way:
splitContainerControl1.Panel2.Controls.Clear();
You can also remove specific items:
splitContainerControl1.Panel2.Controls.RemoveByKey("the key of your control");
Or if you want the user control removes itself from the panel, you can call this snippet within the user control instance:
SplitContainerControl splitPanel = (SplitContainerControl) this.Parent;
splitPanel.Panel2.Controls.Remove(this);

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

How to close the current form in a SplitContainer?

I have a user control in my SplitContainer's right panel. A form opens below the user control. Now if i click a button in the user control, that current form should close and a new form should open. How to do this?
You can implement below logic in your app easily:
splitContainer.Panel2.Controls.Remove(myPanel);
splitContainer.Panel2.Controls.Add(myOtherPanel);
which will remove the existing panel from the container and place the other panel. You can extend the same logic to place forms, or separate controls on the container easily.

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

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.

Removing the default MDI menu of a MDI Container form when a MDI Child is maximized

I am working on a .NET C# application that has a main Form which is MDI container. When the user maximizes a MDI child, Windows draws a control strip right under the title bar of the container Form which has the Icon of the child and the system buttons on the right. Basically, what I need is hide this strip and use a custom control to provide the same functionality.
Is there any way to prevent Windows from drawing this MDI strip?
Actually, I found an easy and interesting way to remove this thing from my form by assigning the MainMenuStrip property of the Form with a dummy MenuStrip control (without putting it in the Controls collection of the Form):
private void OnForm_Load(object sender, EventArgs e)
{
this.MainMenuStrip = new MenuStrip();
}
This prevents the default MDI caption from being painted since the Form delegates its functionality to its default menu strip if any. Since the MenuStrip control is not in the Controls collection of the Form, it is also not visible and thus it just serves as a dummy menu used for hiding the nasty MDI menu when a child is maximized.
This conversation from years ago suggests that there's no way to do it, and he ended up with User Controls on the main form, instead of actually using an MDI interface:
http://answers.google.com/answers/threadview/id/135136.html
Every other thread I can find online is either abandoned with no answers or a dead-end. I can't believe this functionality if so cumbersome and not something natively available.
There is an easier way than adding the code to the Load event for every form. Just place this code in the MdiParent form and substitute MenuStrip for the actual name you're using for your menustrip control.
Private Sub MenuStrip_ItemAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemEventArgs) Handles MenuStrip.ItemAdded
Dim s As New String(e.Item.GetType().ToString())
If s = "System.Windows.Forms.MdiControlStrip+SystemMenuItem" Then
e.Item.Visible = False
End If
End Sub

Categories