0

I have an image name as a string. The real imagename on the form is called "image". So i get something like this:

    image.Visibility = Visibility.Hidden;

string imageName = "image";
// need something here to make it usable...

changedImageName.Visibility = Visibility.Visible;

Now, a string can not be used in combination with the Visibility property. I cant really find what i must make the string to, to make it usable for the visibility property. If i see this page: http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx Do I understand correct that I make it a "enum" ? And if yes, how do I get a string to that property?

EDIT 1

I see I have not been explaining it proper enough. I forgot to mention I am using a WPF form. on this form, I have put an image.

In the initialize part, the image get set to hidden. so for example the imagename I named "Image" so I use image.Visibility = Visibility.Hidden;

later on in my code, I want to make the image visible again, depending on what the user does. so, instead if just using the name to get the image visible again, I want to use a string. this string is looking exactly as the name of the image. but i cant use the string in combination with the Visibility function. but i cant find anywhere what i must make this string to, to be able to use that visibility option on it. hope i explained a bit better now :).

Later on, i will have multiple images on the WPF window. So the key is that i will use the string, that is corresponding with the name of the image. Depending on what the user has input into the string, some image will or will not show.

EDIT 2

If you have:

String theName = ImageName.name

you can get the name of the image into a string, so you can do stuff with it. i am looking for a way to do the exact opposite of this. So i want to go from a string, to that name, so after that i can use this to control the image again.

Edit 3

some example:

        private void theButton_Click(object sender, RoutedEventArgs e)
        {

            //get the Name property of the button
            Button b = sender as Button;
            string s = b.Name;

            MessageBox.Show("this is the name of the clicked button: " + s);

            //the name of the image to unhide, is the exact same as the button, only with IM in front so:
            string IM = "IM";
            IM += s;

            MessageBox.Show("this string, is now indentical to the name of the image i want to unhide, so this string now looks like: " + IM );

            // now, this wont work, because i cant use a string for this, although the string value looks exactly like the image .name property
            // so string IM = IMtheButton
            // the image to unhide is named: IMtheButton.name
            IM.Visibility = Visibility.Visible;
}
Anders
  • 8,307
  • 9
  • 56
  • 88
Dante1986
  • 58,291
  • 13
  • 39
  • 54

3 Answers3

1

If I understand correctly, you are trying to find a control based on the name or ID of the control. If so, try this:

Control changedImage = this.Controls.Find("image", false)[0];

Depending on what you are targeting and what version you might need to tweak a little

EDIT Updated per @Alexander Galkin comments about Find returning an array. There should definitely be some checking and whatnot but I'm leaving that up to the OP.

EDIT 2 For finding a control by name in WPF see this post.

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • 1
    He's using silverlight, whether he's mentioned it or not. – George Johnston Jan 12 '12 at 21:14
  • You should consider the situation that many controls can be found, `this.Controls.Find("image", false)[0]` will return the single control (or Exception if none is found). It is probably good to immediately cast it to `PictureBox` (this is how I understand the question) and to mention that this works in WinForms only. – Alexander Galkin Jan 12 '12 at 21:15
  • Thanks @Alexander Galkin, I forgot that it returned an array, answer updated. – Chris Haas Jan 12 '12 at 21:23
  • i made some Edit to explain a bit more, sometimes its hard to describe situations haha – Dante1986 Jan 12 '12 at 21:51
1

looks like you're using WPF, so you can create a boolean to visibility converter and use it with a boolean (and create a method that receives string if necessary) and just use:

<ContentControl Visibility="{Binding Path=IsControlVisible, Converter={StaticResource BooleanToVisibilityConverter}}"></ContentControl>

or any other converter...

check this links: http://bembengarifin.wordpress.com/2009/08/12/setting-visibility-of-wpf-control-through-binding/

http://andu-goes-west.blogspot.com/2009/05/wpf-boolean-to-visibility-converter.html

http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx

EDIT 1:

so then you will have to iterate over the images and check if your string is equals to name of the Image class.

something like this (not tested):

foreach (Image img in container.Items)
{
    if img.Name == yourMagicallyString;
    {
        img.Visibility = Visibility.Visible;
    }
else 
    {
        img.Visibility = Visibility.Hidden;
    }
}
Gustavo F
  • 2,071
  • 13
  • 23
  • mm, no i dont really think this is what i was looking for. I have edited first post to explain more – Dante1986 Jan 12 '12 at 22:01
  • i will see if this edit1 can work. also i edited my post some more (Edit 2 and 3) – Dante1986 Jan 12 '12 at 22:40
  • yeah, and now you have to find the control with this name and change the visibility, to find the control: http://www.c-sharpcorner.com/uploadfile/mahesh/find-controls-by-name-in-wpf/ – Gustavo F Jan 12 '12 at 22:45
0

The code I was looking for:

  object item = FindName(IM);
  Image test1 = (Image)item;
  test1.Visibility = Visibility.Visible;
Anders
  • 8,307
  • 9
  • 56
  • 88