The first line is able to clear the panel but doesnt seem to show the usercontrol in the panel in the parent form.
panel1.Parent.Controls.Clear();
if (panel1.Parent == null)
return;
messagessent uc = new messagessent();
uc.Dock = DockStyle.Fill;
panel1.Parent.Controls.Add(uc);
Why you use panel1.Parent? In this way, you remove all controls not from the panel itself, but from the parent control on which this panel is located.
This action removes the panel itself. And, accordingly, the panel does not have a parent now. The Parent property becomes null.
I suppose you need to write like this:
panel1.Controls.Clear();
messagessent uc = new messagessent();
uc.Dock = DockStyle.Fill;
panel1.Controls.Add(uc);
Related
I have a panel in WinForms which loads panels at run time during a method call.
My code looks like:
//Adding a child panel
Panel p = new Panel();
//Adding controls to panel
Label lbl5 = new Label();
lbl5.Location = new Point(105, 3);
lbl5.Text = note.noteName;
Label lbl6 = new Label();
lbl6.Location = new Point(105, 43);
lbl6.Text = note.noteName;
p.Controls.Add(lbl5);
p.Controls.Add(lbl6);
//Adding child panel to main panel
Panel1.Controls.Add(p);
In this way whenever the method is called a new child panel will be added to main panel.
Can I Click a particular panel which is displayed in main panel ?
I want to get the value of the controls present in selected panel and show it somewhere.
I would appreciate any help on this.
Name your panel....
var pPanel = new Panel();
pPanel.Name = "pPanel";
// or write it this way....using object initializer
var pPanel = new Panel
{
Name = "pPanel"
};
Then loop through the controls in you master panel for the control you are looking for...
foreach(Control ctrl in mainPanel)
{
if (ctrl.Name.Contains("pPanel")) .... then do something etc...;
}
You can also search for other controls in your panels the same way ...
Subscribe to a event like so:
Panel p = new Panel();
p.Click += panel_click;
And then create the event:
private void panel_click(object sender, EventArgs e)
{
Panel childPanel = sender as Panel;
foreach(Control c in childPanel.Controls)
{
//Do something with you values...
}
}
I create a user control dynamically in my code
UserControl myobject = new UserControl();
myObject contains a button etc.
when I add this control to my picturebox
picturebox.Controls.Add(myobject);
my picturebox's backgorund image is dissappeared.
Why?
note: the button can be seen however. I want picturebox to be seen also
Set transparent basckground color of your user control. This will make picturebox visible:
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
picturebox.Controls.Add(myobject);
BTW I believe you have different name of user control. And yes, as #samjudson stated, PictureBox should not be used this way: try to use Panel with background image instead (approach will stay same - use transparent color to see parent control):
panel.BackgroundImage = // your image
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
panel.Controls.Add(myobject);
Try this:
UserControl myobject = new UserControl();
Button but = new Button();
but.BackColor = Color.Gray
pic.BackColor = Color.Green;
myobject.Controls.Add(but);
pic.Visible = true;
pic.Controls.Add(myobject);
The PictureBox control is not meant to be used as a container. Try adding a parent Panel or similar and the adding the PictureBox and your custom control to the Panel control.
I have written code that will resize a Control and all of its controls, but there's a problem with the PropertyGrid. The user interface is a GroupBox that contains the TabControl tabContAll. In tabContAll is a TabPage that contains a PropertyGrid.
private void ResizeUI ()
{
ui.Location = new Point (this.ClientRectangle.Left, this.ClientRectangle.Top + menubar.Height);
ui.Size = new Size (this.ClientRectangle.Width, this.ClientRectangle.Height - menubar.Height);
ResizeControl (tabContAll, ui);
}
private void ResizeControl (Control control, Control parent)
{
control.Location = new Point (parent.ClientRectangle.Left, parent.ClientRectangle.Top);
control.Size = new Size (parent.ClientRectangle.Width, parent.ClientRectangle.Height);
foreach (Control child in control.Controls) {
ResizeControl (child, control);
}
}
This function is called when the form loads, and this is what it looks like compared to if I commented out the resizing in the loop so the PropertyGrid doesn't get resized:
Furthermore when it is resized, the description doesn't work. It just shows the name of the property.
I strongly recommend you to not write code for controls resizing unless you need a very very custom behaviour.
Set Control.Dock or Control.Anchor properties instead, and leave the rest to them.
For example, your case can be solved easily by setting the Dock property to DockStyle.Fill for both your TabControl and PropertyGrid (and obviously removing the custom resizing methods).
Here's a complete MSDN Walkthrough for WinForms custom controls design:
http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx
In my project, I used a Windows Form and user controls. There is a panel on the form. And I put user controls on it.
When I click a button on the user control, I want to load another user control on the Windows Form panel. I set the panel's modifiers public.
The code I tried is below, this are the used variables:
myusercontrolpage1: this is my first user control
myusercontrolpage1: this is my second user control
FrmMain: this is my main form
pnlOrta: this is my panel I load user control in it
This is the code, which is not working:
Userclasses.myusercontrolpage1 page1 = new Userclasses.myusercontrolpage1();
Userclasses.myusercontrolpage2 page2 = new Userclasses.myusercontrolpage2();
FrmMain pnl = new FrmMain();
pnl.pnlOrta.Controls.Clear();
pnl.pnlOrta.Controls.Add(page2);
pnl.pnlOrta.Dock = DockStyle.Fill;
pnl.pnlOrta.BringToFront();
When I click a button on the user control, I want to load another user control on a Windows Form panel.
How can I access form's panel from user control and load another user control?
EDIT:
I replaced this:
FrmMain pnl = new FrmMain();
to
FrmMain f = (FrmMain)this.ParentForm;
This worked.
I'm not sure if I understand what you're trying to do, but answering this part of your question:
How can i access form's panel from
usercontrol...
If you know your UserControl is going to be load on Panel, then you can use .Parent property. You can do something like this in your UserControl (suppose it has a button):
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Panel pnl = this.Parent as Panel;
if (pnl != null)
{
pnl.BackColor = Color.Red;
}
}
}
I have a main form in a panel on the left thats clickable, depending on what you click a new type of form opens. on the righti have another panel where i want to dock the forms that have been opened from clicking on the left.
How can i get the forms to add in a list under one another in the panel on the right? the issue with the code below is that it adds the first element fine. However when i add the second element they both dissapear behind the panel :/
private void addToPanel2(Form o)
{
if (o is Form)
{
if (panel2.Controls.Count == 0)
{
o.MdiParent = this;
panel2.Controls.Add(o);
o.Dock = DockStyle.Top;
o.Show();
}
else
{
//then we know that this is an addable data item
foreach (Form obj in panel2.Controls)
{
if(obj.GetType().Name.Equals(o.GetType().Name))
{
//we dont want to add it as the data type is already open
MessageBox.Show("This data item must already be open. Please Check.");
}
else
{
// add it as its not in there
Form f = (Form)obj;
f.MdiParent = this;
f.Dock = DockStyle.Top;
f.Show();
}
}
}
}
thanks
This is not possible, an MDI child form cannot be a child control of a panel. Adding a non-MDI form to a panel is an iffy proposition as well but is supported. Call its SetTopLevel() method, passing false, set its Visible property to true. You also have to set its FormBorderStyle property to None, it no longer behaves properly as a top-level window.
This just turns it into a UserControl. You are better off actually making it a UserControl, that uses a lot less resources and is much better documented.