簡體   English   中英

在不同的窗體上的C#Winform調整大小按鈕

[英]c# winform Resizing button on different form

我想使用form1上的按鈕來調整form2的按鈕的大小。

這是我的代碼,它給我錯誤:

public void button7_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    int x = Convert.ToInt32(textBox7.Text);
    int y = Convert.ToInt32(textBox8.Text);
    f2.button8.Size = new System.Drawing.Size(x, y);
}

它在Form2.button8.Size處出現錯誤。.我將我的button8公開,因此可以對其進行訪問..但仍然給我錯誤“ Form2.button8由於其保護級別而無法訪問” ..

這是我在form2上的button8

public void button8_Click(對象發送者,EventArgs e){}

我該如何解決這個問題?

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
    Form2 f2 = new Form2();

    public Form1()
    {
        InitializeComponent();
        f2.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        f2.button1.Size = new Size(int.Parse(textBox1.Text), int.Parse(textBox2.Text));
    }
}
}

我在form2.designer.cs內部創建了button1。

namespace WindowsFormsApplication2
{
    partial class Form2
    {
        /// <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()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(95, 89);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "buttonForm2";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);

        }

        #endregion

    //This is where you change from private to internal
        internal System.Windows.Forms.Button button1;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM