-1

这是在 WinForms 中。我正在使用microsoft.vsualbasic.powerpacks 如何在 c# 中连接矩形控件名称这是我到目前为止所拥有的

  string n = "1";
  Rectangle match = this.Controls.Find("rectangleShape" + n,true)[0] as Rectangle;
  match.BackColor = Color.Red;
4

1 回答 1

0

在这里尝试一些解决方案... – Idle_Mind

好的,我会试试 - 机器人斐济

它并没有真正帮助:( – droid fiji

这是一个基于我在评论中链接到的解决方案的工作示例。请注意,您的 RectangleShape 的BackStyle属性需要不透明才能看到您设置的颜色!

此代码会将rectangeShape1 到rectangeShape3 的BackColor 设置为红色:

using Microsoft.VisualBasic.PowerPacks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string rectName;
            string rectBaseName = "rectangleShape";
            var shapeContainer = this.Controls.OfType<ShapeContainer>().FirstOrDefault();
            if (shapeContainer != null)
            {
                for (int i = 1; i <= 3; i++)
                {
                    rectName = rectBaseName + i.ToString();
                    RectangleShape match = shapeContainer.Shapes.OfType<RectangleShape>().FirstOrDefault(o => o.Name == rectName);
                    if (match != null)
                    {
                        match.BackColor = Color.Red;
                        match.BackStyle = BackStyle.Opaque;
                    }
                }
            }      
        }

    }

}
于 2017-07-24T05:12:41.507 回答