Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

C#: WinForms partial refresh

Hi

I have a movie database populated from TMDB api

On the main form each actor is generated by a User Control
Clicking the actors name sends both Actor Name and database person id to  People Search form
When I click the update button on the People Search form this should update the database

How do i update the UC on the main form?

Person search form
        private void PersonUpdateBTN_Click(object sender, EventArgs e)
        {
            using (var context = new MoviesEntities())
            {
                var results = context.people.SingleOrDefault(p => p.id == PersonID);
                if (results != null)
                {
                    results.name = ActorName;
                    results.tmdb_id = TMDB_id;
                    results.profile_path = profile_path;
                 //   context.SaveChanges();
                }
            }

        }

        private void OKbtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

Open in new window


Main Form
        private void GetCast(int id)
        {
            using (var context = new MoviesEntities())
            {
                var Movies =  context.movielinkpersons.Where(p => p.movieId == id).OrderBy(mp => mp.role) .ToList();
                foreach(var movielinkperson in Movies)
                {
                    var person = movielinkperson.person;
                    

                    var actorBox = new ActorBox(person);
                    actorBox.OnFilterClick += ActorBox_OnFilterClick;
                    actorBox.OnActorNameClick += ActorBox_OnActorClick;
                    if (movielinkperson.role == "actor")
                    {
                        CastFlowLayoutPanel.Controls.Add(actorBox);
                    }
                    else
                    {
                        CrewFlowLayoutPanel.Controls.Add(actorBox);
                    }
                    
                }


            }
        }

        private void ActorBox_OnFilterClick(object sender, string e)
        {
            var actorBox = (ActorBox)sender;

            /*
            Add whatever code you want into this event handler - you have access to 
            both the actorName string and ALSO the instance of the ActorBox
            where the filter button was clicked.
            */
            PeopleSearchTxtBox.Text = actorBox.ActorName;
        }
        private void ActorBox_OnActorClick(object sender, string e)
        {
            var actorBox = (ActorBox)sender;
            PersonSearchForm personSearchForm = new PersonSearchForm(actorBox.ActorName, actorBox.PersonID);
            personSearchForm.ShowDialog();
        } 

Open in new window


Main Form
User generated image
PersonSearch

User generated image
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

I'm not sure if I understand the question. What specifically are you trying to update (give an example)?
Avatar of trevor1940
trevor1940

ASKER

On the main form the picture & underlying database info is wrong for "Arnold Schwarzenegger"

The Update button on the Person search form will update the database with the  correct info

How do I tell the main form this has been updated so I can either replace the individual User Control or call GetCast again and refresh the whole section?

I thought about having an Updated bool on the Person search form  when the OK button is pressed the Main form checks if this is true?
you could get the update information from personSearchForm when it returned after ShowDialog and do a refresh of the mainform dialog then.

.....
personSearchForm.ShowDialog();
if (personSearchForm.ActorWasModified())
{
      this->DoRefresh();
}

Open in new window


note, the personSearchForm object is available after dialog close and you could get any information stored in this class object.

Sara
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
gr8gonzo
Yeah that makes sense pressing update could also close the form and set  DialogResult.OK

Where as pressing OK just closes the form

Are you any closer to publishing your article on Winforms to WPF transition?
Thanx