简体   繁体   中英

Slow navigation when inheriting from a base form

I am using a base form that has a menu on it so that I don't have to re-do the code for the whole application.

Essentially this form is called StudentBase.cs

Then I have another form called StudentProfile that inherits from StudentBase

    public partial class StudentProfile : StudentBase
    {
       public string selectedPage;
    }

This then inherits the menu that is in StudentBase and I don't have to re-do the menu.

On the menu, there are buttons for the individual forms.

So let's say I press on Student Profile I use this to navigate:

    private void btnProfile_Click(object sender, EventArgs e)
    {
          //I don't want the page to reload if it is the current page
          if (selectedPage != "Profile") 
          {
             StudentProfile profile = new StudentProfile();
             profile.Show();
             this.Hide();
          }      
    }

Doing this produces a very laggy result, as well as it looks very glitchy

I override selectedPage in the child forms so in the case of StudentProfile I use:

   private void StudentProfile_Load(object sender, EventArgs e)
    {
        selectedPage = "Profile";
    }

I have tested this on my friend's code and his navigation works without lag or glitch. He didn't do the inheritance on the form

The problem with your inheritance solution is that when you create an instance of StudentProfile you also create an instance of the StudentBase form. You show this new instance and hide the old one. You now have two instances of StudentBase (one visible and one hidden). As you open more forms from your menu, you get more instances of StudentBase in memory. Even though they are hidden they still consume resource. This would explain the result you see.

I suggest you do as your friend, which is by the way the typical way child forms are handled from a main menu.

So, I couldn't properly figure out how to use the UserControls. I put it on my to-do list so I can try that at the end of the project if I still have time left.

But, I figured out why it was taking so long to move from one navigation to the other.

I was selecting the student details in the base form using

Student student = new Student();
Student studentDetailsFound = student.GetStudent(2);

I never stopped it from selecting from the database every time it navigates to a new form, thus, the two-second delay every time. So there were two options to fix this: Static variables or caching .

I used the latter and now it switches to the pages rather fast.

By adding a transition on the form, it made it a lot smoother on the eyes.

Also note : if you are getting the data like I am you should first wait for the designing of the form to finish. So put the GetStudent part into this if:

if (this.Site == null || !this.Site.DesignMode)
{
    studentDetailsFound = GetStudent();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM