Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
In a MainForm I'm using a RadDock control (kinda tab control),in first tab of this control(startpage) form1 was loaded
C#
MainForm()
{
 DocumentWindow DW = new DocumentWindow();
            DW.Text = "form1";
            DW.CloseAction = DockWindowCloseAction.Hide;
            Form1 frm1 = new Form1();
            frm1 .FormBorderStyle = FormBorderStyle.None;
            frm1 .Dock = DockStyle.Fill;
            frm1 .TopLevel = false;
            DW.Controls.Add(frm1);
            this.radDock1.AddDocument(DW);
            this.radDock1.ActiveWindow = DW;
            frm1 .Show();
}


now In an event of Form1 I want to show form2:
C#
Form1()
{
Form2 frm2 = new Form2(ParametersToForm2);
frm2 .ShowDialog();
}

but this code opened frm2 in a new window. How can I show that in a new tab in current tabcontrol of MainForm?

my solution was sending "ParametersToForm2"to a function of MainForm and then this function call form2.

C#
form1()
{
    MainForm mfrm=new MainForm();//I defiend this to can access to a function of this form but this causes a new MainFormWindow that is not what I want
     mfrm.showForm2InNewTab(ParametersToForm2);
    
}


C#
MainForm()
{
showForm2InNewTab(showForm2InNewTab)
{
      DocumentWindow DW = new DocumentWindow();
            DW.Text = "form2";
            DW.CloseAction = DockWindowCloseAction.Hide;
            Form2 frm2 = new Form2(ParametersToForm2);
            frm2 .FormBorderStyle = FormBorderStyle.None;
            frm2 .Dock = DockStyle.Fill;
            frm2 .TopLevel = false;
            DW.Controls.Add(frm2);
            this.radDock1.AddDocument(DW);
            this.radDock1.ActiveWindow = DW;
            frm2 .Show();
}
}


but this way doesn't show form2.
Posted
Updated 17-Feb-14 21:15pm
v5
Comments
BillWoodruff 18-Feb-14 5:06am    
Hi, Keep in mind that most of us here are almost certainly not using the Telerik Controls, and that Telerik does have tech support, and support forums for users, that have good reputations (at least the few people I know using their WinForms Controls say so).

So, in this question, for example, people like me who don't use Telerik Controls, will not know if Telerik's equivalent of the TabControl in WinForms supports hiding different TabPages: the WinForms TabControl does not support that (easily).

Please do take the advice you got here and switch to UserControls !
mit62 18-Feb-14 5:10am    
Ok,I do that :)

Two things:
1) A new instance of the Main form is not the same as the currently displayed instance of the Main form, in the same way that a new car is not the same as your existing car. If you go for a test drive in a new car, and put your mobile phone in the glove box, would you expect to find it again by opening the glove box of your old car?

Instead, use events: The Form1 raises an event which is handled by it's "parent" Main form, and the Main form then creates and handles the new instance of Form2.

This explains how to set up the event: Transferring information between two forms, Part 2: Child to Parent[^]

2) Why, oh why, are you embedding forms in tab pages? Why not use a UserControl, since that is what they are designed for? It's simpler, cleaner, and you don't get a load of garbage with it...
 
Share this answer
 
Comments
mit62 18-Feb-14 3:36am    
oh Thanks,but I'm beginner and I don't know about user controls ,I'll change my design.thanks again
OriginalGriff 18-Feb-14 3:57am    
They are really easy to use: you create a control, drop what you need on it and hook it all up, and to the world outside it is just a single control, like a TextBox, with the Properties, Events, and method you let it have. I use them a fair amount, just to reduce the "clutter" on a form: three user controls each containing 6 or 7 related items is a lot "cleaner" to handle than a form with 20 odd controls scattered around! :laugh:
mit62 18-Feb-14 4:02am    
you're right
mit62 18-Feb-14 4:34am    
sorry,for changing winform to UC is that a good way to change the form to inherit from UserControl instead of Form, then fix any compile errors?
OriginalGriff 18-Feb-14 4:37am    
No - I'd start by creating a new UC and copy the controls over, then hook them up - that way you don't risk damaging the existing code beyond repair, and you have the original to compare functionality against to be sure that you have implemented the UC correctly. Then when you are happy, you can delete the forms, but nothing stops working in the meantime!
This is all the bad idea. Forget about forms, have only one main form. Everything you had as forms, convert to tab pages, or panels, or user controls added as children to tab pages.

—SA
 
Share this answer
 
Comments
mit62 18-Feb-14 3:37am    
thanks SA, the Speed was very low too I'll change my design thanks
mit62 18-Feb-14 4:00am    
Sorry,I have another question.If I put some controls on a tab page instead of form. then sometimes I must hide or show this tab for example if an event occured,how can i handle this?
using tab.hide() and tab.show() is enough? and something else,when main form will be loaded all embedded controls should be load too?doesn't make it a bad performance?
Sergey Alexandrovich Kryukov 18-Feb-14 11:34am    
I never tried to hide or show tabs, as this is not quite the concept tabs were designed for. You can remove the tab (and tab page) completely and preserve the reference to, say, some panel, to be able to add it again...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900