I have a Windows application.
The windows application has our XML Library on it.
public NetspotXMLLibV1 XMLLib;
public Form1()
{
InitializeComponent();
XMLLib = new XmlLibrary.NetspotXMLLibV1();
}
It also has a custom control.
That custom control has a button on it that has
SelectWidgitWindow widgit = new SelectWidgitWindow();
widgit.ShowDialog();
when clicking a button, it opens up the new window. I do some stuff on this new window. When I click a button on the new window I want to save stuff on the Form1 Window
How do I access this on my new Window (SelectWidgitWindow ) ?
Ie
form1.XMLLib.Add(ItemForProcessing);
or
Windows(1).XMLLib.Add(ItemForProcessing);
Please help
Use the ShowDialog overload to which you can pass an owner for the new form:
Form2 f = new Form2();
f.ShowDialog(this.Parent);
and in Form2:
((Form1)Owner).MyProperty = 11;
Couple of ways:
1) Expose the items you want the parent form to extract as properties of the child form. In the parent form's code, when ShowDialog returns you can get the values you want to save from the properties.
2) Pass a reference to the parent form to the form (perhaps when it's constructed). Then the child form can call some method or set some properties on the parent form. Note that this is not preferred as it couples the child for to the parent form, meaning it couldn't be called from any other form.
Related
I have a main form application which is an MDI container. A user can press a button to make a new form pop up (not as an MDI child), and then from this new form that popped up, I want to be able to have a button that creates a different form as an MDI child.
In the main form I have:
ResSelectForm resSelectForm = new ResSelectForm();
resSelectForm.Show();
So in the resSelect form that popped up, when the user presses an OK button, I have:
ImageForm imageForm = new ImageForm();
imageForm.MdiParent = Mainform; // doesn't work
imageForm.Show();
I get the following error:
Error CS0119 'MainForm' is a type, which is not valid in the given context
The problem you are having is you were trying to access the Mainform type and not an instance of it. To fix the problem you would have to pass the instance to the ResSelectForm constructor like so:
ResSelectForm resSelectForm = new ResSelectForm(this);
Then in the ResSelectForm constructor do this:
private Mainform _mainform; //Variable to use throughout the class
public ResSelectForm(Mainform mainform)
{
_mainform = mainform;
}
Lastly whenever you need to access Mainform you'd access the variable, in your case like so:
imageForm.MdiParent = _mainform;
I just ended up using a dialog box for it, since it's essentially the same thing:
https://www.youtube.com/watch?v=8aDsXyiBLsI
I've been having an issue that's been bugging me for the past 3-4 hours.
Basically, I have a main form, that is an MDI parent. I have another form, that is an MDI child of that parent. I use the second form for logging in and after I've been logged in, I want to add new items to the menuStrip of that main form, but for some reason I can't. I tried debugging and I saw that the menuStrip's indexes are expanding correctly for the new items, but they are not being updated in the form. I tried displaying the main form again with this.Show. This worked! I got a new form displaying the new items in the menuStrip. However, I just want to update them and not create another form. I tried refreshing/updating, but nothing worked. :/ Any suggestions?
Thank you!
EDIT:
Here is some more info:
this is the code from the 2nd form
if (successLogin==DialogResult.OK || successLogin==DialogResult.Cancel)
{
Form1 main = new Form1(); //define main form
FullUser = textBox1.Text; //getting the username (will use it for the menuStrip items "Logged in as: "+ FullUser)
main.LogedIn(FullUser);//calls method
this.Close(); //closing the 2nd form
}
that calls for the public method in the main form
public void LogedIn(string user)
{
menuStrip1.Items.Add("Item here"); //adding the item to the menuStrip
}
Edit 2: I put a timer which starts when opening the 2nd form and checks for a public boolean. That got it working, but still not the way I wanted it to.
You are creating a new form of type Form1 in the line:
Form1 main = new Form1();
And adding the items to that new form, instead of your existing one.
If the form is the MDIParent of the second form as you state in your question, you could change that line to:
Form1 main = (Form1)this.MDIParent;
If it's not, then you need to keep a reference to the existing form. If you create a new one (like you are doing) then the items are added to that new one, but not to the one that is already created.
I have these forms:
MainScreen - MDI container
DataBaseOutput - child
NewAnime - child
DataBaseOutput has a tab control that holds datagrids, each for different tables. I use an access database.
In those tabs, there is a menustrip, where the functions "New", "Edit", "Delete" etc. will be called from. Now when I'm on the first tab's menustrip and click on "New" I want to open the form "NewAnime", inside the MDI container. This however, is not working as I planned. At first I tried to just call it from the childform (DataBaseOutput). This resulted in opening a new form instead of a child. when I made it a child it was not showing up.
Then I triend lots of things, but I still haven't figured it out.
This is the current code for calling the form. It calls the form with a method in the main form:
private void NewAnime_Click(object sender, EventArgs e)
{
MainScreen main = new MainScreen();
main.mShowForm(2);
this.Close();
}
Method in the main form:
// Forms for MDI Parent
DataBaseOutput OutputForm = new DataBaseOutput();
NewAnime AddAnime = new NewAnime();
// How i made them childs (this is at the InitializeComponent(); part)
OutputForm.MdiParent = this;
AddAnime.MdiParent = this;
public void mShowForm(int formnumber)
{
switch (formnumber)
{
case 1: OutputForm.Show(); break;
case 2: AddAnime.Show(); break;
}
}
Does anyone have a clue of what i'm doing wrong and maybe has a better idea? This might be a bit too much working around, but as I said, it's my first time using MDI forms and i'm just trying to get it to work.
Have you set the MainForm to be an MDIContainer? To do this set its IsMdiContainer property to true; Also check it has File and Window top-level menu items and New and Close menu items. (The tutorial suggests this, I know it should have a Window menu item at least).
Have a look at this tutorial for more guidance: Creating MDI Child Forms (MSDN)
EDIT: Looking at it more closely, it seems you are creating a new instance of MainForm, and trying to show the form as a child of that instance, as opposed to showing it in the existing MainForm. I assume you already have an instance of MainForm open at this point? And assuming the OutputForm and AddAnime forms are children of MainForm you could call the existing instance's method like this:
private void NewAnime_Click(object sender, EventArgs e)
{
this.ParentForm.mShowForm(2);
this.Close();
}
but ideally you should have an event on DataBaseOutput that MainForm listens to, and shows the new Form when the event is raised. See here for more info (it talks about user controls and not child forms, but the principle is the same):
Calling parent form functions from a user control
I know this mite be a bit of a silly question but how do i create another window from my main window in c# windows application? I don't know where to look for this type of questions.
You can use the following to create a new form. Note that I have provided two examples.
// This example creates a new Form control. While this Form is open,
// you cannot gain focus of the parent form.
Form form = new Form();
form.ShowDialog();
// If you want to be able to use both Forms. Then this is what you want:
Form form = new Form();
form.Show();
Also, MSDN is your bestest friend: MSDN on Windows Forms.
...And Google.
What about:
YourForm newForm = new YourForm();
newForm.Show();
You have several methods of showing your form. I use YourForm as a name here, replace that with the classname of your own form.
Note that a form-class is nothing more than a regular class that can be instantiated like any other class using new and inherits all methods from it parent calls (Form in this case), which includes the methods Show and ShowDialog. You can create as many instances of your class (i.e., of your form) as you like.
I will assume you are using winforms and will walk you through a simple example:
In Solution Explorer, Right Click your project and select Add | New Item...
Select the type About Box and you will see a new AboutBox1.cs get generated.
Select View | Toolbox to get the Toolbox to appear.
On your main form, drag a Button from the ToolBox | Common Controls onto the form.
Double click the newly created button to create the Clicked Event.
the clicked event type the following code:
AboutBox1 aboutBox = new AboutBox1();
aboutBox.ShowDialog();
This code will declare a variable aboutBox of type AboutBox1 and then instantiate it ( construct it ). Then you call the ShowDialog() method to get it to appear.
In my application... to navigate between winforms what i do is that i make an object of the form that needs to be shown and i use
Register reg = new Register()
reg.show();
this thing has two problems
if i do it with a button, more than
one instance of same form could be
opened.
if i close through which the instance
was created, the child form stays
opend.
what is the solution....
have the child form take as a parameter the parent form:
Form2 f2 = new Form2(this);
this.hide();
f2.show();
then when you wish to close the new form you just close it and show the parent form again.
code from Form2:
private Form Fatherform;
Form2(Form father){
Fatherform = father;
}
Form2_closeevent( ... )
{
if(Fatherform != null)
Fatherform.show();
Take a look at this code sample from MSDN code gallery. If you go through the code in detail, you should be good to go