-1

When I refresh my form with SHOWDIALOG my image doesn't load in my picture-box but if i refresh with only SHOW it works fine.

I want to be able to refresh with SHOWDIALOG and still have my picture load method work.

I have tried clearing data-bindings of both picture-box and the brows button.

private void formrefresh()
{
     FoodItem FoodItem = new FoodItem();
     FoodItem.ShowDialog();
     this.Close();           
}

public void GetImage()
{
     OpenFileDialog BrowseImage = new OpenFileDialog();

     BrowseImage.Filter = "Image Files(*.jpg; *.gif;)|*.jpg; *.gif";

     if (BrowseImage.ShowDialog() == DialogResult.OK)
     {
          TextBox t = 
               Application.OpenForms["FoodItem"].Controls["imagePath"] as TextBox;
          t.Text = BrowseImage.FileName;
          filenametext = BrowseImage.FileName;
          PictureBox p = Application.OpenForms["FoodItem"].Controls["foodImage"] as PictureBox;

          p.Image = new Bitmap(BrowseImage.FileName);
     }

}

      private void BrowsImage_Click(object sender, EventArgs e)
      {
        GetFoodImage image = new GetFoodImage();

        image.GetImage();
     }
CharithJ
  • 46,289
  • 20
  • 116
  • 131

1 Answers1

0

Finding forms (Application.OpenForms) is not a very good approach to use. Try to avoid that if possible. For example, there can be complications if there are multiple instances of a certain Form and you will have to find the exact instance you want to update.

It doesn't make much sense to use a Library to GetImage in your example. If you really need it in a separate Class Library, just return the path. Just return the image path from GetImage method and set the PictureBox from FoodImage.

private void formrefresh()
{
    FoodItem foodItem = new FoodItem();
     foodItem.ShowDialog();
     this.Close();           
}

private void BrowsImage_Click(object sender, EventArgs e)
{
  GetFoodImage image = new GetFoodImage();
  var imagePath = image.GetImage();
  this.foodImage.Image = new Bitmap(imagePath);
  this.imagePath.Text = imagePath;
 }

 public string GetImage(FoodItem foodItem)
 {
     OpenFileDialog BrowseImage = new OpenFileDialog();

     BrowseImage.Filter = "Image Files(*.jpg; *.gif;)|*.jpg; *.gif";

     if (BrowseImage.ShowDialog() == DialogResult.OK)
     {
           return BrowseImage.FileName;
     }        

     return "";
  }
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • i am unable to pass the foodItem instance instead of Application.OpenForms because this code is in an entirely different location where i can only access the form by using the Application.OpenForms. – Rickal Hamilton Aug 29 '19 at 03:43
  • almost got it. it shows the image when i re-add it but it doesn't show the image path in my imagepathtext text box. – Rickal Hamilton Aug 29 '19 at 04:03
  • 1
    i think i might be able to solve it tho. Thanks a lot @ CharithJ – Rickal Hamilton Aug 29 '19 at 04:10
  • @RickalHamilton: Great, I think you just need to set this.foodImage.Image = new Bitmap(imagePath); this.imagePath.Text = imagePath; in the browser click method. See the updated answer now. – CharithJ Aug 29 '19 at 04:13
  • your a genius lol it all works perfectly now... i will analyze tomorrow to see what made what work and what made what didn't work so as to better understand the fix. – Rickal Hamilton Aug 29 '19 at 04:19
  • I've added some details in the answer. Hope that will help you to avoid similar issues. – CharithJ Aug 29 '19 at 04:25
  • i am not so much of a professional at this as yet i have learnt so much over the past few months. so please i appreciate the help and suggestions a whole lot ...verrryyy grateful. – Rickal Hamilton Aug 29 '19 at 13:33