I have a MainForm Class Instance,
I bring up another form like so;
InputForm MyInput= new InputForm("Enter a Number");
MyInput.ShowDialog();
I close MyInput form from within itself like this;
private void Button_Click(object sender, EventArgs e)
{
//Do things here
this.Hide();
}
Flow resumes in the MainForm and using either
this.Show();
or
this.Activate();
neither will consistantly bring the MainForm to the front.
How can I do this?
What you need to do is show your InputForm like this. This form of ShowDialog assigns the owner to your dialogbox.
DialogResult dr = MyInput.ShowDialog(this);
//test for result here
MyInput.Close();
this.Hide() appears to be hiding the main form, not the input. Because ShowDialog is a blocking method, the InputForm will need to be closed by user action, code internal to the InputForm, or by another thread.
Related
i am writing a code for serial key registration.
if the serial key entered by the user is correct then anothr form must open and the present form must close.
please go thought the code.
namespace ExtTrigger
{
public partial class Activation : Form
{
public Activation()
{
InitializeComponent();
}
private void ActivateButton_Click(object sender, EventArgs e)
{
String key;
key = string.Concat(textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text);
if (key == "1234123412341234")
{
Properties.Settings.Default.Registered = true;
MessageBox.Show("hurray", "", MessageBoxButtons.OK);
Form1 f1= new Form1();
f1.ShowDialog();
this.Close();
}
else
MessageBox.Show("No Match", "", MessageBoxButtons.OK);
}
private void Activation_Load(object sender, EventArgs e)
{
}
}
my problem is: On clicking on ActivateBotton, Form1 opens but the present form doesnot close.
i have read in few threads that in VB we can change the property: ShutdownMode.
How can we do that in c#?
f1.ShowDialog(); blocks the call, it doesn't go to the next line until that new form has been closed.
An option would be to use:
f1.Show();
Show doesn't block the call, it passes to the next statement. It will not wait for the new form to be closed.
since you have show the second form as f1.ShowDialog() so first one remain open untile second one close, try this
Form1 f1= new Form1();
f1.Show();
this.Close();
The following code should do the trick:
using(Form1 f1 = new Form1())
{
this.Hide();
DialogResult result = f1.ShowDialog();
if(result == DialogResult.OK)
{
this.Show();
}
}
You create your new form within the using-block, then you hide your main form(or the form you are in at the moment) create a DialogResult that gets set by the newly opened form and open this form. Now you can set the results you want to check for inside of your new form and if everything went well inside of you new form you set the DialogResult to OK via:
this.DialogResult = DialogResult.OK;
Now back in our first form you check for the DialogResult and if it is okay you show your main form again. If it was not okay you could just reopen the 2nd form and let the user try again.
Opening a new form is very simple, but the way you do it really depends on your need.
Case 1: I would like to freeze/ block the calling form on secondary form call
In this case you should be using secondaryFormObj.ShowDialog();
Of course, when using this technique your called form, which now acts as a dialog, should "return" an answer to its caller parent on closure.
For example:
private void SecondaryForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Just a dummy code example.
// Always returns Yes result on form closure
this.DialogResult = System.Windows.Forms.DialogResult.Yes;
}
For more help and examples on this manner you can use MSDN:
DialogResult - MSDN
Case 2: I would like to have both forms responding at the same time
In this case you basically need to call secondaryFormObj.Show();
If you want the caller form to be hidden on secondary form call, just
invoke this.Hide();
after the call to secondaryFormObj.Show(); in the caller class.
You can also close the caller form using this.Close(); as long as the caller form is not the application's main form.
... And remember
Always make sure you initialized the secondary form object before
invoking it with either secondaryFormObj.Show(); or
secondaryFormObj.ShowDialog();
Initializing a form is done the same way like every typical object using the
new operator.
For example: secondaryFormObj = new Form();
Hopes this helps. Happy coding!
I have 2 forms. I'm opening form2 with ShowDialog(), but then when I close it (by hiding it) form 1 disappears for few seconds, but if I use show to open form 2 then this does not happen.
I need to use ShowDialog(), how could I fix disappearance of form 1 after form 2 closes ?
I tried to use Form1.Show() right after I close form 2 with Hide() but does not work.
Form1
private void p0_igra2_Click(object sender, EventArgs e)
{
this.CenterToScreen();
imevislice.ShowDialog();
}
Form2
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
errorProvider1.SetError(textBox1, "Polje more biti izpolnjeno");
else
{
errorProvider1.Clear();
ime = textBox1.Text;
if (radioButton1.Checked)
izbrane_besede = "SLO";
else
izbrane_besede = "ENG";
this.Hide();
form1.Show();
form1.namehangman();
}
}
If you open a form with ShowDialog you need to close (Form.Close) it when you are done. ShowDialog starts a modal dialog; which means the calling function will not continue execution until the form is closed.
Using Show can fix this, but you didn't say what wasn't working with that approach.
In general, ShowDialog is pretty cheap; you should be able to just close the form correctly and you won't run into any issues.
I am making a Windows Forms application. I have a form. I want to open a new form at run time from the original form on a button click. And then close this new form (after 2,3 sec) programatically but from a thread other than gui main thread.
Can anybody guide me how to do it ?
Will the new form affect or hinder the things going on in the original main form ? (if yes than how to stop it ?)
To open from with button click please add the following code in the button event handler
var m = new Form1();
m.Show();
Here Form1 is the name of the form which you want to open.
Also to close the current form, you may use
this.close();
I would do it like this:
var form2 = new Form2();
form2.Show();
and to close current form I would use
this.Hide(); instead of
this.close();
check out this Youtube channel link for easy start-up tutorials you might find it helpful if u are a beginner
This is an old question, but answering for gathering knowledge.
We have an original form with a button to show the new form.
The code for the button click is below
private void button1_Click(object sender, EventArgs e)
{
New_Form new_Form = new New_Form();
new_Form.Show();
}
Now when click is made, New Form is shown. Since, you want to hide after 2 seconds we are adding a onload event to the new form designer
this.Load += new System.EventHandler(this.OnPageLoad);
This OnPageLoad function runs when that form is loaded
In NewForm.cs ,
public partial class New_Form : Form
{
private Timer formClosingTimer;
private void OnPageLoad(object sender, EventArgs e)
{
formClosingTimer = new Timer(); // Creating a new timer
formClosingTimer.Tick += new EventHandler(CloseForm); // Defining tick event to invoke after a time period
formClosingTimer.Interval = 2000; // Time Interval in miliseconds
formClosingTimer.Start(); // Starting a timer
}
private void CloseForm(object sender, EventArgs e)
{
formClosingTimer.Stop(); // Stoping timer. If we dont stop, function will be triggered in regular intervals
this.Close(); // Closing the current form
}
}
In this new form , a timer is used to invoke a method which closes that form.
Here is the new form which automatically closes after 2 seconds, we will be able operate on both the forms where no interference between those two forms.
For your knowledge,
form.close() will free the memory and we can never interact with that form again
form.hide() will just hide the form, where the code part can still run
For more details about timer refer this link, https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netframework-4.7.2
You just need to use Dispatcher to perform graphical operation from a thread other then UI thread. I don't think that this will affect behavior of the main form. This may help you :
Accessing UI Control from BackgroundWorker Thread
This might also help:
void ButtQuitClick(object sender, EventArgs e)
{
QuitWin form = new QuitWin();
form.Show();
}
Change ButtQuit to your button name and also change QuitWin to the name of the form that you made.
When the button is clicked it will open another window, you will need to make another form and a button on your main form for it to work.
private void btnchangerate_Click(object sender, EventArgs e)
{
this.Hide(); //current form will hide
Form1 fm = new Form1(); //another form will open
fm.Show();
}
on click btn current form will hide and new form will open
What is the best way to close a form and get to another form.
At present I have a Main Form and then two forms:
I want to know how to close one form and open another form efficiently.The two times I did it had a slight different outcome:
The main form in my application is just the introduction which will load some Company Logo and show a progress bar which will then take it to a log-in form, where in I have a log-in button, from where the actual application will open.
I have some questions.In the Main Form I have added this code:
private void Form1_Load(object sender, EventArgs e)
{
int i;
for (i = 0; i <= 100; i++)
{
progressBar.Value = i;
label1.Text = "Please wait while Refresh loads up...";
}
}
private void progressTimer_Tick(object sender, EventArgs e)
{
this.Close();
}
private void MainForm_Deactivate(object sender, EventArgs e)
{
Form form = new FirstForm();
form.ShowDialog(this);
}
1) This works fine, just that the new form that opens up is at the task bar and is not in the center of the screen(as I have set it's property).How do I fix this?
In the First Form I have added this code:
private void loginButton_Click(object sender, EventArgs e)
{
using( ERPQueries eq = new ERPQueries())
{
int? count = eq.CheckEmployee(userTextBox.Text,passwordTextBox.Text);
if (count == 1)
{
//testLabel.Text = "Ok";
this.Close();
Form form = new SecondForm();
form.ShowDialog(this);
}
else
{
testLabel.Text = "Invalid username or password!";
}
}
}
2) Here the next Form pops up in the center of the screen.I want to know how is it different from the first case, as I have used showDialog() in both the cases?
3) Also in the first case my Main Form Disappears, whereas in the second case the First Form is still visible in the background and disappears only after closing the SecondForm.
I'm sure I'm doing a lot of mistakes, my code is flawed.Please help.This is the first time I'm making an application with multiple forms.
Edit:
When I use Show() instead of ShowDialog() I don't see the new form.Am I missing something?
I tried this.Dispose() instead of this.Close().In the first case it works fine.In the second case, it disposes all the forms.
try:
form.Show();
not
form.ShowDialog(this);
which is what makes it modal.
ShowDialog() is blocking call, so if in one Form's deactivate event you call ShowDialog() of another, your parent form will not yet finish it deactivating.
Like a suggesion I can give you create a collection/stack of forms you want to manage and give control over it to a FormManager class, where you implement whatever logic you want and can call either ShowDialog() or Show(). In other words bring out forms Show/Hide management out forms itself.
Regards.
I tried everything I know. The trouble must be that my textbox is in a groupbox. I have a Mainform from which I move to another form. When I return to Mainform, I want a particular object to be focused. How is this done?
Here's my code in my Mainform.
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.ShowDialog();
}
now this is how i return back to my Mainform from Form1.
private void button3_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
I have textBox1 in Mainform. How to set focus to textBox1 when quitting Form1 and entering Mainform. I tried textBox1.Focus(); and this.ActiveControl = this.textBox1; under Mainform Load, Show, Activated and Enter events. Still didn't work. I tried creating a public method and call it under the exit button of Form1. Like this.
In Mainform,
public void textBox1Focus()
{
textBox1.Focus();
}
And then in Form1,
private void button3_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
Mainform frm = new Mainform();
frm.textBox1Focus();
}
Which still didn't work. My textBox1 is in a groupbox. Could that be the reason?
Thanks.
I don't understand how the code that you've shown even compiles. You're calling textBox1Focus() from inside a method that's defined in the Form1 class, which as best I can understand, doesn't include a definition for textBox1Focus. That method is only defined in the Mainform class.
And no, the text box being placed in a group box is not preventing it from getting the focus. There's something else wrong with your code. It's hard to tell; I feel like I'm looking at a sunset through Venetian blinds, rather than through a large picture window.
Anyway, I suspect there's a simpler solution. Just set the focus to the textbox control at the end of the button1_Click method. The ShowDialog method is a blocking call, which means execution won't continue until after the user closes the second form. When that happens, execution will continue with the next line of code, which will set the focus to the textbox control.
Try changing your code to the following:
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.ShowDialog();
this.textBox1.Select();
}