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;
}
}
}
Related
i am new in c# windows application learning
i have 3 form.
main form, form menu, child form
in main form when i click on button1, form menu is show in 1st panel.
this code in main form.....this work properly for me... if anyone have better then this then suggest me.
private void button1_Click(object sender, EventArgs e)
{
bool IsOpen = false;
foreach (Form frmchild in Application.OpenForms)
{
if (frmchild.Text == "Child Form")
{
IsOpen = true;
frmchild.Close();
break;
}
}
if (IsOpen == false)
{
child form cf = new child form();
cf.Owner = this; //main form is owenr
cf.Show();
cf.Location
= new Point(button1.Left + 37, button1.Height + button1.Top + 4);
}
}
main form have 2 panel, 1st is small and dock in top. 2nd is main panel (dock = fill) where i want to dock all child form.
in form menu i have button and when i click on i want to open child form in main form 2nd panel.
please help me to open child form in main form's panel when i click button in another child form in c# language.
please help me.
i think you have to use USER CONTROL instead of a form. Add a usercontrol at your prject and here is the code :
for (int i = panel2.Controls.Count; i > 0; i--)
{
panel2.Controls[i-1].Dispose();
}
//add user control you want
UserControl1 uc = new UserControl1();
panel2.Controls.Add(uc);
I have a c# form with 5 tabs which includes an user control. I have a button in this control, and I want that when the user clicks on the button, another user control will be opened and change the tab.
my form with 5 tabs
public Form1()
{
InitializeComponent();
PanelSlider.Controls.Add(new Accueil());
PanelSlider.Controls.Add(new Outils());
PanelSlider.Controls.Add(new Utilitaires());
PanelSlider.Controls.Add(new Extras());
PanelSlider.Controls.Add(new Options());
Accueil_menuStrip.Renderer = new MyMenuRenderer_Menu();
Load += new EventHandler(Form1_Load);
Shown += Form1_Shown;
}
the tab Accueil call user control Accueil
my button click user control Acceuil in my form
public void UtilsBtn_Click(object sender, EventArgs e)
{
PanelSlider.Controls.Find("Utilitaires", false)[0].BringToFront();
}
the tab Utilitaires call user control Utilitaires
public Utilitaires()
{
InitializeComponent();
PanelSlider_Utils.Controls.Add(new Project1());
PanelSlider_Utils.Controls.Add(new Project2());
PanelSlider_Utils.Controls.Add(new Project3());
}
In the user control Utilitaires i can call 3 others user control
How i can directly call project 3 from my user control Accueil and select the tab utilitaires ?
I am able to select the utilitaires tab directly by the button in my acceuil user control but NO the project3 usercontrol
public void btn_UserFrame_Click(object sender, EventArgs e)
{
Form1 essai = (Form1)this.FindForm();
essai.UtilsBtn.PerformClick();
how to call project3 in my utilitaires usercontrol...?
}
Always show project1 in my utilitaires usercontrol no project3... I do not know how to do...
RESUME:
Form with 5 tabs which includes an usercontrol PanelSlider
Button UserFrame in the usercontrol PanelSlider
When i click on the button UserFrame i want to change the tab form and show the usercontrol Utilitaires with the PanelSlider_Utils Project3
Thanks
Since you're adding Project3 with no name:
PanelSlider_Utils.Controls.Add(new Project3());
You'd have to iterate over all the controls and look for a match based on TYPE. Another option would be to set the Name property and then use Controls.Find() like you're already doing.
Why don't you store those references, though?
private Project1 p1 = new Project1();
private Project2 p2 = new Project2();
private Project3 p3 = new Project3();
public Utilitaires()
{
InitializeComponent();
p1.Name = "p1";
p2.Name = "p2";
p3.Name = "p3";
PanelSlider_Utils.Controls.Add(p1);
PanelSlider_Utils.Controls.Add(p2);
PanelSlider_Utils.Controls.Add(p3);
}
Now you can use "p1", "p2", and "p3" directly to manipulate them somehow...
I have created ribbon form (XtraMain)and I set IsMdiContainer Property to true,i also add documentManager controle i set MdiParent to XtraMain I have add this code to open child forms
public void ViewChildForm(XtraForm _form)
{
if (!IsFormActived(_form))
{
_form.MdiParent = this;
_form.Show();
}
}
private bool IsFormActived(XtraForm form)
{
bool IsOpenend = false;
if (MdiChildren.Count() > 0)
{
foreach (var item in MdiChildren)
{
if (form.Name == item.Name)
{
tabbedView1.ActivateDocument(item);
IsOpenend = true;
}
}
}
return IsOpenend;
}
and i use this code in click of button to open the child form
private void bbtnEmployee_ItemClick(object sender, ItemClickEventArgs e)
{
FrmEmployee frme = new FrmEmployee();
frme.Name = "FrmEmployee";
ViewChildForm(frme);
}
my problem start when the form contain a LayoutControl for example i have this code that open on click of button
private void btnBonLivraison_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
LayoutControl lc = new LayoutControl();
lc.Dock = DockStyle.Top;
LookUpEdit OrderNumber = new LookUpEdit();
OrderNumber.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
OrderNumber.Properties.DataSource = shippProdu.GetOrderNumber();
OrderNumber.Properties.DisplayMember = "N° Bon de livraison";
OrderNumber.Properties.ValueMember = "N° Bon de livraison";
lc.AddItem(Resources.selectOrderNumber, OrderNumber).TextVisible = true;
lc.Height = 70;
this.Controls.Add(lc);
this.Dock = DockStyle.Top;
lc.BestFit();
the second I click on a button the tabHeader disappears,what cause this problem?and how can I solve it.before I use documentManager I used XtraTabControl and if i click a button to open LayoutControl and after that try to open another form the focus remaining in the first form even when the form two is already opened and if I want to go to form two I must first click on a tab of the first form and then click on tab of the second form , thanks in advance .
this is my main form
and this is when the eader disappears
If DocumentManager resides within the same form to which you add LayoutControl, it is the expected behavior. DocumentManager places a special documents' host onto the main form and set its Dock property to Fill. That is why it is incorrect to place LayoutControl onto the same form and dock it to form edges.
If you need to show tabbed documents and LayoutControl on the same form simultaneously, do not use the MDI mode. Consider the use of a separate UserControl. Place your DocumentManager there. Then, put this UserControl onto your form. Note that in this case UserControl's Dock property should be set to Top or Bottom since LayoutControl should fill all available area or vice versa.
I have a main application form, I've set the IsMdiContainer property to TRUE
I have a panel in the main application form on the top but when I open the ChildForm it opens behind the panel. How do I set location relative to the bottom of the panel + maximize the window?
As Hans said in comments, Set the Dock property of Panel to Top for example. And add your Mdi Childs this way:
Example:
private void MdiTest_Load(object sender, EventArgs e)
{
//Codes for test only
var f = new Form();
f.Controls.Add(new TextBox());
f.MdiParent = this;
f.Show();
}
Note:
You can access to MdiClient control this way:
var mdiClient = this.Controls.OfType<System.Windows.Forms.MdiClient>().First();
Then you can do anything with it if you need. It is a Control like other controls.
Normal State:
Maximized State:
i have make user control and inside that user control takes two buttons name dock and close respectively .
now i want to dock my user control to left when i clicks button dock and close my user control when i clicks button close..
now it works fine.....
but when i add my usercontrol to the toolbox by taking choose items....
then drag and drop my user control to form...
now i have chk on form move event if user control is dock or not...
(i am trying to use by making object of user control but doesnt helps.....)
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Container_User_Control.Container1 obj = new Container_User_Control.Container1();
if (obj.Dock != DockStyle.Left)
{
obj.visible=false;
}
else
{
obj.visible=true;
}
}
Thanks in advanced....
I have no idea why you are using a Mouse_Move event but if I understood your question right then:
When you drag your UserControl from the toolbox to the form, an instance of the usercontrol is created in the form designer code. Something like Container_User_Control1, so instead of using:
Container_User_Control.Container1 obj = new Container_User_Control.Container1();
if (obj.Dock != DockStyle.Left)
{
MessageBox.Show("none");
}
else
{
MessageBox.Show("left");
}
use:
if (Container_User_Control1.Dock != DockStyle.Left)
{
MessageBox.Show("none");
}
else
{
MessageBox.Show("left");
}