hi,

i work on a windows application and i have a principal form which show another form in its load event:

private void FormPrincipal_Load(object sender, EventArgs e)
        {
            FormR fp = new FormR();
            fp.Show();
            
        }

the form FormR contain this code in its load event:

private void FormR_Load(object sender, EventArgs e)
        {
                // code of retrieving data from database

            if (rrs1 == 0)   // if there is no data retrieving
            {
                MessageBox.Show(" there are no persons");
                this.Hide();
                
            }
            else
            {

                        // code of showing data in datagridview control               
            } 

        }

the problem is that when there no data retrieved from database ( the condition of "if" is checked) the form FormP is shown for second once.
thanks

Recommended Answers

All 14 Replies

Is it not possible to do the test in your principal form and then only show the FormR form if there is data.

but my application requires that FormR should be shown directly after the principal form, also when there is data to show in FormR datagridview it work normally. the problem is just when there is no data when the meesagebox appears but after that FormR is shown even i put this.Hide

thanks

Why do you want to keep FormR hidden instead of closing it?

if i close FormR i get the following error message:
"Cannot access a disposed object.
Object name: 'FormR'." in this code

private void FormPrincipal_Load(object sender, EventArgs e)
        {
            FormR fp = new FormR();
           fp.Show();            
        }

thanks

I meant closing it if there are no rows found:

if (rrs1 == 0)   // if there is no data retrieving
{
   MessageBox.Show(" there are no persons");
   [B]this.Close();  [/B]      
}

Behind the scenes what happens is that your code gets to the end of the method and windows shows the form. Effectively, your request to "Hide" is overriden internally.

Instead, you should hook the "Shown" event. In this method, you can hide/close the form as normal.

Note though, that the form will open for a fraction of a second. The best option is to check first and show the form only if necessary.

It's a lot more complex than this but effectively what your FormPrincipal code does is this...

FormR fp = new FormR(); // Constructor is Called...
fp.Show()
{
    FormR::Show(){
         LoadForm(); // Does some WinForm init (Creates Window handles, Init GDI. etc) and fires your FormR_Load event hook
         ShowForm(); // Gets the Window system to show the form. Fires the FormShown event at the end of the method.
    }
}

I meant closing it if there are no rows found:

(Toggle Plain Text)

if (rrs1 == 0) // if there is no data retrieving
{
MessageBox.Show(" there are no persons");
this.Close();
}

yes when i put this code i get the message error.

Instead, you should hook the "Shown" event. In this method, you can hide/close the form as normal.

Note though, that the form will open for a fraction of a second. The best option is to check first and show the form only if necessary.

can you tell me what is the code please?

thanks for replies..

I've already told you what to do and how to do it. You should implement it yourself in order to make it an effective learning exercise.

hi,

after searching on net about how to hook shown event of a form i try this code

private void FormPrincipal_Load(object sender, EventArgs e)
        {
            FormR fp = new FormR();
            fp.Shown  += new EventHandler(FormPrincipal_Load );
            fp.Show();
            fp.Close();    
        }

but it closes FormR when the "else" condition is checked too..
i'm a beginner in c#, i think it is not like that, can you help me much more please
thanks for reply

Check the event list in the Designer. You don't want it to be looping back to the Load event.

where can i find the event list?
sorry i bother you...
thanks

In the designer, select the form. On the right, where the properties are, you'll see a little lightning bolt symbol. Click that. That's the event list.

Ahh... i know it but i think that you mean another thing..sorry
i check it there is only the code of the load event. what do you mean by this

You don't want it to be looping back to the Load event.

thanks again

There is an event called "Shown" you want that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.