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.
Related
I bound a dataset in my main form to DataGridView source and using this code to call and loading last added record.
this.phoneTableAdapter1.Fill(this.sDs1.phone);
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
I have second form that include a save button. I cant use this code for call and show last record of DataGridView in main form. How to fix it? need a properties for define main form data set and use it in form2 or something else??
If second form opened from Main form, then pass main form reference to the second form through contructor
private MainForm _main;
public SecondForm(MainForm main)
{
_main = main;
}
Then use this constructor when open second form
private void OpenSecondForm()
{
SecondForm second = New SecondForm(this);
second.Show();
}
This is simple example. Give some more information how you used your forms(How second form created and showed).
Anytime I want to display a new form, I create a new form object and hide the current form.
For example:
this.Hide();
new Form2().Show();
In this way, a new object keeps getting created over and over, and the old form is still running but hidden somewhere.
I would like to know what would be the most appropriate way to do so, I've been doing this for long time and anytime I do this I feel like I am doing it wrong. I mean how can we access the previous object and set it back to show instead of creating a new one.
Use Application.OpenForms property to get already opened form object and show that.
The OpenForms property represents a read-only collection of forms
owned by the application. This collection can be searched by index
position or by the Name of the Form.
Form1 frm1 = Application.OpenForms["Form1"] as Form1 ;
if (frm1 != null)
{
frm1.Show();
}
I'm trying to access a form from my main form of my project. The new forms name will be AboutBox1 and I want it to open when a user clicks on the About toolstrip button.
When I go to code in the About toolstrip button though the Intellisense does not recognize my new form (AboutBox1) that I had created
I have read that I need to create an instance of my new form however I'm not sure where or how to do that.
Below is the code that I have. (Ignore all the stuff in the middle, the toolbox button that needs to be clicked to access the new form is at the bottom)
https://gist.github.com/anonymous/5366535
in the onClick-Handler of you MenuItem:
//assuming AboutBox is derived from Form
AboutBox AboutBox1 = new AboutBox();
AboutBox1.Show(this);
First, if you need access to your form from outside the method, make a variable at the top of the class.
AboutBox1 frmAboutBox;
[STAThread]
...
But seeing this is an About page I don't think you need that.
Then, make and launch your form.
AboutBox frmAboutBox = new AboutBox();
frmAboutBox.Show();
You can also do frmAboutBox.ShowDialog() to make the newly hatch form always be on top.
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.