How to resize everything in a different class with c#?

问题描述 投票:0回答:0

这是我的 Windows 窗体应用程序代码,

Form1.cs:

``using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CreateNewTab();
        }

        private void CreateNewTab()
        {
            recipeTab tab = new recipeTab();
            TabPage newTab = new TabPage();
            tab.tabSplit = new SplitContainer();
            tab.image = new PictureBox();
            tab.title = new TextBox();
            tab.save = new Button();
            tab.rightSplit = new SplitContainer();
            tab.ingredientsText = new RichTextBox();
            tab.label3 = new Label();
            tab.instructionsText = new RichTextBox();
            tab.label4 = new Label();
            newTab.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)tab.tabSplit).BeginInit();
            tab.tabSplit.Panel1.SuspendLayout();
            tab.tabSplit.Panel2.SuspendLayout();
            tab.tabSplit.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)tab.image).BeginInit();
            ((System.ComponentModel.ISupportInitialize)tab.rightSplit).BeginInit();
            tab.rightSplit.Panel1.SuspendLayout();
            tab.rightSplit.Panel2.SuspendLayout();
            tab.rightSplit.SuspendLayout();

            newTab.Controls.Add(tab.tabSplit);
            newTab.Location = new Point(4, 24);
            newTab.Name = "newTab";
            newTab.Padding = new Padding(3);
            newTab.Size = new Size(1166, 726);
            //newTab.TabIndex = 1;
            //newTab.Text = "newTab";
            newTab.UseVisualStyleBackColor = true;
            // 
            // tabSplit
            // 
            tab.tabSplit.Dock = DockStyle.Fill;
            tab.tabSplit.Location = new Point(3, 3);
            tab.tabSplit.Name = "tabSplit";
            tab.tabSplit.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
            // 
            // tabSplit.Panel1
            // 
            tab.tabSplit.Panel1.Controls.Add(tab.image);
            tab.tabSplit.Panel1.Controls.Add(tab.title);
            // 
            // tabSplit.Panel2
            // 
            tab.tabSplit.Panel2.Controls.Add(tab.save);
            tab.tabSplit.Panel2.Controls.Add(tab.rightSplit);
            tab.tabSplit.Size = new Size(1160, 720);
            tab.tabSplit.SplitterDistance = 561;
            tab.tabSplit.TabIndex = 0;
            // 
            // image
            // 
            tab.image.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            tab.image.Location = new Point(20, 108);
            tab.image.Name = "image";
            tab.image.Size = new Size(516, 609);
            tab.image.TabIndex = 3;
            tab.image.TabStop = false;
            // 
            // title
            // 
            tab.title.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            tab.title.Font = new Font("Segoe UI Variable Display", 48F, FontStyle.Regular, GraphicsUnit.Point);
            tab.title.Location = new Point(20, 9);
            tab.title.Name = "title";
            tab.title.Size = new Size(520, 93);
            tab.title.TabIndex = 2;
            // 
            // save
            // 
            tab.save.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            tab.save.Font = new Font("Segoe UI", 20F, FontStyle.Regular, GraphicsUnit.Point);
            tab.save.Location = new Point(468, 668);
            tab.save.Name = "save";
            tab.save.Size = new Size(112, 47);
            tab.save.TabIndex = 13;
            tab.save.Text = "Save";
            tab.save.UseVisualStyleBackColor = true;
            // 
            // rightSplit
            // 
            tab.rightSplit.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            tab.rightSplit.Location = new Point(3, 3);
            tab.rightSplit.Name = "rightSplit";
            tab.rightSplit.Orientation = Orientation.Horizontal;
            // 
            // rightSplit.Panel1
            // 
            tab.rightSplit.Panel1.Controls.Add(tab.ingredientsText);
            tab.rightSplit.Panel1.Controls.Add(label3);
            // 
            // rightSplit.Panel2
            // 
            tab.rightSplit.Panel2.Controls.Add(tab.instructionsText);
            tab.rightSplit.Panel2.Controls.Add(label4);
            tab.rightSplit.Size = new Size(589, 659);
            tab.rightSplit.SplitterDistance = 322;
            tab.rightSplit.TabIndex = 12;
            // 
            // ingredientsText
            // 
            tab.ingredientsText.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            tab.ingredientsText.Location = new Point(3, 82);
            tab.ingredientsText.Name = "ingredientsText";
            tab.ingredientsText.Size = new Size(574, 222);
            tab.ingredientsText.TabIndex = 6;
            tab.ingredientsText.Text = "";
            // 
            // label3
            // 
            tab.label3.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            tab.label3.AutoSize = true;
            tab.label3.Font = new Font("Segoe UI Variable Display", 39.75F, FontStyle.Regular, GraphicsUnit.Point);
            tab.label3.Location = new Point(3, 6);
            tab.label3.Name = "label3";
            tab.label3.Size = new Size(305, 70);
            tab.label3.TabIndex = 5;
            tab.label3.Text = "Ingredients:";
            // 
            // instructionsText
            // 
            tab.instructionsText.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            tab.instructionsText.Location = new Point(3, 93);
            tab.instructionsText.Name = "instructionsText";
            tab.instructionsText.Size = new Size(569, 237);
            tab.instructionsText.TabIndex = 7;
            tab.instructionsText.Text = "";
            // 
            // label4
            // 
            tab.label4.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            tab.label4.AutoSize = true;
            tab.label4.Font = new Font("Segoe UI Variable Display", 39.75F, FontStyle.Regular, GraphicsUnit.Point);
            tab.label4.Location = new Point(3, 11);
            tab.label4.Name = "label4";
            tab.label4.Size = new Size(315, 70);
            tab.label4.TabIndex = 6;
            tab.label4.Text = "Instructions:";

            tabControl1.TabPages.Add(newTab);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void splitContainer3_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer3_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}`

` recipeTab.cs

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    internal class recipeTab
    {
        public TabPage newTab { get; set; }
        public SplitContainer tabSplit { get; set; }
        public PictureBox image { get; set; }
        public TextBox title { get; set; }
        public Button save { get; set; }
        public SplitContainer rightSplit { get; set; }
        public RichTextBox ingredientsText { get; set; }
        public Label label3 { get; set; }
        public RichTextBox instructionsText { get; set; }
        public Label label4 { get; set; }
    }
}


design.cs


namespace WinFormsApp1
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            tabControl1 = new TabControl();
            tabPage2 = new TabPage();
            splitContainer2 = new SplitContainer();
            pictureBox2 = new PictureBox();
            textBox2 = new TextBox();
            button3 = new Button();
            splitContainer3 = new SplitContainer();
            richTextBox3 = new RichTextBox();
            label3 = new Label();
            richTextBox4 = new RichTextBox();
            label4 = new Label();
            button1 = new Button();
            tabControl1.SuspendLayout();
            tabPage2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)splitContainer2).BeginInit();
            splitContainer2.Panel1.SuspendLayout();
            splitContainer2.Panel2.SuspendLayout();
            splitContainer2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)pictureBox2).BeginInit();
            ((System.ComponentModel.ISupportInitialize)splitContainer3).BeginInit();
            splitContainer3.Panel1.SuspendLayout();
            splitContainer3.Panel2.SuspendLayout();
            splitContainer3.SuspendLayout();
            SuspendLayout();
            // 
            // tabControl1
            // 
            tabControl1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            tabControl1.Controls.Add(tabPage2);
            tabControl1.Location = new Point(8, 12);
            tabControl1.Name = "tabControl1";
            tabControl1.SelectedIndex = 0;
            tabControl1.Size = new Size(1174, 754);
            tabControl1.TabIndex = 0;
            // 
            // tabPage2
            // 
            tabPage2.Controls.Add(splitContainer2);
            tabPage2.Location = new Point(4, 24);
            tabPage2.Name = "tabPage2";
            tabPage2.Padding = new Padding(3);
            tabPage2.Size = new Size(1166, 726);
            tabPage2.TabIndex = 1;
            tabPage2.Text = "tabPage2";
            tabPage2.UseVisualStyleBackColor = true;
            // 
            // splitContainer2
            // 
            splitContainer2.Dock = DockStyle.Fill;
            splitContainer2.Location = new Point(3, 3);
            splitContainer2.Name = "splitContainer2";
            // 
            // splitContainer2.Panel1
            // 
            splitContainer2.Panel1.Controls.Add(pictureBox2);
            splitContainer2.Panel1.Controls.Add(textBox2);
            // 
            // splitContainer2.Panel2
            // 
            splitContainer2.Panel2.Controls.Add(button3);
            splitContainer2.Panel2.Controls.Add(splitContainer3);
            splitContainer2.Size = new Size(1160, 720);
            splitContainer2.SplitterDistance = 561;
            splitContainer2.TabIndex = 0;
            // 
            // pictureBox2
            // 
            pictureBox2.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            pictureBox2.Location = new Point(20, 108);
            pictureBox2.Name = "pictureBox2";
            pictureBox2.Size = new Size(516, 609);
            pictureBox2.TabIndex = 3;
            pictureBox2.TabStop = false;
            // 
            // textBox2
            // 
            textBox2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            textBox2.Font = new Font("Segoe UI Variable Display", 48F, FontStyle.Regular, GraphicsUnit.Point);
            textBox2.Location = new Point(20, 9);
            textBox2.Name = "textBox2";
            textBox2.Size = new Size(520, 93);
            textBox2.TabIndex = 2;
            // 
            // button3
            // 
            button3.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            button3.Font = new Font("Segoe UI", 20F, FontStyle.Regular, GraphicsUnit.Point);
            button3.Location = new Point(468, 668);
            button3.Name = "button3";
            button3.Size = new Size(112, 47);
            button3.TabIndex = 13;
            button3.Text = "Save";
            button3.UseVisualStyleBackColor = true;
            // 
            // splitContainer3
            // 
            splitContainer3.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            splitContainer3.Location = new Point(3, 3);
            splitContainer3.Name = "splitContainer3";
            splitContainer3.Orientation = Orientation.Horizontal;
            // 
            // splitContainer3.Panel1
            // 
            splitContainer3.Panel1.Controls.Add(richTextBox3);
            splitContainer3.Panel1.Controls.Add(label3);
            splitContainer3.Panel1.Paint += splitContainer3_Panel1_Paint;
            // 
            // splitContainer3.Panel2
            // 
            splitContainer3.Panel2.Controls.Add(richTextBox4);
            splitContainer3.Panel2.Controls.Add(label4);
            splitContainer3.Panel2.Paint += splitContainer3_Panel2_Paint;
            splitContainer3.Size = new Size(589, 659);
            splitContainer3.SplitterDistance = 322;
            splitContainer3.TabIndex = 12;
            // 
            // richTextBox3
            // 
            richTextBox3.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            richTextBox3.Location = new Point(3, 82);
            richTextBox3.Name = "richTextBox3";
            richTextBox3.Size = new Size(574, 222);
            richTextBox3.TabIndex = 6;
            richTextBox3.Text = "";
            // 
            // label3
            // 
            label3.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            label3.AutoSize = true;
            label3.Font = new Font("Segoe UI Variable Display", 39.75F, FontStyle.Regular, GraphicsUnit.Point);
            label3.Location = new Point(3, 6);
            label3.Name = "label3";
            label3.Size = new Size(305, 70);
            label3.TabIndex = 5;
            label3.Text = "Ingredients:";
            // 
            // richTextBox4
            // 
            richTextBox4.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            richTextBox4.Location = new Point(3, 93);
            richTextBox4.Name = "richTextBox4";
            richTextBox4.Size = new Size(569, 237);
            richTextBox4.TabIndex = 7;
            richTextBox4.Text = "";
            // 
            // label4
            // 
            label4.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            label4.AutoSize = true;
            label4.Font = new Font("Segoe UI Variable Display", 39.75F, FontStyle.Regular, GraphicsUnit.Point);
            label4.Location = new Point(3, 11);
            label4.Name = "label4";
            label4.Size = new Size(315, 70);
            label4.TabIndex = 6;
            label4.Text = "Instructions:";
            // 
            // button1
            // 
            button1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            button1.Location = new Point(1152, 6);
            button1.Name = "button1";
            button1.Size = new Size(26, 23);
            button1.TabIndex = 1;
            button1.Text = "+";
            button1.UseVisualStyleBackColor = true;
            button1.Click += button1_Click;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 15F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(1194, 775);
            Controls.Add(button1);
            Controls.Add(tabControl1);
            Name = "Form1";
            Text = "Form1";
            Load += Form1_Load;
            tabControl1.ResumeLayout(false);
            tabPage2.ResumeLayout(false);
            splitContainer2.Panel1.ResumeLayout(false);
            splitContainer2.Panel1.PerformLayout();
            splitContainer2.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)splitContainer2).EndInit();
            splitContainer2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)pictureBox2).EndInit();
            splitContainer3.Panel1.ResumeLayout(false);
            splitContainer3.Panel1.PerformLayout();
            splitContainer3.Panel2.ResumeLayout(false);
            splitContainer3.Panel2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)splitContainer3).EndInit();
            splitContainer3.ResumeLayout(false);
            ResumeLayout(false);
        }

        #endregion

        private TabControl tabControl1;
        private Button button1;
        private SplitContainer splitContainer1;
        private TabPage tabPage2;
        private SplitContainer splitContainer2;
        private TextBox textBox2;
        private SplitContainer splitContainer3;
        private PictureBox pictureBox2;
        private RichTextBox richTextBox3;
        private Label label3;
        private Button button3;
        private RichTextBox richTextBox4;
        private Label label4;
    }
}

我在 CreateNewTab 函数中创建了该类的一个新实例,我最终想要多个页面,问题是当页面调整大小时,newtab 的内容不会调整大小。 我无法做到这一点,以便在创建新标签时,它会随着页面大小的调整而调整大小。 我该如何解决这个问题?

我是 c# 和 Windows 窗体的新手,所以请详细解释需要发生什么。

c# class windows-forms-designer
© www.soinside.com 2019 - 2024. All rights reserved.