我的 Form1 设计器中有一个用户控制图,这是调整它大小的代码:
private void graphChart1_MouseEnter(object sender, EventArgs e)
{
graphChart1.Size = new Size(600, 600);
}
当我将鼠标移动到控制区域时,它并没有调整它的大小使其变大,而是删除了一些其他控件。
这是我将鼠标移到控件上之前的图像:
这是我将鼠标移到控件上时的图像:
这是图表所在的用户控件的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Windows.Forms.DataVisualization.Charting;
namespace GatherLinks
{
public partial class GraphChart : UserControl
{
public GraphChart()
{
InitializeComponent();
}
private double f(int i)
{
var f1 = 59894 - (8128 * i) + (262 * i * i) - (1.6 * i * i * i);
return f1;
}
private void GraphChart_Load(object sender, EventArgs e)
{
chart1.Series.Clear();
var series1 = new System.Windows.Forms.DataVisualization.Charting.Series
{
Name = "Series1",
Color = System.Drawing.Color.Green,
IsVisibleInLegend = false,
IsXValueIndexed = true,
ChartType = SeriesChartType.Line
};
this.chart1.Series.Add(series1);
for (int i = 0; i < 100; i++)
{
series1.Points.AddXY(i, f(i));
}
chart1.Invalidate();
}
}
}
编辑:
我在用户控件类代码中这样做了:
public void ChangeChartSize(int width, int height)
{
chart1.Size = new Size(width, height);
chart1.Invalidate();
}
我必须添加chart1.Invalidate();
以使其生效,但随后它会在用户控件中自行调整图表的大小。用户控件未更改。
所以在 Form1 鼠标输入中我也改变了 graphChart1 的控件大小:
private void graphChart1_MouseEnter(object sender, EventArgs e)
{
graphChart1.ChangeChartSize(600, 600);
graphChart1.Size = new Size(600, 600);
}
问题是,当我将鼠标移到控件上时,它需要花费将近 20 秒左右的时间才能生效。如果我将删除第二行:
graphChart1.Size = new Size(600, 600);
它会工作得很快,但它只会改变控件内部的图表,但不会改变控件的大小。
也尝试过无效:
private void graphChart1_MouseEnter(object sender, EventArgs e)
{
graphChart1.ChangeChartSize(600, 600);
graphChart1.Size = new Size(600, 600);
graphChart1.Invalidate();
}
但是还是很慢。也许我还需要在用户控件类代码中而不是在 Form1 中更改控件的自身大小?