Hi all,

I have a very easy question (I think, at least), but I can't for the life of me figure it out, which makes me feel stupid.

I'm trying to make a function to generate buttons (I have quite a few buttons in my app and don't want to write them all out). So I have this:

private void buttonGenerator(string name, string text, int height, int width, int top, int left)
        {
            Button[name] = new Button();
        }

But my problem is this: How do I access the content of variable "name" instead of creating a load of buttons, all called name?

Thanks for the help, and sorry for the newb-question.

Recommended Answers

All 9 Replies

buttonGenerator(string name, string text, int height, int width, int top, int left)
when you call this method you do something like this:
buttonGenerator("firstBtnname", "Btntext", Btnheight, iBtnwidth, Btntop, Btnleft)
Here name is a placeholder for "firstBtnname" , so whenever you use name IN your method it will be reffering to "firstBtnname"
So Button[name] would become Button["firstBtnname"] I doubt if that would compile.

What variable-type is Button ?

Alxandr: I assume Button is a class that lives in the System.Windows.Controls namespace.

Not the class. He's named some sort of array Button. Or else, the code Button[name] is (as well as I know) meaningless.

Hi all,

I know the current code doesn't work, I've tried () [] and {} brackets in order to accept it as a variable instead of a hardcoded name.

This is how I'd like it to work: buttonGenerator(multiply, *, 30, 30, 50, 50) after which it creates a new button named "multiply", with text "*", and with the location and size supplied.

But I can't figure out how to use the variable to create a new and unique button.
If I would use: Button name = new Button(); I would create a button that would always be called "name".

Hope this clears it up.

I think I got what you mean, but it's (if I understand correctly) not possible to do that simple.

You'll need a list to handle your buttons (i suggest using a dictionary).

using System.Collections.Generic;
namespace someNameSpace
{
    public partial class Form1
    {
        protected Dictionary<String, Button> buttons;
        public Form1()
        {
            buttons = new Dictionary<String, Button>();
        }
        public void buttonGenerator(string name, string text, int height, int width, int xPos, int yPos)
        {
            if(buttons.Keys.Contains(name))
            {
                throw new Exception("A button with that name alreaddy exists!");
            }
            buttons[name] = new Button();
            buttons[name].Size = new System.Drawing.Size(width, height);
            buttons[name].Position = new System.Drawing.Point(xPos, yPos);
        }
    }
}

Though, long time since I've used C#.NET

Coming to think of it:
See if you can figure this out: perhaps one code snippet says more than 1000 words.

//create array of 100 buttons
    Button[] MyBtnArray = new Button[100];
    //your method
    private void buttonGenerator(int index, string name, string text, int height, int width, int top, int left)
        {
            MyBtnArray[index] = new Button();
            MyBtnArray[index].Name = name;
            MyBtnArray[index].Text = text;
            MyBtnArray[index].Height = height;
            MyBtnArray[index].Width = width;
            MyBtnArray[index].Location = new Point(left, top);
        }

Any questions? Ask.

Hi all,

As far as I can discern, I would be most comfortable with your second option by far.

I'll see if I can get that to work (don't think I will have any problems though), and will report back if it works.

Thanks!

Back again,

I've tested the array of buttons, and it does exactly what I want it to do! Saves me a heck of a lot of typing!

Thanks for the help all.

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.