tags:

views:

669

answers:

4

In my application I have a mainform. When the open button is clicked I want to show a second (borderless) form whith the text loading. I've got this working so far.

But what I want is that the loading form is centered relative to the mainform. How do I do this?

SOLUTION:

private void tsbOpen_Click(object sender, EventArgs e)
{
    if (_fileDialog.ShowOpenDialog() == DialogResult.OK)
    {
        _progress = new frmProgress(); // _progress is a member var
        backgroundWorker1.RunWorkerAsync("open");
        _progress.ShowDialog(this);

    }
}
+8  A: 

You can set StartPosition to CenterParent and pass the mainform as an Owner.

winSharp93
Thnx, but I show the second form in a thread. And when I try form.Show(this) I get this error: 'Cross-thread operation not valid: Control 'frmProcessBuilder' accessed from a thread other than the thread it was created on.'
Martijn
Only works for `ShowDialog()`.
Joey
I can't use ShowDialog() because than my code stops until this dialog is closed. That's not what I need. The form must as long displayed until the loading is finished.
Martijn
@Martijn: Looks like you have a threading problem. You have to use Control.BeginInvoke when accessing a form from another thread than the UI tread. However, threading is the solution to the "code-stopping" when using ShowDialog. Using Show, however, is no solution, too: Then the user is able to manipulate the form which is being constructed.
winSharp93
I can't work it out. Now I try to use ShowDialog. I've moved my code. Now I have this: frmProgress progress = new frmProgress(); progress.ShowDialog(this); backgroundWorker1.RunWorkerAsync("open"); progress.Close();But now nothing happens, because of the ShowDialig(). How do I put this in the Do_Work event? If I copy this code I get the thread error.
Martijn
Try putting the "RunWorkerAsync" call before the "ShowDialog".In the "Do_Work", you will have to use Control.BeginInvoke when you want to access controls.
winSharp93
Thnx, that did the trick!
Martijn
A: 

Get the position of the main form coordinates and its size and take the size of child form and put some simple mathematics on it.

S M Kamran
Thnx, but this ain't working. Maybe because I run this code from a thread? (Backgroundworker_DoWork())
Martijn
A: 

Martijn try this

at the start of the method put some code like this

public sub Bah()
{
if (me.InvokeRequired)
{
 me.Invoke(new action(Bah));
return
}

myform.showdialog...
}

dont know if this code compiles to 100% but you get the idea

Petoj
A: 

I created a subform named ProcessingRequest and I put some text and an animated gif on it.

I have a Property in my main form that calculates the location my sub form should be in.

private Point ProcessingLocation { get { return new Point(this.Location.X + this.Width / 2 - new ProcessingRequest().Width / 2, this.Location.Y + this.Height / 2 - new ProcessingRequest().Height / 2); } }

I have a class that makes a new thread to show the sub form.

    public class ShowProgress
    {
        static private System.Drawing.Point point;
        static private ProcessingRequest p;
        static public void ShowProgressForm(System.Drawing.Point myPoint)
        {
            point = myPoint;
            Thread t = new Thread(new ThreadStart(ShowProgress.ShowForm));
            t.IsBackground = true;
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
        static private void ShowForm()
        {
            p = new ProcessingRequest();
            p.StartPosition = FormStartPosition.Manual;
            p.Location = point;
            p.TopMost = true;
            Application.Run(p);
        }

        static public void CloseForm()
        {
            p.Invoke(new CloseDelegate(ShowProgress.CloseFormInternal));
        }

        static private void CloseFormInternal()
        {
            p.Close();
        }
    }
    public delegate void CloseDelegate();

Then in my main form I simply put

ShowProgress.ShowProgressForm(ProcessingLocation);
//heavy processing code goes here or whatever
ShowProgress.CloseForm();

:)

Biff MaGriff