0

I've got a Picturebox, the user can select its backgroundimage out of a resource file.

Later I want to get the resourcename out of the picturebox.

I've already tried this:

MessageBox.Show(((PictureBox)sender).BackgroundImage.ToString());

But it gave me the format of the picture.. there isnt something like:

MessageBox.Show(((PictureBox)sender).BackgroundImage.Name.ToString());

and I've already thougt about setting a Tag to the Picturebox with the picturename when setting the Image... but this is annoying as hell...

So how can I get the name of the Resource used as the backgroundimage at a Picturebox easily?

I think i have to explain the whole situation:

Ive got a form with a lot of raidbuttons...
if you select one of those buttons and click on a panel,
the panel changes to the selected radiobuttonimage...

the click event of the panel:

PanelClick(object obj ,...)
{
    if(radiobuttonApple.checked)
    {
        obj.backgroundimage = resource.apple;
    }

    if(radiobuttonPear.checked)
    {
        obj.backgroundimage = resource.Pear;
    }
}

and hundred more of those... and later on i want to know wich resourcefile the backgroundimage is..

Isnt there something like this:

(if i would name the radiobuttons like the resources)

PanelClick(object obj ,...)
{
   obj.backgroundimage = resource[selectedradiobutton.Name]
   obj.tag = selectedradiobutton.Name
}




So now im about to use LINQ:

RadioButton checkedRadioButton = panel1.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);
obj.tag = checkedRadioButton.Text;

so i only need to know how to get a resource dinamical by name e.g.:

obj.backgroundimage = resource[checkedRadioButton.Text];


and ill use a resourcemanager :

var resman = new System.Resources.ResourceManager(
    "RootNamespace.Pictures",
    System.Reflection.Assembly.GetExecutingAssembly()
)
var image = resman.GetPicture("checkedRadioButton.Text");

i hope this will work ..

Vloxxity
  • 980
  • 1
  • 9
  • 30
  • I'd go for the `Tag` solution. What's annoying about one more line of code? It doesn't have to be directly on the `PictureBox`. You can set a tag on the images themselves on initialization. – Rotem Sep 13 '12 at 08:44
  • can i set the tag via resource designer in Visual Studio? I just see the comment field... – Vloxxity Sep 13 '12 at 08:54
  • and there are about 100 diffrent pictures and thats why i dont want to set the tags per code... – Vloxxity Sep 13 '12 at 09:00
  • Why not set the tag right before assigning the resource to the picturebox? It's one line, isn't it? – Rotem Sep 13 '12 at 09:02
  • it would be one line if i could get the name of the resource.... hmm but i think i can get the name out of the selected Radiobutton ... – Vloxxity Sep 13 '12 at 09:07
  • If you don't have the name how are you assigning it to the picturebox? – Rotem Sep 13 '12 at 09:11

1 Answers1

1

Create a method to return the resource based on the selected radio button.

Example:

private resource checkResource()
{
    if(radiobuttonApple.checked)
    {
        return resource.apple;
    }

    if(radiobuttonPear.checked)
    {
        return resource.Pear;
    }
}

Then you can use it like this:

PanelClick(object obj ,...)
{
    obj.backgroundimage = checkResource();
}

or

PanelClick(object obj ,...)
{
   obj.backgroundimage = checkResource();
   obj.tag = selectedradiobutton.Name
}

EDIT:

As you said, this approach can have different problems based on the number of iterations for each assignment. To avoid this and also in the light of another solution, you can use a single event to handle all the radio button state changes like this:

First, create a resource variable to be assigned to whenever a radioButton's status changes. ie.

 private Resource bgResource;

 private void radioButton_CheckedChanged(object sender, EventArgs e)
 {
      RadioButton obj = sender as RadioButton;
      bgResource = resman.GetPicture(obj.Tag);
 }

Then any time you want to change background you can simply say:

obj.BackgroundImage = bgResource;
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
  • this is how im doing it at the moment... just without 'obj.tag = selectedradiobutton.Name' – Vloxxity Sep 13 '12 at 13:48
  • but i want to avoid the 100 if queries..
    but now i know i can do it like that :
    RadioButton checkedRadioButton = panel1.Controls.OfType().FirstOrDefault(r => r.Checked); obj.tag = checkedRadioButton.Text;
    – Vloxxity Sep 13 '12 at 14:03
  • That is a complex process and the runtime will take a longer time to execute a LINQ statement. Instead, change the if to switch, and then it won't have to test all the conditions like an if statement. – Chibueze Opata Sep 13 '12 at 14:16
  • but what would be the input for the switch? and how do you get it? ... is there something like an inverse switch eg: switch(true) ...case radiobutton1.checked ...case radiobutton2.checked ?? – Vloxxity Sep 13 '12 at 14:36
  • Oh, I understand now, simply assign a single event to all your radio controls, in that event set currentResource = the radio button's tag. If you're using Visual studio, you can do this by selecting all the radio buttons, go to the event properties, and select checkstatechanged, then in the event, you can have `RadioButton obj = sender as RadioButton`. `currentResource = obj.tag;` I'll update my answer shortly for full code. – Chibueze Opata Sep 13 '12 at 14:42