MNIHD 2024-08-23 01:27 采纳率: 33.3%
浏览 8

C#运行的窗体里控件不显示

我只是把性能计数器加到了上次的折线统计图控件上,就发现运行时窗体上不显示它了,这是IDE的问题还是代码的问题啊


private int[] datas = new int[40];

public int[] Datas
{
    get
    {
        return datas;
    }
    set
    {
        if (datas.Length > 1)
        {
            datas = value;
        }
        this.Invalidate();
        panel1.Invalidate();
        points = new Point[datas.Length];
    }
}

private Point[] points = new Point[10];

private int maxValue = 100;

private int minValue = 0;

public int MaxValue
{
    get
    {
        return maxValue;
    }
    set
    {
        maxValue = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

public int MinValue
{
    get
    {
        return minValue;
    }
    set
    {
        minValue = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private int numberOfHorizontalLines = 5;

public int NumberOfHorizontalLines
{
    get
    {
        return numberOfHorizontalLines;
    }
    set
    {
        numberOfHorizontalLines = Math.Abs(value);
        this.Invalidate();
        panel1.Invalidate();
    }
}

private int numberOfVerticalLines = 5;

public int NumberOfVerticalLines
{
    get
    {
        return numberOfVerticalLines;
    }
    set
    {
        numberOfVerticalLines = Math.Abs(value);
        this.Invalidate();
        panel1.Invalidate();
    }
}

private Color backLineColor = Color.DarkGray;

private Color lineColor = Color.DodgerBlue;

public Color BackLineColor
{
    get
    {
        return backLineColor;
    }
    set
    {
        backLineColor = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

public Color LineColor
{
    get
    {
        return lineColor;
    }
    set
    {
        lineColor = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private bool isUsingSizeControl = true;

public bool IsUsingSizeControl
{
    get
    {
        return isUsingSizeControl;
    }
    set
    {
        isUsingSizeControl = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private byte fillColorA = 100;

public byte FillColorA
{
    get
    {
        return fillColorA;
    }
    set
    {
        fillColorA = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private DashStyle lineStyle = DashStyle.Solid;

public DashStyle LineStyle
{
    get
    {
        return lineStyle;
    }
    set
    {
        lineStyle = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private SmoothingMode smoothingMode = SmoothingMode.HighQuality;

public SmoothingMode SmoothingMode
{
    get
    {
        return smoothingMode;
    }
    set
    {
        smoothingMode = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private string categoryname = "Processor Information";

public string CategoryName
{
    get
    {
        return categoryname;
    }
    set
    {
        categoryname = value;
        performanceCounter1.CategoryName = categoryname;
    }
}

private string instancename = "_Total";

public string InstanceName
{
    get
    {
        return instancename;
    }
    set
    {
        instancename = value;
        performanceCounter1.InstanceName = instancename;
    }
}

private string countername = "% Processor Utility";

public string CounterName
{
    get
    {
        return countername;
    }
    set
    {
        countername = value;
        performanceCounter1.CounterName = countername;
    }
}

private void panel1_Paint(object sender, PaintEventArgs e)
{
    Datas[Datas.Length - 1] = (int)performanceCounter1.NextValue();
    for (int i = 0; i < datas.Length - 1; i++)
    {
        Datas[i] = Datas[i + 1];
    }
    for (int i = 0; i < datas.Length; i++)
    {
        points[i] = new Point(panel1.Width / (datas.Length - 1) * i, panel1.Height - (panel1.Height / (maxValue - minValue - 1) * (datas[i] - minValue)));
    }
    Color realfillcolor = Color.FromArgb(fillColorA, lineColor.R, lineColor.G, lineColor.B);
    Graphics graphics = e.Graphics;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.SmoothingMode = smoothingMode;
    Pen pen = new Pen(backLineColor, 1);
    Pen linepen = new Pen(lineColor, 2);
    linepen.DashStyle = lineStyle;
    Brush brush = new SolidBrush(realfillcolor);
    graphics.DrawLine(pen, 0, 0, panel1.Width - 1, 0);
    graphics.DrawLine(pen, 0, panel1.Height - 1, panel1.Width - 1, panel1.Height - 1);
    graphics.DrawLine(pen, panel1.Width - 1, panel1.Height - 1, panel1.Width - 1, 0);
    graphics.DrawLine(pen, 0, 0, 0, panel1.Height - 1);
    for (int i = 1; i <= numberOfHorizontalLines; i++)
    {
        graphics.DrawLine(pen, 0, (panel1.Height - 1) / (numberOfHorizontalLines + 1) * i, panel1.Width - 1, (panel1.Height - 1) / (numberOfHorizontalLines + 1) * i);
    }
    for (int i = 1; i <= numberOfVerticalLines; i++)
    {
        graphics.DrawLine(pen, (panel1.Width - 1) / (numberOfVerticalLines + 1) * i, 0, (panel1.Width - 1) / (numberOfVerticalLines + 1) * i, panel1.Height - 1);
    }
    for (int i = 0; i < datas.Length - 1; i++)
    {
        graphics.DrawLine(linepen, points[i], points[i + 1]);
    }
    if (fillColorA != 0)
    {
        Point[] polygonpoints = new Point[points.Length + 2];
        polygonpoints.SetValue(new Point(0, panel1.Height - 1), 0);
        points.CopyTo(polygonpoints, 1);
        polygonpoints.SetValue(new Point(panel1.Width - 1, panel1.Height - 1), polygonpoints.Length - 1);
        graphics.FillPolygon(brush, polygonpoints);
    }
    if (isUsingSizeControl == true)
    {
        this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
        this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
    }
}

private void LineChart_Load(object sender, EventArgs e)
{
    points = new Point[datas.Length];
    if (isUsingSizeControl == true)
    {
        this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
        this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
    }
}

private void LineChart_Resize(object sender, EventArgs e)
{
    if (isUsingSizeControl == true)
    {
        this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
        this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
    }
    panel1.Width = this.Width - 6;
    panel1.Height = this.Height - 6;
}
  • 写回答

3条回答 默认 最新

  • 吃不了席 2024-08-23 01:28
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    从代码来看,没有明显的错误。但是,你可以尝试以下方法来解决问题:

    1. 确保你的窗体控件(如panel1)已经添加到窗体中,并且它们的Visible属性设置为true
    2. 检查你的代码中是否有其他地方修改了控件的可见性或大小。
    3. 尝试在窗体的构造函数中调用LineChart_Load方法,以确保在窗体加载时正确初始化控件。
    4. 如果仍然无法显示控件,请尝试在窗体的Load事件处理程序中添加一些调试输出,以检查是否正确执行了相关代码。例如:
    private void LineChart_Load(object sender, EventArgs e)
    {
        Console.WriteLine("LineChart_Load called");
        points = new Point[datas.Length];
        if (isUsingSizeControl == true)
        {
            this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
            this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
        }
    }
    

    然后在运行程序时查看控制台输出,以确认LineChart_Load是否被调用。

    评论

报告相同问题?

问题事件

  • 创建了问题 8月23日

悬赏问题

  • ¥50 求恶意流量检测系统搭建教程( C++ python C)
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
  • ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
  • ¥15 io.jsonwebtoken.security.Keys