1

I'm fairly new to C# (and programming in general) so stick with me if I make any huge errors or talk complete bull. So what I'm trying to do is have a private void that resizes the background image of a button. I send the name of the button to the private void via a string. Anyway, the code looks something like this:

ButtonResize("Zwaard");


protected void ButtonResize(string Button)
    {
        string ButNaam = "btn" + Button;
        Button Butnaam = new Button();
        Butnaam.Text = ButNaam;

        if (Butnaam.BackgroundImage == null)
        {
            return;
        }
        else
        {
            var bm = new Bitmap(Butnaam.BackgroundImage, new Size(Butnaam.Width, Butnaam.Height));
            Butnaam.BackgroundImage = bm;
        }
     }

But it doesn't work like that. I can't seem to find a way to declare a new object named the value I have in a string. What I want my code to do is instead of making a button called "Butnaam", I want it to create a button called btnZwaard (the value of the string Butnaam).

How do I tell C# I want the value of the variable to be the name of a new button, not literally what I type?

Thanks in advance.

4

2 回答 2

2

你在寻找这样的东西吗?通过将 传递Button给方法,您可以对对象进行操作。如果这是您正在寻找的内容,那么您应该阅读传递引用类型参数

protected void ButtonResize(Button button)
    {
        if (button != null && button.BackgroundImage != null)
        {
            button.BackgroundImage = new Bitmap(button.BackgroundImage, new Size(newWidth, newHeight));
        }
     }
于 2013-10-09T18:02:29.717 回答
1

字符串是一段文本。您随后将其称为 a class,这是错误的。假设它是正确的,您创建一个新按钮而不是“调整其图像大小”。

您想要开始做的是在与具有按钮的对话框相同的类中创建一个新函数。该函数可以调整控件图像的大小。

编辑:顺便说一句,这似乎不是学习语言的好起点。请找到一个很好的 C# 在线教程(例如一个 hello world 应用程序)。

于 2013-10-09T17:53:08.113 回答