0

This seems like a fairly basic thing to do, but for some reason it just fails silently:

    /// <summary>
    /// Sets the button to show it's busy image
    /// </summary>
    public void SetBusy()
    {
        if (Control is Button)
        { ((Button)Control).Image = BusyImage; }
        else if (Control is ToolStripButton)
        { ((ToolStripButton)Control).Image = BusyImage; }
    }

BusyImage is set using BusyImage = Properties.Resources.Busy;

If I debug this, I can see that the image appears to be setting correctly (if I hover over the Image member when at a breakpoint I can see it change), but it doesn't actually change the image when you look at the button.

I have noticed that this works when all the above code is hosted in the same Project file as the UI, but when it's shipped out to a different project (but within the same Solution), it fails silently.

Any ideas where I'm going wrong?

Thanks

EDIT 1: Even trying to set the Image to a file from the Resources of the same project as the ToolStripButton doesn't work (still fails silently).

Interestingly, it works absolutely fine when using a normal Button, regardless of which project the images are in.

Why the difference in behaviour between Button and ToolStripButton?

EDIT 2: It appears that moving the code that sets the image into the same project as the ToolStripButton works. However, I would like to keep it in a separate project if at all possible...

Jez Clark
  • 383
  • 5
  • 19
  • Do you need to force a `Refresh()`? – DonBoitnott Sep 17 '14 at 16:26
  • Is the image in the resources of your other project? Try to set the image to a bitmap you create – Ahmad Al Sayyed Sep 17 '14 at 16:43
  • Are you trying to update the image on the same thread that is already working? You'll have to set the image before starting the work or use a separate thread to perform the work so the UI can be updated. It doesn't explain why it would work in the project but not out of it, though. – ps2goat Sep 17 '14 at 16:57
  • Is it just not showing up? I mean image. – Dmitri E Sep 17 '14 at 17:07
  • @DonBoitnott, will try that and report back – Jez Clark Sep 17 '14 at 17:14
  • @Ahmad, no the image is in the Resources of the other project; to clarify, I pass the ToolStripButton from Project A to the above code which resides in Project B. The image I want to use resides in the Resources of Project B. – Jez Clark Sep 17 '14 at 17:15
  • @Dmitri E, the image does not change from the image assigned at design time. – Jez Clark Sep 17 '14 at 17:15
  • @ps2goat, no, I'm doing this from the UI thread. – Jez Clark Sep 17 '14 at 17:16
  • @DonBoitnott, no, forcing a refresh does not make any difference. I presume you meant a Form.Refresh()... – Jez Clark Sep 17 '14 at 17:44
  • `It appears that moving the code that sets the image into the same project as the ToolStripButton works.` It's not clear from your posted code how the variable "Control" is getting set. – LarsTech Sep 17 '14 at 18:10
  • SetBusy is a method within an ImageButton class I have written. Control is a public variable within that class (so ImageButton.Control). It's set by ImageButton's constructor (which has the control passed to it). Either way, Control is definitely being set correctly, I can see that when debugging. – Jez Clark Sep 17 '14 at 18:19

2 Answers2

0

try this instead, tested it and working fine at my end:

    public void ChangeImg(Component ctrl)
    {
        if (ctrl is Button)
        { ((Button)ctrl).Image = Properties.Resources.keylock; }
        else if (ctrl is ToolStripButton)
        { ((ToolStripButton)ctrl).Image = Properties.Resources.keylock; }
    }
Ahmad Al Sayyed
  • 596
  • 5
  • 13
0

Finally figured this out. To cut a long story short, I had some ToolStripButtons hidden somewhere in my form, only visible in the combobox in the designer's properties window (even when you select it from there, you can't see it on the form anywhere). I was passing the name of one of these to the ImageButton instead of the correct one (which had a default name like toolStripButton3)...

I'd love to know how it happened, I suspect user error on my part...but then again I find it strange that VS will allow a ToolStripButton to exist when it doesn't appear on any ToolStrip on the form.

Either way, my code seems to work quite happily now. The reason it appeared to work when run from the same project was that I was using a different button to test the theory.

Lots of process of elimination got it down to just two buttons that weren't playing ball; on a hunch I decided to compare the properties of the working and non-working buttons, whereupon I discovered the issue...

Jez Clark
  • 383
  • 5
  • 19