窗体的 C# 大小应取决于移交的变量

本文关键字:变量 取决于 窗体 | 更新日期: 2023-09-27 17:56:14

我有两种形式("Startfenster"和form1)。第一个 Startfenster 打开,我可以编写两个变量("Rohrdurchmesser"和"Messlanzen"),我提供 Form1。表单 1 有 2 个图片框和 5 个按钮。图片框的大小应该取决于变量"Rohrdurchmesser",它有效,但第二种形式(form1)的大小和最小大小也应该取决于"Rohrdurchmesser",这是行不通的......这是我代码的一部分...

public partial class Form1 : Form
                {       
                    Startfenster fh;
                    int Rohrdurchmesser, Messlanzen;
                    ...
                    Bitmap bmp;
                    Graphics z;

                    public Form1(Startfenster aufrufer) // Startfenster was the first Form, which opens first when you start the program
                    {
                        fh = aufrufer;
                        InitializeComponent();
                        Anordnen();
                        bmp = new Bitmap(pic1.ClientSize.Width, pic1.ClientSize.Height);
                        z = Graphics.FromImage(bmp);          
                        Startfenster.ActiveForm.Hide();       
                    }
                    private void Form1_Load(object sender, EventArgs e)
                    {            
                        Rohrdurchmesser = Convert.ToInt32(fh.Controls["txbRohrdurchmesser"].Text);
                        Messlanzen = Convert.ToInt32(fh.Controls["txbMesslanzen"].Text);

                        pic1.Height = Convert.ToInt32(PixelToCm(Rohrdurchmesser)) * Rohrdurchmesser;
                        pic1.Width = pic1.Height;
                        pic2.Height = Convert.ToInt32(PixelToCm(Rohrdurchmesser)) * Rohrdurchmesser;
                        pic2.Width = pic2.Height;
                        Form1.ActiveForm.Size = new Size(2 * pic1.Width + 40, pic1.Height + 60); // doesn't work! why? 
                        Form1.ActiveForm.MinimumSize = new Size(2 * pic1.Width + 40, pic1.Height + 60);// doesn't work! why? 
                    }
          private void Anordnen()
                {
                    pic1.Width = this.ClientSize.Width / 2 - 30;
                    pic2.Width = this.ClientSize.Width / 2 - 30;
                    pic2.Left = pic1.Right + 20;
                    btnVergleichen.Left = pic1.Right + 10 - btnVergleichen.Width / 2;
                    btnEinlesen1.Left = pic1.Left + pic1.Width / 2 - btnEinlesen1.Width / 2;
                    btnBewerten1.Left = pic1.Left + pic1.Width / 2 - btnBewerten1.Width / 2;
                    btnEinlesen2.Left = pic2.Left + pic2.Width / 2 - btnEinlesen2.Width / 2;
                    btnBewerten2.Left = pic2.Left + pic2.Width / 2 - btnBewerten2.Width / 2;          
                }
      double PixelToCm(double Pixel)
            {
                double cm = -1;
                using (Graphics g = this.CreateGraphics())
                {
                    cm = (Pixel / g.DpiY) * 2.5399999d;
                }
                return (double)cm;
            }

窗体的 C# 大小应取决于移交的变量

"不起作用"是什么意思?

  Form1.ActiveForm.Size = new Size(2 * pic1.Width + 40, pic1.Height + 60); // doesn't work! why? 
  Form1.ActiveForm.MinimumSize = new Size(2 * pic1.Width + 40, pic1.Height + 60);// doesn't work! why? 

这不应该是

  this..ActiveForm.Size = new Size(2 * pic1.Width + 40, pic1.Height + 60);  
  this.ActiveForm.MinimumSize = new Size(2 * pic1.Width + 40, pic1.Height + 60);

还是一些真实的形式引用,而不是类名?