I'm new to Visual Studio 2010 and I'm planning to create a Timekeeping system. I'm just want to ask how could I create a form that compose 2 forms in it. For example, if I will click a button it will open a new form inside a form. Please help. Thanks
Form formA = new Form();
formA.IsMdiContainer = true;
Form formB = new Form();
formB.MdiParent = formA;
formB.Show();
You have to work with MDI (Multiple Document Interface), have alook at this article that might help.
You could create custom form, remove all borders, and toolbars to make it look as closely to a panel as possible. Then make that new custom form a MdiContainer / MDI-panel and show forms in that panel, something like the code below will do the job
Mdi-Panel definiton:
public class MdiClientPanel : Panel
{
private Form mdiForm;
private MdiClient ctlClient = new MdiClient();
public MdiClientPanel()
{
base.Controls.Add(this.ctlClient);
}
public Form MdiForm
{
get
{
if (this.mdiForm == null)
{
this.mdiForm = new Form();
/// set the hidden ctlClient field which is used to determine if the form is an MDI form
System.Reflection.FieldInfo field = typeof(Form).GetField("ctlClient", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
field.SetValue(this.mdiForm, this.ctlClient);
}
return this.mdiForm;
}
}
}
Usage:
/// mdiChildForm is the form that should be showed in the panel
/// mdiClientPanel is an instance of the MdiClientPanel
myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm;
I think, this is a very easy way:
Form1 form= new Form1 ();
form.TopLevel = false;
this.Controls.Add(form);
form.Show();
Maybe MDI interface will do what you want..
Here's a tutorial to do that.
Related
I've got a Bing Map element in UserControl1.xaml file:
<UserControl x:Class="MyMaps.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
<Grid>
<m:Map CredentialsProvider="Gr8GooglyMoogly" x:Name="myMap" />
</Grid>
</UserControl>
I can access it like so from Form1, on which it sits:
this.userControl11.myMap.Mode = new RoadMode();
...but when I try to access it from another form, none of these attempts work:
userControl11.myMap.Children.Add(pin); // does not exist in the current context
Form1.userControl11.myMap.Children.Add(pin); // inaccessible due to its protection level
UserControl1.myMap.Children.Add(pin); // object reference is required for the static field, ...
How can I get a handle on the UserControl from another form?
UPDATE
Using Reza's comment to change the Modifier property of the map from Private to Public, and utilizing the method shown at the link provided, the following works:
var frmMain = new Form1();
frmMain.userControl11.myMap.Children.Add(pin);
UPDATE 2
Reza's idea worked perfectly. This is how I tested it to verify:
In "Form 2" (mdlDlgFrm_AddNewLocation):
// to access map on main form (Form1)
private Form1 frmMain;
// second constructor so as to access map on main form (to add pushpins)
public mdlDlgFrm_AddNewLocation(Form1 f1)
{
InitializeComponent();
this.frmMain = f1;
// test
AddPushpin("blaJustATest");
}
private void AddPushpin(string fullAddress)
{
Pushpin pin = new Pushpin();
// "brute-forcing" the coordinates for this test
pin.Location = new Location(37.1481402218342, -119.644248783588); // Interesting location: out in the "boondocks" between Oakhurst and Auberry
this.frmMain.userControl11.myMap.Children.Add(pin);
}
...and "Form 2" being invoked from the main form (Form 1):
private void addLocationToolStripMenuItem_Click(object sender, EventArgs e)
{
mdlDlgFrm_AddNewLocation frmAddNewLocation = new mdlDlgFrm_AddNewLocation(this);
frmAddNewLocation.ShowDialog(this);
frmAddNewLocation.Dispose()
}
When you create a new form, you must send the current form as a parameter to the creator of the new form, where you can make changes to that instance using the instance you submitted.
In addition to all the options that I've already explained in the linked post and has been told in other posts, including the one that I mentioned in the comments, you can consider the following options here:
If you use ShowDialog to show Form2, you don't need a reference to Map or to Form1. Just return Pushpin from Form2 and use it.
OR Just pass the dependency, the Map to your second form. (No need to make the usercontrol public or pass the whole form1).
Example 1 - Return Pushpin
If you use ShowDialog to show Form2, you don't need a Map in Form2. Just create the Pushpin and return it back to Form1 and use it there.
In Form2, define a Pin property:
//using Microsoft.Maps.MapControl.WPF;
//...
public Pushpin Pin {get; set;}
In Form2, when you want to create the Pushpin, assign it to Pin property:
//...
this.Pin = new Pushpin(){Location = location};
this.DialogResult = DialogResult.OK;
Then in Form1 when you want to show Form2:
using(var f2 = new Form2())
{
if(f2.ShowDialog() == DialogResult.OK)
{
this.userControl11.myMap.Children.Add(f2.Pin);
}
}
Example 2 - Pass the Map
Just pass the dependency, the Map to your second form. You don't need to make the user control public or you don't need to pass the whole form1:
Change Form2 constructor to accept a Map:
//using Microsoft.Maps.MapControl.WPF;
//...
private Map map;
public Form2(Map map)
{
InitializeComponent();
this.map = map;
}
In Form1, when you want to show Form2:
var map = this.userControl11.myMap;
using(var f2 = new Form2(map))
f2.ShowDialog();
In Form2, when you want to use the map:
//...
var pin = new Pushpin(){Location = locatin};
map.Children.Add(pin);
I have a form page with text boxes and data grid view and other forms that contain a tab control. I want to add the first form tab in the second form. I tried to write the code for the form to appear but it is larger than the tab container and doesn't fit. Only half of the form appears.
This is my code:
private void tcMainPage_SelectedIndexChanged(object sender, EventArgs e)
{
if (tcMainPage.SelectedIndex == 0)
{
GTOWN.PrintingPage BookInfo = new PrintingPage();
BookInfo.TopLevel = false;
BookInfo.FormBorderStyle = FormBorderStyle.None;
BookInfo.Dock = DockStyle.Fill;
tpSearch.Controls.Add(BookInfo);
BookInfo.Show();
}
}
this is the form
and that is what appears
Set your main FORM as a Container.
yourForm.IsMdiContainer = true;
Then add the child form to the tabPage:
private void tcMainPage_SelectedIndexChanged(object sender, EventArgs e)
{
if (tcMainPage.SelectedIndex == 0)
{
PrintingPage newFrm = new PrintingPage
{
MdiParent = this,
// This set the form parent as the tabClicked
Parent = tcMainPage.TabPages[0]
};
newFrm.Show();
}
}
my tab form work good in the same code
thank you all my code was correct but the problem was in tab property i deleted the tab and add another one and the code is working now
thank you
I face this issue and I create this if may help
public void addform(TabPage tp, Form f)
{
f.TopLevel = false;
//no border if needed
f.FormBorderStyle = FormBorderStyle.None;
f.AutoScaleMode = AutoScaleMode.Dpi;
if (!tp.Controls.Contains(f))
{
tp.Controls.Add(f);
f.Dock = DockStyle.Fill;
f.Show();
Refresh();
}
Refresh();
}
Forms are top-most objects and cannot be placed inside of other containers.
You may want to refactor your code so that the items on your Form are on a UserControl instead. At that point you can then add that UserControl to both a Form and a TabControl
public UserControl myControl(){ /* copy your current view code here */}
public Form myForm(){
Controls.Add(new myControl());
}
public Form myTabbedForm(){
var tabControl = new TabControl();
var page1 = new TabPage();
page1.Controls.Add(new myControl());
tabControl.TabPages.Add(page1);
this.Controls.Add(tabControl);
}
How do I create a simple form that has a MenuStrip at the top and a TabControl filling all of the remaining space?
If I go with DockStyle.Top/DockStyke.Fill tabControl fills whole form regardless of MenuStrip:
public MainWindow()
{
initializeComponent();
}
private void initializeComponent()
{
MenuStrip mainMenu = new MenuStrip();
mainMenu.Dock = DockStyle.Top;
TabControl tabs = new TabControl();
tabs.Dock = DockStyle.Fill;
TabPage test = new TabPage("test");
tabs.Controls.Add(test);
Controls.Add(mainMenu);
Controls.Add(tabs);
}
You should change the z-order of mainMenu or tabs. For example you can call:
mainMenu.SendToBack();
//Or
//tabs.BringToFront();
After adding controls to the controls collection.
Another approach through designer, without writing code manually, so your changes will affect design time too
Use Document outline tab and arrange control's hierarchy with your requirements
View -> Other Windows -> Document outline or CTRL+ALT+T
This question already has an answer here:
Add Form to a UserControl - is this possible?
(1 answer)
Closed 8 years ago.
I would like to know if it is possible to show/place a form1 INSIDE form2.
So that form1 is stuck inside form2.
Any help will be appreciated. thanks in advance.
Yes. The word you're looking for is MDI or Multiple Document Interface.
Just set form2's IsMdiContainer property to true
and set form1's MdiParent property to be form2 like so:
form2.IsMdiContainer = true;
form1.MdiParent = form2;
Of course, you cause the visual designer to write the first line for you by setting the property in the properties section:
EDIT
Here's an easy example.
Say you have 2 forms. FormContainer and FormChild.
FormContainer is the application's main form.
All you need to do is make sure that FormContainer's IsMdiContainer property is set to true and then you could add instances of other forms to this one by setting those instances' MdiParent property. Except for the main form, any instance of the Form class or a subclass is by default not visible.
public partial class FormContainer : Form {
public FormContainer() {
InitializeComponent();
this.IsMdiContainer = true;
// if you're not excited about the new form's backcolor
// just change it back to the original one like so
// note: The dark gray color which is shown on the container form
// is not it's actual color but rather a misterious' control's color.
// When you turn a plain ol' form into an MDIContainer
// you're actually adding an MDIClient on top of it
var theMdiClient = this.Controls
.OfType<Control>()
.Where(x => x is MdiClient)
.First();
theMdiClient.BackColor = SystemColors.Control;
}
private void FormContainer_Load(object sender, EventArgs e) {
var child = new FormChild();
child.MdiParent = this;
child.Show();
// if you wish to specify the position, size, Anchor or Dock styles
// of the newly created child form you can, like you would normally do
// for any control
child.Location = new Point(50, 50);
child.Size = new Size(100, 100);
child.Anchor = AnchorStyles.Top | AnchorStyles.Right;
}
}
I have a unique c# source file named source.cs that i compile using CSharpCodeProvider from a builder to get an executable.
I would put an option on the builder whether to display the About form on application startup or not.
How can i create a form with title as About Us then add controls within (Labels, RichTextEdit etc..)
Something like
if (display_about_dialog) {
// code to display the form }
Any help would be highly appreciated
Try something like this:
using (Form form = new Form())
{
form.Text = "About Us";
// form.Controls.Add(...);
form.ShowDialog();
}
Here's the documentation page for the System.Windows.Forms.Form class.
if you have a class MyForm : System.Windows.Forms.Form (that you create using windows form builder)
You can do
MyForm form = new MyForm();
form.Show();
To launch an instance of MyForm.
Though if you want to create a simple confirmation or message dialog, check out the many uses of MessageBox
MessageBox.Show("text");
MessageBox.Show("text", "title", MessageBoxButtons.OKCancel);
Form aForm = new Form();
aForm.Text = #"About Us";
aForm.Controls.Add(new Label() {Text = "Version 5.0"});
aForm.ShowDialog(); // Or just use Show(); if you don't want it to be modal.
Form is a class which you can instantiate like any other, set it's properties, call it's methods.