How to change a form controls using treeview in C#? - c#

I'm using C#, and I want create a form - which is composed of treeview treeview1, a panel panel1 and a button btn1.
In the begining the panel is empty, then a userControl userControl1 will be shown when the user select a node from the tree, and another userControl userControl2 will replace the first whene the user select another specific node , and go on ... as in setting forms
I found a solution here :
How to create an options form in C# Windows Forms?
but it is written i pseudo-code, i try implementing the code, and this what i get:
//on initializing
treeView1.Nodes.Add(new TreeNode());
treeView1.Nodes.Add(new TreeNode());
treeView1.Nodes.Add(new TreeNode());
treeView1.Nodes[0].Tag = new UserControl1();
treeView1.Nodes[1].Tag = new UserControl2();
treeView1.Nodes[2].Tag = new UserControl3();
treeView1.Nodes[0].Text = "user controle1";
treeView1.Nodes[1].Text = "user controle2";
treeView1.Nodes[2].Text = "user controle3";
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
UserControl currentControl = (UserControl)treeView1.SelectedNode.Tag;
TreeNode currentnode = treeView1.SelectedNode;
if (panel1.Controls != null)
{
panel1.Controls.Clear();
}
if (currentnode.Tag == null)
return;
panel1.Controls.Add((UserControl)currentnode.Tag);
}
and about the usecontrols I add nothing to the code, just controls that i need...
It works, but I am not sure if it is correct, I find other solutions and they add a lot of stuff to both, the user controls and to the main form.
So, what this code need to work properly.

Related

Open Child Form in Parent Form's Panel from Another Child Form

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

Devexpress TabHeader Disappears

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.

How to Hide and show child form in c#

I prepared the form with using "SplitContainer tool".I added Treeview into left side of that SplitContainer. next i added to that treeview two Node such as hide and show and also i prepared a "child form". I need to do, Chile form SplitContainer load to right side when I click on the Node show and hide child form when click on the hide node.i can show chile form but can not hide it.please help me to do this.below i attached code which i used to "Show"
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
UserControll.UscCreateUser UscPerobjForm = new UserControll.UscCreateUser();
string Tree = (string)e.Node.Tag;
if (Tree == "1")
{
UscPerobjForm.TopLevel = false;
splitContainer1.Panel2.Controls.Add(UscPerobjForm);
UscPerobjForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
UscPerobjForm.Dock = DockStyle.Fill;
UscPerobjForm.Show();
//Show part
}
else if (Tree == "2")
{
// Hide part
}
}
I need to Hide part.
Try using http://dockpanelsuite.com/ where you can have the tree view implement in a Form class docked on Left while the Child Form dock the center.

Access form panel from user control?

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

C# How to design panel for later use..(Settings screen)

So i'm making a settings screen at the moment where i'll have a tree on the left then a panel on the right. The panel thats on screen will depend on what tree item is selected..
Just wondering how do I go about designing these panels and saving theme for use later on (runtime).
Do I need to go and draw them out etc. view code then copy into a class or something?
Sorry if my question is a bit vague but i'm not really sure what I want :-O
EDIT Yes, i'm looking to make a settings screen like the one found in Visual Studio. A tree on the left (explorer like) and then a new form layout for each tree node.
You'll want to create UserControls instead of a Panel, it is easy to edit in the designer. Dock the tree view to the left and use code like this to select the active user control:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
}
private UserControl mActivePanel;
void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
UserControl newPanel = null;
switch (e.Node.Index) {
case 0: newPanel = new UserControl1(); break;
case 1: newPanel = new UserControl2(); break;
// etc...
}
if (newPanel != null) {
if (mActivePanel != null) {
mActivePanel.Dispose();
this.Controls.Remove(mActivePanel);
}
newPanel.Dock = DockStyle.Fill;
this.Controls.Add(newPanel);
this.Controls.SetChildIndex(newPanel, 0);
mActivePanel = newPanel;
}
}
}

Categories

Resources