我正在尝试调整窗体窗口的大小,我发现的所有MSDN页面都告诉我只需使用窗体。宽度或形状高度等。但是Visual studio并没有在IntelliSense弹出窗口中显示这个选项。
如果我使用这个,它会弹出。宽度,但如果我想单击form1中的按钮来调整form2的大小呢?如何通过表单名称更改属性?
我错过了什么?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1.Width = 800; //ERROR: does not contain a definition for width
this.Width = 800; //this is allowed though
Form2.Width = 800; //ERROR: does not contain a definition for width
}
private void Form1_Load(object sender, EventArgs e)
{
//thought mayeb I need a size 1st so tried this
Form1.Size = new System.Drawing.Size.(1029, 640); //ERROR: An object reference is required for ... 'System.Windows.Forms.Form.Size.get'
//dunno why this doesnt work its apparently correct acording to MSDN
//https://msdn.microsoft.com/en-us/library/aa984427(v=vs.71).aspx
//https://msdn.microsoft.com/en-us/library/ms229606(v=vs.110).aspx
}
}
}
我没有做过任何Visual studio UI的东西,因为我玩了VB6很长一段时间以前,我敢肯定,它曾经有可能访问任何形式的名称和操纵它。
更新:我根据评论将width
更改为Width
,问题仍然存在,因此大小写敏感性似乎不是问题。
更新:我现在知道怎么回事了。所以我真正需要知道的是我如何引用form1的实例,已经在启动时从一个单独的表单内执行?
所以我实例化form2并显示它。现在,如果我想在form2上有一个按钮来操纵form1,我怎么引用它,当我从来没有实例化到一个变量?(因为form1在程序启动时自动运行)
尝试以下操作,您将看到它可以工作。
private void button1_Click(object sender, EventArgs e)
{
this.Width = 800; //this is in reference to Form1
var frm2 = new Form2();
frm2.Width = 800;
frm2.ShowDialog();
frm2.Dispose();
}
c#是区分大小写的;width
应为Width
。此外,正如您在代码注释中包含的后一个错误所示,那些(Form1和可能的Form2)是class
标识符,而不是实例。您只能通过类名引用类的static
元素。
正如ueerdo所说,width
应该是Width
。此外,(再次如他所说)Form1
和Form2
是类名,您不能使用它们来影响c#中窗体的值。相反,您需要在当前实例上影响这些值,这些值在c#
this
表示。 this.Size = new System.Drawing.Size.(1029, 640)
如果我没记错的话,在VB中,你可以像现在这样通过类名修改表单,但在c#