0

I have 4 picturebox. The names: pb1, pb2, pb3, pb4 And I have 4 resource file: cards_club, cards_diamon, cards_heart, cards_spades

The resource files contains some french card picture. One of the names is: Cards-6-Club.svg

So my problem is: how to reflect them using a random number.

I mean - here is the main part of the code:

    Random rnd = new Random();
            int color = rnd.Next(1,4+1);
            int value = rnd.Next(1,13+1);
            int pb_num = rnd.Next(1,4+1);
            textBox1.Text=color.ToString()+" "+value.ToString(); //this is just a helper data. It will never show to the user when the program is done
            switch (color) {
                case 1:             
                    if(value>=2 && value<=10){
                        pb??.Image = Projectname.cards_club.(Cards_+VALUE+_Club_svg).ToString();
    
                    }

My problem is: how can I use the previously generated number (stored as pb_num) here pb??.Image = , where the question mark is. And here Projectname.cards_club.(Cards_+value+_Club_svg).ToString(); how can I combine a previously generated random number (stored as value) with the name of the picture? So with this I can get a picture in the picturebox, where a random number (for example 5) shows the exact card. If I get 5 (value = 5) I want to show in the picturebox the Cards-5-Club.svg.

Thank you so much your answers, and please feel free to ask if anything is not exactly clear.

KEmelt
  • 1
  • 1
  • Every control has Tag property; maybe you can use it? – TaW Oct 20 '22 at 12:12
  • @TaW, how you mean? – KEmelt Oct 20 '22 at 12:24
  • With `Properties.Resources.ResourceManager.GetObject(["Resource by Name"])` you can retrieve a resource using its name, a string that you can build -- Do you have a PictureBox Control that can show SVG files? It's not usually a supported format – Jimi Oct 20 '22 at 14:14
  • @Jimi, thankj you!! I will take a try with this. But the Visual Studio doesn't offer me the "Properties" option. :/ I use the `using System.Resources;` and `using System.Reflection;` How can I fix this? What dou you think about the other problem? How can I use a number on the PictureBox name? For example i want to set the _**pb1**_ 's image because the `pb_num` value is 1? What should i write here: `pb??.Image` ? -- The extension of the pictures is png. It's just the filename. I'm so sorry if I confused You. :/ – KEmelt Oct 20 '22 at 16:55

1 Answers1

0

**OK, I could solved the problems. :)**

The first one:

"How can I use the previously generated number (stored as pb_num) here pb??.Image = , where the question mark is. "

The answere was soooo obvious (and this was the easier to find it)

I used this question: Name of picturebox

And the answer, witch marked with a thick. (this: https://stackoverflow.com/a/10934094/20290206)

The second one:

"And here Projectname.cards_club.(Cards_+value+_Club_svg).ToString(); how can I combine a previously generated random number (stored as value) with the name of the picture? "

It was hard to find the answere. :/ But I find! :)

I used this question to solve it: How to retrieve Image from Resources folder of the project in C#

    And these TWO answers:

This: https://stackoverflow.com/a/55959810 and that: https://stackoverflow.com/a/32156875

Because I noticed that I use a different way, to create resource files. So, at first I just clicked with right mouse button on the project name on the Solution Explorer, and choose the Add>>New Item option. In the Pop-up window and I choosed the "Resource Files" option.

This was a good method to store the pictures, and reflect them if you preciselly know their name, and you want to use it "just simply". For example if you want to use program integrated pictures when you set the picturebox-content (see the attached picture!).

My wrong method was this (it is not wrong, just not give me the option of calling a specified picture from the resources): 1th pic 2nd pic

BUT the correct way, if you want to reflect to their name is the method which is in the following link, illustrated with picture: https://stackoverflow.com/a/55959810

Then I just mothified this answere: https://stackoverflow.com/a/32156875

And I can call the picture which contains the random number. :)

So the code which is WORK :

Random rnd = new Random();
            int color = rnd.Next(1,4+1);
            int value = rnd.Next(1,13+1);
            int pb_num = rnd.Next(1,4+1);
            textBox1.Text=color.ToString()+" "+value.ToString(); //this is just a helper data. It will never show to the user when the program is done
            switch (color) {
                case 1:             
                    if(value>=2 && value<=10){
                        ((PictureBox)this.Controls["pb" + pb_num.ToString()]).Image = (Image)Properties.Resources.ResourceManager.GetObject("Cards_" + value + "_Club_svg");
    
                    }

I hope this will help someone how struggle with similar problems. :)

KEmelt
  • 1
  • 1