Accessing multiple forms in C# - c#

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.

Related

Multi form Projects in c# - Displaying multiple windows

My main form is this
Now my problem occurs when I click Add -> Student, which is meant to bring up a new window that, instead it does this
So as you can see it opens the new form inside the same window. It didn't use to do this, then I made the main form an MdiContainer and set it as the MdiParent of the second form.
Is there a property I can change to make this new form pop up in a new window? Becuase I need to leave the MdiContainer property as True so that I can take data from Form2 and use it in Form1
Please let me know any other information you may need to help me fix this issue, as I'm not sure what caused it so I don't know what to change in order to fix it or even where I'm meant to be looking
you need to hide form enroll
when you open add window add.show()
you need to hide enroll form enroll.hide()

"Include" instead of ShowDialog()

I'm trying to make a WindowsFormApplication in Visual Studio 2015 and need some help.
I've been trying to search for the answer on internet but can find out how to do the following:
I have two windows (solutions?). I open the second window with a button in the first one with this code:
this.Hide();
intermec prodinter = new intermec();
prodinter.ShowDialog();
My question is:
How can i "include" the second window (like "include" in PHP) instead of close the first window and then open the next one, like it does now?
A Form is just another Control. Think of it as a Container (because it holds other Controls).
A User Control can also hold more than one Control. There are ways you can display a Window inside another Window in a WinForms app, but the desired effect is not always guaranteed. So it would be best to place all of your controls (for "page 1", for example) in a User Control called "Page1", and then, when appropriate, add that User Control to the Form, and set its Dock property to Fill.
And when it's time to show a different "page", Hide(); "Page1", and Show(); "Page2".
I think you are talking about form inheritance:
Just create a form, lets call it as frmBase. And add some controls onto frmBase which you want to have on other forms as well.
Create other form, lets call it as frmDerived.
In the code behind of frmDerived, just do the following:
// derive the frmDerived form from frmBase
public partial class frmDerived : frmBase
{
public frmDerived()
{
InitializeComponent();
}
}
And then just check the frmDerived form design, it should include everything from frmBase.
And you may want to make the access modifier of some controls of frmBase to Public as required to access them on frmDerived.
I hope this will help you. :)

How to make changing "screens" in c#

I'm new in C#, and I wanted to know if there was any way to show a screen with certain elements, and then with the click of a button, switch to another screen, similar to an installer.
From my experience in Java, I would just use a few JPanels and then hide only the one i want visible.
However, I'm new to C# forms and it's very different from Java swing. Anyone understand my problem and can tell me pretty much how this works? Thanks.
Simple approach
Just use a Grid with multiple Grids inside of it. Set the Visibility property of each internal Grid (except the first one you want to show) to Hidden or Collapsed, and then set them to Visible when you want to display them.
Better approach
Create a class for each section, each of which derives from the same parent class. Create a DataTemplate for the parent class, then just have instances of the template load into the original Grid through a ContentPresenter.
You can try this creating new forms. From my experience I've tried this:
Form2 formTwo = new Form2(); // creates instance
formTwo.Show(); // displays the new form
this.WindowState = FormWindowState.Minimized; // minimizes previous form
this.ShowInTaskbar = false; // hides it from taskbar
Keep in mind that this does not close the previous form. I would recommend setting ShowIntaskbar as True if you don't mind the user seeing the form minimized.
EDIT: If you want to show new elements I suggest you can try adding a new Form class to the project then using the designer.

How can I access one Windows Forms control from another Windows Forms?

Suppose I create a window form Form.cs. It has some controls(label1, label2, button1, button1 etc.). I also create a new window form New_Form.cs under Form.cs. Now I want to access label1, label2, button1 in New_Form.cs. How can I do this?
I don't want to be offensive, but I think that using control from another form is not a good design.
In my oppinion it is not good to couple one class (in this question form1) to the inner implementation of another class (controls of `form2ยด. If you, for some reason, have to change the inner design (e.g. showing the data with a different control) of that class, you have to change the other class too. That makes coding (and of course error searching) quite difficult.
If there is a need to exchange data between two classes, I would prefer using publioc Properties for that. In the inner design you can attach them to a control, but then this "coupling" stays in the same class)
Although this can be bad design, you can access properties in another Form by making them public and then accessing them like this:
Form1 frm = new Form1();
Form1.button1.Visible = false;
Also here is an msdn page pertaining to your question
http://msdn.microsoft.com/en-us/library/f6525896%28v=vs.90%29.aspx
You can access the required properties by setting them as public properties in your Parent form. Not sure as why you would want the button, if you want some thing to be executed for the click then you should encapsulate the logic into separate methods and them make the call.
If you have fewer details to pass then make constructors for the new form which would accept those values
ChildForm child = new ChildForm([label1], [label2])
or
ChildForm child = new ChildForm([parent form reference]) // so you could access require components

How can I create a new C# Window based on my existing Window?

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.

Categories

Resources