I made 2 forms in C# visual studio.
How can I make one form invisible and another visible (Have done this in visual basic only before)
I guess Syntax should be similar.
Use the Form.Visible property, or the Form.Show() method.
To hide and show a form, use the Form.Visible property:
Form.Visible = true;
Form.Visible = false;
There's also methods that do the same thing (these are designed to be used with the MethodInvoker delegate):
Form.Show();
Form.Hide();
If I remember correctly, VB.NET will pre-create the forms for you, and you only have to call Show(). In C#, you will have to create all but the MainForm.
// in a buttonClick on Form1
Form2 f2 = new Form2();
f2.Show();
This will create a new instance each time you click the button.
Use the property Visible of the class Form.
Related
I have 2 forms, one of which is the keyboard and the other with the text field.
But when the forms load, the form with the text field becomes active and then I can't use the keyboard.
It's working fine with notepad.
I want to write to the text field using my keyboard.
Thank you
You can use this code to show multiple form at the same time
Assume that you run this code from button click of main form
Form1 form1 = new Form1();
Form2 form2 = new Form2();
form1.Show(this);
form2.Show(this);
The this keyword is optional, and can be removed.
p.s: I suggest use correct naming in your app to help better coding and debugging. Something like keyboardForm instead of form2 will be more readable and helpful.
The thing was, I want to write to the text field using my own forms keyboard.
here is the code I found (mentioned by #RandRandom) and post
Thread thread = new Thread(() => Application.Run(new MainForm()))
{
IsBackground = false
};
thread.Start();
This works because this is, not to fall the second form (keyboardForm) in the background
I want to open a new form in C#, so I write:
Form2.Show()
But Visual Studio tells me this error:
An object reference is required for the non-static field, method, or
property Control.Show()
The error is generated because you need to call the Show method on an instance of Form2 class, which means that you need to create an object and call that method on that object:
Form2 form2 = new Form2();
form2.Show();
I really suggest you to learn the theory of Object Oriented Programming in deep, so that you can actually understand what's going on whenever you might encounter such errors.
When a method is not static, you can only use it on an instance of the class.
Form2 form = new Form2();
form.Show();
Firstly: Reply to the the main questionnaire.
I think you are from Visual Basic .NET background so you are trying to open the form same like Visual Basic .NET way to open form.
In C# you can not open a form like that - you have to make an object of the form first.
Secondly: Reply to answering persons, myForm.show() is used when we are loading the form in any form container.
Form2 myForm = new Form2();
myForm.ShowDialog();
with the code the form will be opened.
Simple question:
I used Application.Run to start my application because I need it this way.
Now I want to add a form later in the code. But it doesn't open the form if I use new Form1. Instead it runs everything else in the constructor of the class Form1. So somehow it ignores to open the form.
Just newing up a form makes it exist in memory but does not display anything.
You have to show it:
// create instance of Form1, does not show it
var myForm = new Form1();
// show the form.
myForm.Show();
see msdn documentation
As the title says I have a RibbonForm inside a SplitPanel, the problem here is that while I'm being able to place the form inside the panel, the form controls behave strange, specially the TextEdit control. When you try to place the caret between two characters, you can't, instead the whole text is selected. This is what I have
Form2 frm2 = new Form2();
frm2.TopLevel = false;
frm2.Parent = this;
frm2.FormBorderStyle = FormBorderStyle.Sizable;
frm2.WindowState = FormWindowState.Normal;
splContainer.Panel2.Controls.Add(frm2);
frm2.Show();
frm2.BringToFront();
I've tried using MdiParent but to no avail, the behavior stays the same.
I also read that a possible solution is to use a Dock Panel and a User Control instead of a Ribbon Form, the problem here is that I already have 20+ forms developed and fully working so I was hoping to find another solution.
Thank you.
After an exhaustive search, I've found the solution.
The key was to add a DocumentManager, set the main form as its MdiParent, and select a type of View, the next thing was to add this code:
Form2 frm2 = new Form2();
frm2.MdiParent = this;
frm2.Show();
I am developing Windows Form Application using Microsoft Visual C# 2010
I have two forms I made through Visual Studio GUI now I want to switch to next form on Button Click event this.hide() works but when I write next form name followed by dot than show() method does not show it seems show method is not available for it.
How can I show my new Form as It is not dynamically created I have already created it through Visual Studio GUI
When I Create It Dynamically As
Form myform = new Form();
than
myform.show();
show function exists but in my case this does not exists
Kindly tell me what's the problem
You need to create an object of the Form2 and call the show method on instance of Form2
Form2 form = new Form2();
form.Show();
EDIT: Since you edited your question and added the part Form myform = new Form();. You are actually creating an instance of base class Form. You need to create the instance of your Form which is inheriting Form class.
public partial class Form2 : Form
You need to create instance of Form2 class in this case. But your myForm should have Show method. Are you sure you have System.Windows.Forms; in using statement and Form class is from the System.Windows.Forms;. Try
System.Windows.Forms.Form myform = new Form(); //Although this is wrong but it should have show method
myform.Show();
Make an object of your second form then call Show() method
SecondForm frmsecond = new SecondForm();
frmsecond.Show();
Form myform = new Form(); is not needed here because you are trying to make myform as an object of Form Class
well.. 'you have two forms (and you are not creating them dynamically), and you want to switch from one to another on a click event', isn't ?
For example,consider you have created two forms namely Form1 and Form2 through VS GUI. And if you like to do something like
Form2.Show();
then you can't. Have you looked at your application' entry point, ie. Main() method of your application(is in 'Program.cs' most of the cases). Though you have created your Form1 statically, you will find
Application.Run(new Form1);
You got it now. You cant use a 'type'. You should use a variable.For that reason only we are creating the instance of your second Form and showing it.
Form2 form=new Form2();
form.Show();