f2.show() method does not show the progress bar in form2 - c#

I have an application that has 2 forms. First one is where I do all the job and second one is just for displaying a progressbar.
I want to open the second one from the main form. if I use
Form2 newForm = new Form2();
newForm.Show();
Form2 opens and closes when it needs to open and close, but I cannot see the progress bar. I just can see a blank instead of it.
When I use
Form2 newForm = new Form2();
newForm.ShowDialog();
I can see the progressbar but Form2 doesn't close when it needs. It runs forever, what should I do?
I use a static public variable closeForm to close the second form. When I need to close the form I set
closeForm = true;
and in the second form, I have a timer
private void timer1_Tick(object sender, EventArgs e)
{
if (Form1.closeForm)
{
this.Dispose();
this.Close();
return;
}
else
{
progVal++;
progressBar1.Value = (progVal % 100);
}
}
this is where I put the ProgressBar value and close the form.
When I use show method, I only see blanks instead of the controls in form2. not just the progressbar, and I want form1 to close form2

first of all you need to report progress to progressbar
int iProgressPercentage = (int)(dProgressPercentage * 100);
// update the progress bar
progressBar1.ReportProgress(iProgressPercentage);
try doing that first then call this.close();

As I said above in the comment, you need to check Modal dialog from here Form.ShowDialog Method, and I just quote the following form there:
You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.
As why you can't see your ProgressBar on Form2 with Show(); you need to provide more information of how you handles it, as if I separate your program into two parts and use two button click to run them (Click button1 to show Form2; and click button2 to close it) I can see your expected result: the progressbar.
Without your further information, my best guess is something running prevents the Form2 to update its GUI.

Related

Show a form when another form is closed

On the click of a button (that's in the main form), I show a (second) form that does some parsing (decompress, extract, get information, etc) of an archive file. After this process is done, I close the second (parsing) form and show another (third) form that contains the parsed information from the archive.
Click callback looks like this:
private void ParseInputBackupButton_Click(object sender, EventArgs e)
{
Form PBF = new ParseBackupForm();
Form CBF = new CreateBackupForm();
PBF.FormClosed += delegate
{
CBF.ShowDialog();
};
PBF.ShowDialog();
}
and the second form:
private void ParseBackupForm_Load(object sender, EventArgs e)
{
new Thread((ThreadStart)delegate
{
// parse and update form
this.Invoke((MethodInvoker)delegate
{
this.Close();
});
}).Start();
}
The problem is when the third form (CreateBackupForm) appears, the second form (ParseBackupForm) doesn't close. They both appear. If I don't show the third form, the second form closes.
What am I doing wrong?
As you are calling ShowDialog on both windows, couldn't you just call them one after the other?
i.e.
PBF.ShowDialog();
CBF.ShowDialog();
The second call will not be made until the PBG dialog has closed.
Edit: The reason why the second form doesn't close is that the you are subscribing to an event raised by the form as it closes, then within that delegate calling ShowDialog which blocks the form from actually closing.
As far as I can remember .ShowDialog() is a blocking method, so would stop the other form from completing its close method until that form is also closed?
Pretty sure you can use .Show() to just make a form visible without blocking?
You could BeginInvoke the call of CBF.ShowDialog();, thus not blocking the completion of the FormClosed callback method and the closing of the PBF form.

Properties Window in C#

I'm currently trying to create a Properties Window which is opened after a Button on the Outlook Toolbar is pressed, i now have:
1) the Button on the Toolbar (currently if pressed nothing occurs)
2) i know how to create the method which would hold the action after the Button is Pressed
-but, I am a beginner and i don't know how to create a window which would open after the button is pressed, the Window should be fairly big, and for now have nothing but a checkbox(which i later would like to apply some method to.
if you ever created a window which opens after a button is pressed, i would be really pleased to get your help.
All help is appreciated, thank you
Here's the recommended way of opening a dialog window when the user clicks a button:
Add a new form to your project (e.g. MyForm) and then you can use the following code in your button's click event handler:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
if (myForm.ShowDialog() == DialogResult.OK)
{
// The code that should be executed when the dialog was closed
// with an OK dialog result
}
}
In case you do not want the new window to be modal (i.e. you want to allow the user use other parts of the application while the window is opened), the code gets even more simple:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
myForm.Show();
}
You can also create your form on the fly without adding one to your project, which is a bit more complicated, but advanced developers prefer this approach instead of messing with the designer ;)
private void OnMyButtonClicked(object sender, EventArgs e)
{
Form myForm = new Form();
myForm.Text = "My Form Title";
// Add a checkbox
CheckBox checkBox = new CheckBox();
checkBox.Text = "Check me";
checkBox.Location = new Point(10, 10);
myForm.Controls.Add(checkBox);
// Show the form
myForm.Show();
}
Here is a small tutorial for you to follow..
http://msdn.microsoft.com/en-us/library/ws1btzy8%28v=vs.90%29.aspx
EDIT: I would also recommend you remember the msdn website because it will prove invaluable for other programming issues you come across..
you have to add a new form to your project. Then you call the constructor where you want to pop up the window.
like this
Form2 form2 = new Form2();
form2.showDialog();
Edit:
where form2 is not the "main" Form of you program.
This'll set your main window to the background as long as the newly popped up window is closed.

Control.Focus() bug

I have some problems with Form control focusing.
On form1 I click a button and run the code below:
private void btnTest_Click(object sender, System.EventArgs e)
{
form2 = new Form2();
Application.Idle += new EventHandler(Application_Idle);
form2.Show();
form2.Activate();
form2.textBox1.Focus();
Form3 form3 = new Form3();
form3.ShowDialog();
}
Then, after this CLR I run the event Application_Idle on which I add a method that must focus on the textBox2 control..
private void Application_Idle(object sender, EventArgs e)
{
form2.textBox2.Focus();
form2.textBox2.Select();
form2.textBox2.Focus();
Application.Idle -= new EventHandler(Application_Idle);
}
But when I click the button on form1, I see Form2 showing, Form3 showing and then Application_Idle method raise, but form2.textBox2 control doesn't get focused...
If I comment out the form3.ShowDialog(); line it's works fine, but how do I focus a form element with another form activation?(form3.ShowDialog()) ?
Remark added:
Problem in also is I have a strict architecture and all I can change is Application_Idle method.
The issue you are having is with modality:
Forms and dialog boxes are either modal or modeless. A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application.
Dialog boxes that display important messages should always be modal. The About dialog box in Visual Studio is an example of a modal dialog box. MessageBox is a modal form you can use.
Modeless forms let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed.
When you use ShowDialog, the form that is shown prevents the caller from returning control until the dialog box is closed. If this is not the desired effect, you can use the Show method.
You could focus the textfeld, when the form itself got the focus:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.GotFocus += (s, e) =>
{
this.textBox2.Focus();
};
}
}
As John Koerner stated, you cannot set focus to Form 2 while Form 3 is open because of modality.
Since you stated that a user input in Form 3 is necessary to proceed, you should change your approach. You can place a listener watch for Form 3's closing. Only then can you set the focus somewhere else
form3.FormClosed += Application_Idle

How to hide a stack of modal forms in .NET Windows Forms

I have the following problem:
I open multiple modal forms in a stack (for example, form1 opens modal form form2 which in turn opens modal form form3, etc.). I would like to hide the entire stack.
I tried calling the Hide method or setting the Visible property on the parent, but this only hides the parent. I also tried hiding every form individually, but then I have to call ShowDialog on each of the forms which locks the thread in which I call the aforementioned method.
Is there be a way to set the modal dialogs so that they inherit the status of the parent and get hidden in a cascade just by setting the property on the first form?
I'm also open to other suggestions.
To re-show a form you hid by setting obj.Visible = false just set obj.Visible = true, not ShowDialog.
ShowDialog initiates a message loop, which will cause confusion since the dialog is already running a message loop.
Since you're talking about modal dialogs, it would be the last one opened that would commence this action. Open every form as in the following example, and then Hide() that last one.
public partial class Form1 : Form
{
Form2 frm2 = new Form2();
public Form1()
{
InitializeComponent();
frm2.VisibleChanged += frm2_VisibleChanged;
Shown += Form1_Shown;
}
void Form1_Shown(object sender, EventArgs e)
{
frm2.ShowDialog();
}
void frm2_VisibleChanged(object sender, EventArgs e)
{
if (frm2.Visible == false) Hide();
}
}

How to hold previous opened form active in C#?

I have two forms ,for example form1 and form2
the form1 is parent form ,from form1 im calling form2 and show it ,but the problem is when second form "form2" has been opened the previous form will be inactive ?
Please tell me how to have multiple form active at same time.
You probably want to use Form.Show() instead of Form.ShowDialog(). The first one will show a form along side anther one while the other will "pause" the first form until you close the 2nd one.
Use
Form.Show()
Instead of
Form.ShowDialog()
set TopMost property of form2 to true
and then use form2.Show() instead of ShowDialog()
OR
you can open form 2 in another thread like what i done
private void ShowForm2()
{
new Form2().ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(ShowForm2));
th.Start();
}
How are you showing your 2nd form? Sounds like you're showing it in a modal way - you don't want to!
Copy and paste this code into your second form class:
protected override bool ShowWithoutActivation {
get {
return true;
}
}

Categories