Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello! I need some help to figure out how to code things "faster" (will explain this in a minute)

P.S Very long code!!!!

I am pretty much a beginner with C# & coding generally and do things the easiest & slowest way possible..

I got a form with 150+ pictureboxes & when clicking a picturebox it loads text information and whatnot.

If you are known for the game League of Legends this will be more understanding (I hope)
Anyhow it is a way for me to code & see how far I can get with programming hehe.

I would like to know if there is a faster way of coding this instead of coding in each and every picturebox.

This is my current code for 5 current pictureboxes.

(Code is VERY long so had to upload it to pastebin, I am so sorry for that.)

The Code -> http://pastebin.com/4r73jLzg[^]

Here are the things each picturebox does.

• Loads the sound of the picturebox (Different sounds on each pB)
• Clicked Picturebox gets Borderstyle Fixed3D and changes all others to None.
• Changing programs top name to respective clicked pB
• Adds second name for a certain textbox
• txtAtk1 ~ 10 & txtDef1 ~ 10 & txtAbi1 ~ 10 & txtDiff11 ~ 10 makes the texturebox color White and thereafter colors it from 1 to 10 depending on the stats of this pB (I say pB but it is for the respective Character)
• Adding Prices for each Character (Different Price)
• Adding Health / Mana / Atk (Stats) For each champion (Different there too)
• At the end Clearing previous Images from pBSkins1 ~ 7 then adding respective Images depending which picturebox is clicked.

Here are some images with how it looks like..

http://i.gyazo.com/e0e623a40a86338c5ee7a91408815540.png[^]
http://i.gyazo.com/67c166de694d276682a22b928ac59c8a.png[^]
http://i.gyazo.com/a2ddcf51d83ff01a78b0d2a11afb0ede.png[^]
http://i.gyazo.com/732d52622144c4d3152783bf23924b2e.png[^]
http://i.gyazo.com/f9f9e803bc65d8e42eee2b97015e82e8.png[^]
Posted
Comments
Emre Ataseven 18-May-14 14:34pm    
Where are you loading from these data? From a server or a database or a file?
Xakzi 18-May-14 14:40pm    
ah sorry forgot to say that.. Everything is loaded from resource.resx (Thought I can load from folder if it has to.)
Emre Ataseven 18-May-14 15:07pm    
Wow I saw your code, you should immediately change this design and make the most of OO design, and don't use resx for this purpose, use a xml file and let program loads all information and creates all visuals dynamically.
Xakzi 18-May-14 15:23pm    
I know right...<br>
It is not as easy as using an combobox.. much less codin & everything works automaticly.. <br>
A small of that code. <br>
pictureBox2.Load("Champions\\" + strChampNameBox1 + "\\Ability\\" + "P.png");<br>
pictureBox3.Load("Champions\\" + strChampNameBox1 + "\\Ability\\" + "Q.png");<br>
pictureBox4.Load("Champions\\" + strChampNameBox1 + "\\Ability\\" + "W.png");<br>
pictureBox5.Load("Champions\\" + strChampNameBox1 + "\\Ability\\" + "E.png");<br>
pictureBox6.Load("Champions\\" + strChampNameBox1 + "\\Ability\\" + "R.png");<br>
pictureBox8.Load("Champions\\" + strChampNameBox1 + "\\" + strChampNameBox1 + "_Square_0.png");

Yeah, don't use PictureBoxes at all.

Learn to draw directly on the control surface, like TabPage or Form or a Panel control. This is done in the Paint event for each control.

Google: How to draw on form[^]
 
Share this answer
 
Comments
Xakzi 19-May-14 18:49pm    
Really helpful actually!
Drew squares as the same size as the pB works perfectly (well kinda, but I'll get there)
First of all, read data from a xml file. It can be something like this;

XML
<game>
   <champion id = 1 name = "Ahri" Health = 460>
      <Skins id = 1 name = "A Ahri"/>
      <Skins id = 2 name = "B Ahri"/>
      <Skins id = 3 name = "C Ahri"/>
      <Skins id = 4 name = "D Ahri"/>
   </champion >
   <champion id = 2 name = "Akali" Health = 550>
      <Skins id = 1 name = "A Akali"/>
      <Skins id = 2 name = "B Akali"/>
      <Skins id = 3 name = "C Akali"/>
      <Skins id = 4 name = "D Akali"/>
   </champion >
</game>


Put all those character images into a specific folder, like "Char Images". I know character names are unique, you can name images by char name.

+ Now read this xml, you have an enumerable XDocument variable.
+ Put a flowlayoutpanel docked into your "Select champion" tab.
+ Foreach XElement in this XDocument
+ I don't know what kind of operations you are doing with picturebox of champs but you'd rather make a usercontrol instead of it. Populate this usercontrol with XElement, register its click event, and add to flowlayoutpanel.


C#
private void LoadXml(string xmlPath)
{
    var doc = XDocument.Load(xmlPath);
    var champGroups = from grp in doc.Descendants("champion")
                      select grp;

    foreach (XElement champion in champGroups)
    {
        Champion champUC = new Champion(champion);
        champUC.Click += champUC_Click;
    }
}

void champUC_Click(object sender, EventArgs e)
{
    (sender as Champion).BorderStyle = BorderStyle.Fixed3D;
    flowLayoutPanel1.Controls.OfType<Champion>().ToList().ForEach(p=> p.BorderStyle = BorderStyle.None);
    //Populate other controls with this champion object, like;
    //CharacterAttributesUC.Load(sender as Champion);
    //SkinsUC.Load(sender as Champion);
}


If you stuck, I can go further.
Happy coding.
 
Share this answer
 
Comments
Xakzi 19-May-14 18:50pm    
Really helpul trying this out at the moment.
So far so good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900