Hi

I need to access a label's properties from a method that I will be running inside the label's click event handsler. I have no clue???
I suppose I need to add a bit more info. I have 25 labels, when you click on one label, this method will check the adjacent labels. I can write the code to check, but am having problems because I need access to certain properties, to use them in my method. Oh, C# and WinForms desktop application, working in VS.

Thanks

Recommended Answers

All 13 Replies

More information please :)

What properties do you need? What are you trying to do? What code do you have now?

Which adjacent label? The closest one on all four sites (up, down ,left and right)? Please defile which one the code needs to check Precisly.
Mitja

Hi

I'm doing a simple number slider game, by swapping the text of the label that was clicked with a label that is empty if it is adjacent(above, below, to the left and right). Previously I just wrote the code to check in each click event handler:

private void label16_Click(object sender, EventArgs e)
        {
            if (label12.Text == "") 
            {
                label12.Text = label16.Text; label16.Text = ""; 
            }

            if (label15.Text == "") 
            {
                label15.Text = label16.Text; label16.Text = ""; 
            }
            count = count + 1;
            Winner();
        }

A very long way of doing things, and now expanding it to 25 labels is redicilous.
So I want to write a method that I can implement in each label's click event handler

This is the little bit I've come up with:

private void label25_Click(object sender, EventArgs e)
        {
            count = count + 1;
            int lblNumber = 25; // Need number to perform check
            Swapper(lblNumber); // Method to check
            Winner();
        }

        private void Swapper(int lblNum) 
        {
            if((lblNum - 5) > 0)
            {
            // get label text of the label I want to check and possibly replace it with the 
            // text from the label that was clicked
            }
        }

As you can see I'm clueless
And please no code, I need some direction, but want to figure it out.

Thanks

I did the code for you. Let me know what you think.
PS: the only problem the code has, is that when is looking for a new empty contol, is looking for an order (from upper left corner, to the down right corner) - I have to use a radnom, to choose the control randomly if there is more then one available to pass the value.

PS: I used buttons instead of labels. Its the same thing, you only chage buttons with labels and will do.
Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Feb09Exercise3
{
    public partial class Form1 : Form
    {
        Random random = new Random();
        public Form1()
        {
            InitializeComponent();
            foreach (Control c in Controls)
            {
                if (c is Button)
                {
                    c.Click += new EventHandler(c_Click);
                }
            }
            RandomText();
        }

        private void c_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            string strText = b.Text;
            if (strText != String.Empty)
            {
                string items = GetAdjacents(b.Name);
                string[] values = items.Split(',');
                bool bGoOut = false;

                for (int i = 0; i < values.Length; i++)
                {
                    if (!bGoOut)
                    {
                        string name = "button" + values[i];
                        foreach (Control c in Controls)
                        {
                            if (c is Button)
                            {
                                if (c.Name == name && c.Text == String.Empty)
                                {
                                    c.Text = strText;
                                    b.Text = String.Empty;
                                    bGoOut = true;
                                    break;
                                }
                            }
                        }
                    }
                    else
                        break;
                }
            }
        }

        private void RandomText()
        {
            Button[] buttons = new Button[] { button1, button2, button3, button4, button5, button6,
                                              button7, button8, button9, button10, button11, button12, 
                                              button13, button14, button15, button16 };
        //insert into 12 buttons (out of 16) some text:
            for (int i = 0; i < 12; i++)
            {
                while (true)
                {
                    int num = random.Next(0, buttons.Length);
                    if (buttons[num].Text == String.Empty)
                    {
                        buttons[num].Text = (i + 1).ToString();
                        break;
                    }
                }
            }
        }

        private string GetAdjacents(string button)
        {
            string items = null;
            switch (button)
            {
                case "button1":
                    {
                        items = "2,5";
                        break;
                    }
                case "button2":
                    {
                        items = "1,3,6";
                        break;
                    }
                case "button3":
                    {
                        items = "2,4,7";
                        break;
                    }
                case "button4":
                    {
                        items = "3,8";
                        break;
                    }
                case "button5":
                    {
                        items = "1,6,9";
                        break;
                    }
                case "button6":
                    {
                        items = "2,5,7,10";
                        break;
                    }
                case "button7":
                    {
                        items = "3,6,8,11";
                        break;
                    }
                case "button8":
                    {
                        items = "4,7,12";
                        break;
                    }
                case "button9":
                    {
                        items = "5,10,13";
                        break;
                    }
                case "button10":
                    {
                        items = "6,9,11,14";
                        break;
                    }
                case "button11":
                    {
                        items = "7,10,12,15";
                        break;
                    }
                case "button12":
                    {
                        items = "8,11,16";
                        break;
                    }
                case "button13":
                    {
                        items = "9,14";
                        break;
                    }
                case "button14":
                    {
                        items = "10,13,15";
                        break;
                    }
                case "button15":
                    {
                        items = "11,14,16";
                        break;
                    }
                case "button16":
                    {
                        items = "12,15";
                        break;
                    }

            }
            return items;
        }
    }
}

so I've managed to get one single event handler for all my labels, but is it possible to refer to the label to the left or to the right of the clicked label??My idea was to use the label name as reference(they are all call label1 to label25)???

Yes, I have to do some modification in the code.. the best would be to use a random, to choose or left ot right, or buttom or upper label. I will post a solution.
Mitja

This is now working pretty well (just like I understood you). Hope it helps:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Feb09Exercise3
{
    public partial class Form1 : Form
    {
        Random random = new Random();
        int allButtons;
        public Form1()
        {
            InitializeComponent();
            foreach (Control c in Controls)
            {
                if (c is Button)
                {
                    c.Click += new EventHandler(c_Click);
                    allButtons++;
                }
            }
            RandomText();        
        }

        private void c_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            string strText = b.Text;
            if (strText != String.Empty)
            {
                string items = GetAdjacents(b.Name);
                string[] values = items.Split(',');

                string[] free = null;
                int counter = 0;               
                for (int i = 0; i < values.Length; i++)
                {
                    string name = "button" + values[i];
                    foreach (Control c in Controls)
                    {
                        if (c is Button)
                        {
                            if (c.Name == name && c.Text == String.Empty)
                            {
                                counter++;
                                Array.Resize(ref free, counter);
                                free[counter - 1] = name;
                            }
                        }                        
                    }
                    //FINAL DECISION OF THE NEW RE-LOCATION (using random selection between those places which are free (empty):
                    if ((i + 1) == values.Length)
                    {
                        string item = GetRandomReLocation(free);
                        //var _control = this.Controls.OfType<Button>().Where(c => c.Name == item);
                        (Controls[item] as Button).Text = strText;
                        b.Text = String.Empty;
                    }
                }
            }
        }

        private string GetRandomReLocation(string[] array)
        {
            string item = array[random.Next(0, array.Length)];
            return item;
        }

        private void RandomText()
        {
            Button[] buttons = new Button[] { button1, button2, button3, button4, button5, button6,
                                              button7, button8, button9, button10, button11, button12, 
                                              button13, button14, button15, button16 };
            //insert into 12 buttons (out of 16) some text:
            for (int i = 0; i < 12; i++)
            {
                while (true)
                {
                    int num = random.Next(0, buttons.Length);
                    if (buttons[num].Text == String.Empty)
                    {
                        buttons[num].Text = (i + 1).ToString();
                        break;
                    }
                }
            }
        }

        private string GetAdjacents(string button)
        {
            string items = null;
            switch (button)
            {
                case "button1":
                    {
                        items = "2,5";
                        break;
                    }
                case "button2":
                    {
                        items = "1,3,6";
                        break;
                    }
                case "button3":
                    {
                        items = "2,4,7";
                        break;
                    }
                case "button4":
                    {
                        items = "3,8";
                        break;
                    }
                case "button5":
                    {
                        items = "1,6,9";
                        break;
                    }
                case "button6":
                    {
                        items = "2,5,7,10";
                        break;
                    }
                case "button7":
                    {
                        items = "3,6,8,11";
                        break;
                    }
                case "button8":
                    {
                        items = "4,7,12";
                        break;
                    }
                case "button9":
                    {
                        items = "5,10,13";
                        break;
                    }
                case "button10":
                    {
                        items = "6,9,11,14";
                        break;
                    }
                case "button11":
                    {
                        items = "7,10,12,15";
                        break;
                    }
                case "button12":
                    {
                        items = "8,11,16";
                        break;
                    }
                case "button13":
                    {
                        items = "9,14";
                        break;
                    }
                case "button14":
                    {
                        items = "10,13,15";
                        break;
                    }
                case "button15":
                    {
                        items = "11,14,16";
                        break;
                    }
                case "button16":
                    {
                        items = "12,15";
                        break;
                    }

            }
            return items;
        }      
    }
}

Onlo lokked at it on my mobile, looks like I have a lot to learn, Thanks for the help, will try it out as soon as I can

This is the game you want I guess:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Feb09Exercise3
{
    public partial class Form1 : Form
    {
        Random random = new Random();
        int allButtons;
        public Form1()
        {
            InitializeComponent();
            foreach (Control c in Controls)
            {
                if (c is Button)
                {
                    c.Click += new EventHandler(c_Click);
                    allButtons++;
                }
            }
            RandomText();        
        }

        private void c_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            string strText = b.Text;
            if (strText != String.Empty)
            {
                string items = GetAdjacents(b.Name);
                string[] values = items.Split(',');

                string[] free = null;
                int counter = 0;               
                for (int i = 0; i < values.Length; i++)
                {
                    string name = "button" + values[i];
                    foreach (Control c in Controls)
                    {
                        if (c is Button)
                        {
                            if (c.Name == name && c.Text == String.Empty)
                            {
                                counter++;
                                Array.Resize(ref free, counter);
                                free[counter - 1] = name;
                            }
                        }                        
                    }
                    //FINAL DECISION OF THE NEW RE-LOCATION (using random selection between those places which are free (empty):
                    if ((i + 1) == values.Length)
                    {
                        string item = GetRandomReLocation(free);
                        //var _control = this.Controls.OfType<Button>().Where(c => c.Name == item);
                        (Controls[item] as Button).Text = strText;
                        b.Text = String.Empty;
                    }
                }
            }
        }

        private string GetRandomReLocation(string[] array)
        {
            string item = null;
            try
            {
                item = array[random.Next(0, array.Length)];                
            }
            catch { MessageBox.Show("Some error occured... simply continue!"); }
            return item;
        }

        private void RandomText()
        {
            Button[] buttons = new Button[] { button1, button2, button3, button4, button5, button6,
                                              button7, button8, button9, button10, button11, button12, 
                                              button13, button14, button15, button16 };
            //insert into 12 buttons (out of 16) some text:
            for (int i = 0; i < 15; i++)
            {
                while (true)
                {
                    int num = random.Next(0, buttons.Length);
                    if (buttons[num].Text == String.Empty)
                    {
                        buttons[num].Text = (i + 1).ToString();
                        break;
                    }
                }
            }
        }

        private string GetAdjacents(string button)
        {
            string items = null;
            switch (button)
            {
                case "button1":
                    {
                        items = "2,5";
                        break;
                    }
                case "button2":
                    {
                        items = "1,3,6";
                        break;
                    }
                case "button3":
                    {
                        items = "2,4,7";
                        break;
                    }
                case "button4":
                    {
                        items = "3,8";
                        break;
                    }
                case "button5":
                    {
                        items = "1,6,9";
                        break;
                    }
                case "button6":
                    {
                        items = "2,5,7,10";
                        break;
                    }
                case "button7":
                    {
                        items = "3,6,8,11";
                        break;
                    }
                case "button8":
                    {
                        items = "4,7,12";
                        break;
                    }
                case "button9":
                    {
                        items = "5,10,13";
                        break;
                    }
                case "button10":
                    {
                        items = "6,9,11,14";
                        break;
                    }
                case "button11":
                    {
                        items = "7,10,12,15";
                        break;
                    }
                case "button12":
                    {
                        items = "8,11,16";
                        break;
                    }
                case "button13":
                    {
                        items = "9,14";
                        break;
                    }
                case "button14":
                    {
                        items = "10,13,15";
                        break;
                    }
                case "button15":
                    {
                        items = "11,14,16";
                        break;
                    }
                case "button16":
                    {
                        items = "12,15";
                        break;
                    }

            }
            return items;
        }      
    }
}

Thanks Mitja. Have been offline for a while, but have gone through your code and got some good ideas. Here is a piece of my code:

private void control_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            Point locationCheck = new Point(0, 0);
            locationCheck = b.Location;

            //check right
            locationCheck.X = locationCheck.X + 50;
            foreach (Button but in btnList)
            {
                if (but.Location == locationCheck && but.Text == "")
                {
                    but.Text = b.Text;
                    b.Text = "";
                }

            }
            locationCheck.X = locationCheck.X - 50;


            //check left
            locationCheck.X = locationCheck.X - 50;
            foreach (Button but in btnList)
            {
                if (but.Location == locationCheck && but.Text == "")
                {
                    but.Text = b.Text;
                    b.Text = "";
                }

            }
            locationCheck.X = locationCheck.X + 50;

            //check top
            locationCheck.Y = locationCheck.Y - 50;
            foreach (Button but in btnList)
            {
                if (but.Location == locationCheck && but.Text == "")
                {
                    but.Text = b.Text;
                    b.Text = "";
                }

            }
            locationCheck.Y = locationCheck.Y + 50;

            //check below
            locationCheck.Y = locationCheck.Y + 50;
            foreach (Button but in btnList)
            {
                if (but.Location == locationCheck && but.Text == "")
                {
                    but.Text = b.Text;
                    b.Text = "";
                }
            }
            locationCheck.Y = locationCheck.Y - 50;
            counting = counting + 1;
            Winner();
        }

As you can see I ended up using the location properties of the buttons as they are easy to get to and use. Works nicely.

Right now I'm adding a best times table to mygame, and thinking about using a simple array to store the times, and then write that to a file to save.

So does it work like you want to?

Perfect! Thanks again for the help!

Hehe, anytime mate. I am glad I can do any good for you guys,
cheers and bye, bye
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.