Reputation: 980
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 ..
Upvotes: 0
Views: 548
Reputation: 10034
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
}
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;
Upvotes: 1