I am working on MDI app which have Child Forms. I have to show Child Window once a certain conditions is met.
I created a separate Class named clsDashbord having method loadDashboard() which is supposed to load frmDashboard already designed. Code is given below:
public void loadDashboard(String userName)
{
_Dashboard = new frmDashboard();
_Main = new frmMDI();
// _Dashboard.MdiParent = _Main;
_Dashboard.Text = userName;
_Dashboard.Show();
}
Form does not show up if I set MDIParent to Main which is instance variable of MDI Form otherwise it gets displayed. How to do it?
It looks more like a scoping problem by looking at line '_Main = new frmMDI();'
follow these steps:
create a class named 'ReferenceTable'
create a static variable named _Main in ReferenceTable
set ReferenceTable._Main = new frmMain(); // in Program.cs
set childform.Parent = ReferenceTable._Main //in all your child form
code before calling Show() or showDialog() methods
Related
I am working on a Winforms application that uses base window (e.g. Form1) and many other windows (e.g. Form2, Form3, ...). All of these windows have their property TopLevel set to false, no border and I am placing them in the Form1 when I need them. I am not skilled enough or do not have the brain capacity to solve this issue:
public void ShowForm(string strNewForm) {
var frmNew = Activator.CreateInstance(Type.GetType(strNewForm)) as Form;
frmNew.TopLevel = false;
frmNew.Size = new Size(rctForm.Width, rctForm.Height);
frmNew.Location = new Point(rctForm.X, rctForm.Y);
frmNew.InitializeForm(strActualSection, strActualSubSection);
frmNew.Parent = this;
frmNew.Show();
}
The strNewForm variable contains the name of Form to show. All the methods and properties in each Form can be used as these all are inherited from base class Form. Problem is with InitializeForm() which I use in each form (Form2, Form3, ...) to do various stuff. I know my problem is in this line:
var frmNew = Activator.CreateInstance(Type.GetType(strNewForm)) as Form;
When strNewForm contains string "Form2" and I change as Form to as Form2 it works just fine. I know I need to change it to proper name each time, but I don't know how. I tried to use this:
Type frmType = Type.GetType(strNewForm);
var frmNew = Activator.CreateInstance(Type.GetType(strNewForm)) as frmType;
But it throws an error CS0118: frmType is a variable but is used as a type. I don't know how to solve this, I tried many solutions and I googled for half a day straight and I am still lost. Any insight would be really helpful.
You cannot use a run-time variable to create strongly-typed code.
You have to provide a compile-time type after the as.
If you know it's a Form then all you can do is this:
var frmNew = Activator.CreateInstance(Type.GetType(strNewForm)) as Form;
If you need specific methods or properties then use a custom Form as your base-type and use that in the as.
Why won't you pass a Form object in your ShowForm method?
public void ShowForm(Form newForm)
{
newForm.TopLevel = false;
newForm.Size = new Size(rctForm.Width, rctForm.Height);
newForm.Location = new Point(rctForm.X, rctForm.Y);
newForm.InitializeForm(strActualSection, strActualSubSection);
newForm.Parent = this;
newForm.ShowDialog();
}
I tried to create a new form inside the Parent. I set FormBorderStyle to none.
When I am adjusting MDIParent to the myForm, it gave me a sick looking error like this:
System.ArgumentException : The given Form is not being recalled as a MdiContainer.
This is my code for creating a new Windows Form.
FrmHome myForm = new FrmHome ();
myForm.TopLevel = false;
pnlContainer.Controls.Add(myForm);
myForm.Show();
The Mdi parent must have it's IsMdiContainer property set to True.
You can set this property at design time in your main form or runtime :-
Form1 f1 = new Form1();
f1.MdiParent = this;
f1.Show();
Form1 is the name of the form that you want to show.
Form.IsMdiContainer Property
Property Value
Boolean
true if the form is a container for MDI child forms; otherwise, false.
The default is false.
I am trying to write a generic method which would allow me to add a form as the contents of a Tab on a tab control, however I am only able to figure out how to add it when specifying the exact form I wish to add.
Suppose I have three forms Form1, Form2, and Form3 where Form1 contains the tab control tabControl1, and Form2 inherits Form, where Form3 inherits MetroForm. Following is the method I am currently using :
private void AddFormAsTab() {
Form3 f = new Form3()
{
TopLevel = false,
ShowInTaskbar = false,
ControlBox = false,
SizeGripStyle = SizeGripStyle.Hide,
Visible = false,
Text = string.Empty,
FormBorderStyle = FormBorderStyle.None,
Dock = DockStyle.Fill,
MinimizeBox = false,
MaximizeBox = false,
ShadowType = MetroFormShadowType.None,
Movable = false
};
TabPage tab = new TabPage();
tab.Controls.Add( f );
tabControl1.TabPages.Add( tab );
f.Visible = true;
}
What I would like to do, is modify this method so that AddFormAsTab() accepts any form type and sets the properties as necessary (to ensure they are set as I am lazy and do not wish to change all those properties on every form I design perpetually when recycling this component)
I have seen this done in other controls such as Telerik, and DevExpress where the control accepts a generic and modifies it to 'fit' the purpose. In my case, I am changing the properties of the form so that it fills the tabpage, has no border, title bar, etc.
I have considered using (typeof(T))obj type code, but this generates pre-compile errors where those properties do not exist in obj which prevent it from being built, even though in theory, this should work.
I have also tried stuff like Form f = new Form1(), however this doesn't work as Form is not the same type as Form1 which happens to inherit Form.
What can I do to make this happen, to allow any form to be set without hardcoding those specific forms classes into the AddFormAsTab() method, but still delegate responsibility to set the required properties to that method ?
Ideally, something like :
private void AddFormAsTab<T>(T obj) { ... }
You can use either of these options:
public void AddFormAsTab<T>() where T : Form, new()
{
var f = new T();
f.TopLevel = false;
//...
}
Usage:
AddFormAsTab<Form1>();
Or
public void AddFormAsTab(Form f)
{
f.TopLevel = false;
//...
}
Usage:
AddFormAsTab(new Form1());
Application.OpenForms["formname"];
Is there any other way to access an open form. My application doesn't see this form although it is opened. I dont know why.
Isn't really necessary a name to get an open form.
You can get the form you want by index:
Form frm = Application.OpenForms[0] //Will get the main form
Form frm = Application.OpenForms[1] //Will get the first child
Forms in the OpenForms collection are ordered in the same way that you create it
Otherwise, an alternative is to save a reference to the form and then accessing it by this reference.
//Where you want to save the reference:
Form theForm;
//Where you create the form:
myClass.theForm = new MyForm();
//Where you want to get that form:
MessageBox.Show(myClass.theForm.Caption);
(myClass is the class that will hold your reference to the form, supposing you are accessing it from different classes)
(Also, see if you are affected by this: Application.OpenForms.Count = 0 always)
I recommend you to firstly debug your code to check what is the Form actual name which you want to load:
foreach (Form form in Application.OpenForms) {
string name = form.Name; //check out this name!!
//print, or anything else will do, you only want to get the name
//note that you should be able to get any form as long as you get its name correct
}
And then, once you know what is wrong with your code, simply do:
Form form = Application.OpenForms[name]; //use the same name as whatever is available according to your debug
To get your form.
To check more on the possible bugs, See Hans Passant's Post
You have to instanciate first a Form. after that you have access to it:
Form1 formname = new Form1();
Application.Run(formname);
// access to form by formname.yourproperty
To access the form using this property your form must have a Name.
Remember its not the instance name nor the the form Text:
Form1 f1 = new Form1(); //not "f1" is the "Name"
f1.Text = "it is title of the form"; //neither "Text" is the "Name"
f1.Name= "its the name"; //it is the "Name"
Sample:
frm_myform form1 = new frm_myform();
frm_myform.Name = "form1";
I have a datagridview called logDataGridView in my AnalyzerForm project.
In order to access to it's DataSource property from the other from, called Form2, in the project, below access field has been defined into the AnalyzerForm.Designer.cs:
Public System.Windows.Forms.DataGridView _DGV
{
get {return this.logDataGridView;}
set {logDataGridView.DataSource = value;}
}
And finally, i try to use a filled DataTable called t from the Form2:
AnalyzerForm AZ = new AnalyzerForm();
AZ._DGV.DataSource = t;
Nothing will be shown into the logDataGridView!!!
Does anybody have any idea about the wrong part?
Actually the wrong part of the progress is just reinstantiation of the parent form, as below:
AnalyzerForm AZ = new AnalyzerForm();
One must use the very parent form reference, is which responsible for launching the child form. It is possible to define a secondary constructor for the child parent and feed a parent form object just inside of it:
ParentForm pForm;
public childForm(ParentForm FRM)
{
pForm = FRM;
// Then component initializing...
}
Finally, the required component of the parent form (is which a datagridview, in my case), is possible:
pForm._DVG.DataSource = t;