How to open the two forms at run time using C#? - c#

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

Related

C# Add Form to running application

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

Can I make and work with multiple copies of Form1 in winforms c#?

I have a program that's working pretty well. It's a character sheet program for a game I'm running. Right now, since it only supports 1 active window, I have to save the existing sheet and open a new one. Thus, I can only work with one sheet at a time.
Is there a way in VS2013 C# winforms that I can work with multiple copies of Form1?
This is definitely a noob question, but I'm having trouble finding help on it.
Just create a new instance of your class and call Show()
var newForm = new SomeForm();
newForm.Show();
Yes, just new up another form:
Form1 form = new Form1();
form.Show();

Form closing before new shows

Hello I'm making my first Windows Forms app in C# using Visual Studio and I have a little problem.
While I'm opening a new form and closing the previous one, when I run the app it looks like it's closing the previous form before it opens a new one.
It doesn't look good and I want to avoid it.
UserPanel MDIUserPanel = new UserPanel(Username);
MDIUserPanel.MdiParent = this.MdiParent;
MDIUserPanel.Show();
this.Close();
I don't know what is going wrong. I will be thankful for any help.
Wirth regards,
DarQScreaM
#Edit
This doesn't seem to be the case actually. Propably its that :
I have 3 forms MainForm, Login, LoggedUser.
MainForm is MDI container with FormBorderStyle set on Fixed Single
Login is child of MainForm with FormBorderStyle set on None
LoggedUser is child of MainForm with FormBorderStyle set on None
When application is runned Login form is created in MainForm. MainForm is never closed since its container.
But when i move from Login form to LoggedUser form and vice-versa its created with FormBorderStyle = Fixed Single (normal windows window) and after 0.5~second its changed into None.
Editing it into that didn't really help :
MDIUserPanel.FormBorderStyle = FormBorderStyle.None;
MDIUserPanel.Show();
#Edit2
This change fixed it for me. I don't know why setting it on Form properties didn't work properly. It looked like form was created as FormBorderStyle.FixedSingle and then it was changed into FormBorderStyle.None. If I made this manually in Load it worked but U had to fix the size of my window too. It doesn't seem to be good though. It should work from the beginning since Form properties in Designer are like that from the very beginning.
private void UserPanel_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.Size = new Size(649, 357);
}
You can use the form's Shown event to ensure that the new form have been showed before you close the old one.
UserPanel MDIUserPanel = new UserPanel();
MDIUserPanel.Shown += ((s, ee) =>
{
this.Close();
});
MDIUserPanel.Show();
If this form you are trying to close, is the main form that opens when you run your application, then that's the reason. Closing the main form will exit your application. Try instead to just hide this form instead of closing it. And to make sure you can exit your application ('cause you've hidden your main form), just override the closing event of your current form, and put an "Application.Exit()" in it.
Hope this helps you !
First Step:
this.Hide();
Second Step:
MDIUserPanel.Show();

Opening another form with a method/function that is on another thread?

is it possible to open my second form, when the method/or function opening the 2nd form
is on another thread?
i have read other threads related to this..
but it seems i cant figure out how to use the invoke
here's how i open the 2nd form
when im calling this.. nothing just happens..(because its on the 2nd thread)
TimerMode f2 = new TimerMode();
f2.ShowDialog();
please help me. i newbie to multi -threading..
You need to execute on the main window thread
Try the following:
this.Invoke((MethodInvoker)delegate{
TimerMode f2 = new TimerMode();
f2.ShowDialog();
}
This will create it on the right thread.
It should be doing something. That is because ShowDialog will run its own message loop. The TimerMode form should at least be visible and functioning. But, you are right, this really is not the best practice especially if this form will be interacting with the other forms which are already running on the main UI thread.
Here is how you might do it.
anotherForm.Invoke(
(MethodInvoker)(() =>
{
new TimerMode().ShowDialog();
}));
Note that anotherForm is a reference to one of your other forms which is already hosted on the main UI thread.

C# question - make form visible

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.

Categories

Resources