C#自定义控件问题

uppaway 2014-08-21 08:43:40
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
public partial class UserControl1 : UserControl
{
private int count = 4;
public int Count
{
get { return count; }
set
{
count = value;
this.Invalidate();
}
}

public int index = 0;

public string name = "";

public Color color;

public UserControl1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

Graphics g = e.Graphics;

#region 定义颜色
if (index <= count)
{
if (index == 1)
{
color = Color.Red ;
}
if (index == 2)
{
color = Color.Black;
}
if (index == 3)
{
color = Color.Blue;
}
if (index == 4)
{
color = Color.SkyBlue;
}
}
else
{
count = index;
if (index == 5)
{
color = Color.Coral;
}
if (index == 6)
{
color = Color.CornflowerBlue;
}
}
#endregion

#region 分割图形
Pen pn1 = new Pen(Color.Black, 2);
for (int i = 0; i < count; i++)
{
if (i < count)
{
Point pot1 = new Point((this.Width * i) / count, 0);
Point pot2 = new Point((this.Width * i) / count, this.Height);
g.DrawLine(pn1, pot1, pot2);
}
}
#endregion

#region 写字填充
Font verdanaFont = new Font("Verdana", 10, FontStyle.Bold);
Rectangle rect = new Rectangle(this.Width * (index - 1) / count + 1, 0, this.Width / count - 1, this.Height);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.EllipsisCharacter;
g.FillRectangle(new SolidBrush(color), rect);
g.DrawString(name, verdanaFont, new SolidBrush(Color.Black), rect, strFormat1);
#endregion

#region 边框绘制
Pen pn = new Pen(Color.Black);
g.DrawRectangle(pn, 0, 0, this.Width - 1, this.Height - 1);
#endregion

strFormat1.Dispose();
pn1.Dispose();
pn.Dispose();
verdanaFont.Dispose();

}
#region 公有方法
public void Init(int count)
{
this.count = count;
}

public void SetColor(int index, string name)
{
this.index = index;
this.name = name;
this.Invalidate();
}
//public void save()
//{
// Dictionary<int, string> dic = new Dictionary<int, string>();
// for (int i = 0; i < 6; i++)
// {
// if (i == index)
// {
// dic.Add(index, name);
// }
// }

//}
#endregion
}
}

举例说明我要的效果
当index为1时,name为test1,color为red
当我输入index为2时,name为test2,color为black
控件中同时显示index为1和2时的情况。
2楼放下楼主写的带自定义控件和测试button的csdn内的下载资源,希望有大牛帮我解决
...全文
416 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2014-08-22
  • 打赏
  • 举报
回复
public int index = 0; public string name = ""; -> public List<int> index = new List<int> { 0 }; public List<string> name = new List<string> { "" }; public void SetColor(int index, string name) { this.index = index; this.name = name; this.Invalidate(); } -> public void SetColor(int index, string name) { this.index.Add(index); this.name.Add(name); this.Invalidate(); }
fudashouyao 2014-08-22
  • 打赏
  • 举报
回复
单一对象和数组的问题,要么每次都NEW一个新的对象,对象名修改,要么就用数组来做
Peng_baoqing 2014-08-22
  • 打赏
  • 举报
回复
public partial class UserControl1 : UserControl
    {
        private int count = 4;

        public int Count
        {
            get { return count; }
            set
            {
                count = value;
                this.Invalidate();
            }
        }

        private List<string> showText;

        public List<string> ShowText
        {
            get { return showText; }
            set { showText = value; }
        }
        private int index;

        public int Index
        {
            get { return index; }
            set { index = value; }
        }

        public UserControl1()
        {
            InitializeComponent();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;

            Color[] colors = { Color.Red, Color.Black, Color.Blue, Color.SkyBlue };

            #region 分割图形
            Pen pn1 = new Pen(Color.Black, 2);
            for (int i = 0; i < count; i++)
            {
                if (i < count)
                {
                    Point pot1 = new Point((this.Width * i) / count, 0);
                    Point pot2 = new Point((this.Width * i) / count, this.Height);
                    g.DrawLine(pn1, pot1, pot2);
                }
            }
            #endregion

            #region 背景颜色和写字填充

            for (int i = 0; i < Index; i++)
            {
                Font verdanaFont = new Font("Verdana", 10, FontStyle.Bold);
                Rectangle rect = new Rectangle(this.Width * i / count + 1, 0, this.Width / count - 1, this.Height);
                StringFormat strFormat1 = new StringFormat();
                strFormat1.Alignment = StringAlignment.Center;
                strFormat1.LineAlignment = StringAlignment.Center;
                strFormat1.Trimming = StringTrimming.EllipsisCharacter;
                g.FillRectangle(new SolidBrush(colors[i]), rect);
                g.DrawString(ShowText[i], verdanaFont, new SolidBrush(Color.Black), rect, strFormat1);
            }

            #endregion

            #region 边框绘制
            Pen pn = new Pen(Color.Black);
            g.DrawRectangle(pn, 0, 0, this.Width - 1, this.Height - 1);
            #endregion

            pn1.Dispose();
            pn.Dispose();
        }
        #region 公有方法
        public void Init(int count)
        {
            this.count = count;
        }

        public void SetColor(int index, List<string> name)
        {
            this.Index = index;
            this.ShowText = name;
            this.Invalidate();
        }

        #endregion
    }  使用         List<string> names = new List<string>();

        private void button1_Click(object sender, EventArgs e)
        {
            int number = Convert.ToInt32(textBox3.Text);
            names.Add(textBox1.Text);
            userControl11.SetColor(number, names);
        }
我只是粗略的帮你写了一下,你还需要做一些优化和判断哦!!!!!切记!!!
於黾 2014-08-22
  • 打赏
  • 举报
回复
if (index == 1) { color = Color.Red ; } if (index == 2) { color = Color.Black; } if (index == 3) { color = Color.Blue; } if (index == 4) { color = Color.SkyBlue; } 你真是不怕累,不会用switch{case}么 改成数组之后,画图的时候也是用数组的元素作为变量来画啊 既然做成数组,当然是循环数组画了
Peng_baoqing 2014-08-22
  • 打赏
  • 举报
回复
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;

            Color[] colors = { Color.Red, Color.Black, Color.Blue, Color.SkyBlue };

            #region 分割图形
            Pen pn1 = new Pen(Color.Black, 2);
            for (int i = 0; i < count; i++)
            {
                if (i < count)
                {
                    Point pot1 = new Point((this.Width * i) / count, 0);
                    Point pot2 = new Point((this.Width * i) / count, this.Height);
                    g.DrawLine(pn1, pot1, pot2);
                }
            }
            #endregion

            #region 背景颜色和写字填充

            for (int i = 0; i < index && index <= colors.Length; i++)
            {
                Font verdanaFont = new Font("Verdana", 10, FontStyle.Bold);
                Rectangle rect = new Rectangle(this.Width * i / count + 1, 0, this.Width / count - 1, this.Height);
                StringFormat strFormat1 = new StringFormat();
                strFormat1.Alignment = StringAlignment.Center;
                strFormat1.LineAlignment = StringAlignment.Center;
                strFormat1.Trimming = StringTrimming.EllipsisCharacter;
                g.FillRectangle(new SolidBrush(colors[i]), rect);
                g.DrawString(name, verdanaFont, new SolidBrush(Color.Black), rect, strFormat1);
            }

            #endregion

            #region 边框绘制
            Pen pn = new Pen(Color.Black);
            g.DrawRectangle(pn, 0, 0, this.Width - 1, this.Height - 1);
            #endregion

            pn1.Dispose();
            pn.Dispose();
        }
uppaway 2014-08-22
  • 打赏
  • 举报
回复
引用 12 楼 Z65443344 的回复:
实在玩不明白,定义成int[ ]和string[ ],给个足够大的长度,再用个int变量标识里面到底有几个有效数据
我写成数列或者数组以后,下面的画图就会出现问题。我是要重新定义两个数组去保存吗?
於黾 2014-08-22
  • 打赏
  • 举报
回复
实在玩不明白,定义成int[ ]和string[ ],给个足够大的长度,再用个int变量标识里面到底有几个有效数据
於黾 2014-08-22
  • 打赏
  • 举报
回复
int[ ]和string[ ]会用不? List<int>和List<string>使用起来没什么区别 就是可以动态往里添加数据而已,而不像数组必须初始化时给定长度
uppaway 2014-08-22
  • 打赏
  • 举报
回复
引用 7 楼 caozhy 的回复:
public int index = 0; public string name = ""; -> public List<int> index = new List<int> { 0 }; public List<string> name = new List<string> { "" }; public void SetColor(int index, string name) { this.index = index; this.name = name; this.Invalidate(); } -> public void SetColor(int index, string name) { this.index.Add(index); this.name.Add(name); this.Invalidate(); }
换成这样的,我绘图中就出现错误了,index和name在绘图中是int和string,但换成数列的话,就会出现错误吧。
uppaway 2014-08-22
  • 打赏
  • 举报
回复
引用 8 楼 Z65443344 的回复:
参考7楼 你想记住每一次输入,最起码得有个数组来存放每一次输入吧 只定义一个int来存放,一更新当然上一次的就没了
恩,我想用dictionary<>的,但是写出来错误。所以来问的。
於黾 2014-08-22
  • 打赏
  • 举报
回复
参考7楼 你想记住每一次输入,最起码得有个数组来存放每一次输入吧 只定义一个int来存放,一更新当然上一次的就没了
uppaway 2014-08-22
  • 打赏
  • 举报
回复
感谢各位,现在还差一个文字不能更新的问题,再去研究研究。
uppaway 2014-08-21
  • 打赏
  • 举报
回复
没有人吗
uppaway 2014-08-21
  • 打赏
  • 举报
回复
引用 3 楼 xiangxinzijiwonen 的回复:
LZ的需求是什么
就是,先输入index=1,然后index=2输入以后,还是可以保留index=1时的情况。我现在做成的那个,只能显示单一的情况,当输入index=2时,index=1的格子就变回背景色了。
uppaway 2014-08-21
  • 打赏
  • 举报
回复
引用 2 楼 bdmh 的回复:
你现在是什么问题,你那不是已经根据indwx判断了吗,改变属性后调用invalidate重绘
就是想index=2时还能保留前面index=1时的情况。
ZhongGuanYao 2014-08-21
  • 打赏
  • 举报
回复
LZ的需求是什么
bdmh 2014-08-21
  • 打赏
  • 举报
回复
你现在是什么问题,你那不是已经根据indwx判断了吗,改变属性后调用invalidate重绘
uppaway 2014-08-21
  • 打赏
  • 举报
回复
带了index和name的txtbox,以及button的测试代码,csdn内资源。 http://download.csdn.net/detail/uppaway/7795381

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧