当前位置: 首页 > 知识库问答 >
问题:

不同窗体上的c# winform调整大小按钮

施茂
2023-03-14

我想使用 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 .. 处有一个错误。我把我的按钮8公开了,所以可以访问它。但它仍然给我错误“Form2.button8由于其保护级别而无法访问”。

这是我在form2的按钮8

public void button8_Click(object sender, EventArgs e) { }

我该如何解决这个问题?

共有1个答案

强宾白
2023-03-14
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 internal上做了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;
    }
}
 类似资料:
  • 我有一个窗口与列表框,调整大小与彼此。我用网格来做这个。列表框下面有一排按钮,它们当前正在随着窗口移动和调整大小。我希望按钮保持原位,并在窗口和列表框调整大小时保持一致的大小(所以按钮总是保持在列表框的左下角)。 有简单的方法吗?我今天才开始使用Tkinter,所以我可能只是在文档中错过了它,或者可能只是在网格布局上感到困惑。 按钮设置如下: 我已经尝试了所有粘性组合,但那些不调整按钮大小的组合仍

  • 问题内容: 我一直试图(徒劳地)建立一个页面,当我更改窗口大小时,其元素将重新调整大小。我可以在CSS中使用它来处理图像,但是没有问题,但是我似乎无法在文本上完成相同的操作,而且我不确定在CSS中是否有可能。而且我似乎找不到能完成此任务的jQuery脚本。 当用户调整窗口大小时,我实质上希望页面能够动态流畅地缩放,而无需用户调用页面缩放。通过css在我的img div上这可以正常工作,但对于文本则

  • 我正在尝试构建一个包含6个窗格(作为父级添加到GridPane布局中)的简单Java项目。我必须在开始时设置窗口大小,并通过参考根布局的宽度和高度,设法将它们均匀地拆分。 但我想要他们调整大小,因为我改变了窗口的大小(使用鼠标,现在他们得到固定的大小)。 下面是我的代码:

  • 问题内容: 我有以下JQuery代码: 唯一的问题是,这仅在首次加载浏览器时有效,我是否还希望在调整窗口大小时进行检查? 有任何想法吗? 问题答案: 这是一个使用jQuery,javascript和css处理调整大小事件的示例。 (如果您只是通过调整大小来设置样式(媒体查询),则最好的方法是CSS) CSS javascript jQuery 如何停止调整大小的代码执行如此频繁! 这是绑定大小调整

  • 窗口大小,我们可以非常方便的使用width、height调整,但是如何知道 width和height是一个问题? 在 Window 操作系统中,假如我们想要缩放,我们通常会把鼠标移动到窗口的右边栏,和底部边栏,以及右下边栏。 而且在不同的边栏,鼠标呈现的样式也是不一样的。当我们在右边栏的时候我们可以通过cursor: e-resize;模拟鼠标样式。 在底部边栏我们可以通过cursor: s-re

  • #include <stdio.h> void fun1(void) { int i = 0; i++; i = i * 2; printf("%d\n", i); } void fun2(void) { int j = 0; fun1(); j++; j = j