在c#中连接形状的矩形控件名称

时间:2017-07-23 22:38:13

标签: c# winforms powerpacks

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

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

1 个答案:

答案 0 :(得分:0)

  

尝试一些solutions here ... - Idle_Mind

     

好的会尝试 - droid斐济

     它没有真正的帮助:( - 机器人斐济

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

此代码将rectangeShape1的backColor设置为rectangeShape3为红色:

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;
                    }
                }
            }      
        }

    }

}