我正在使用WinForms和Console Apps学习C#。我在访问另一个表单变量时遇到问题。我没有声明新的表单对象,而是意识到要使用另一种形式的变量,我需要使用包含变量的相同表单实例。更改我的表单后,我在Program.cs文件中收到一个错误,我似乎无法修复。我对表格中的C#很新,所以如果这是noob错误,请提前抱歉。声明类的对象对我来说是新的:X
以下是表单背后的代码,以便您更好地理解:
frmOptions:
public partial class frmOptions : Form
{
frmHelp frmHelp1;
public frmOptions(frmHelp frmHelp1)
{
InitializeComponent();
}
private void tbnDisplayHelp_Click(object sender, EventArgs e)
{
//frmHelp frmHelp = new frmHelp(); //Declare a new object for the Help form so it can be accessed
frmHelp1.bAccessedFromForm = true;
this.Close();
frmHelp1.Show();
}
frmHelp:
public partial class frmHelp : Form
{
frmOptions frmOptions1;
frmLoginCreate frmLoginCreate1;
public bool bAccessedFromForm = false;
public frmHelp(frmOptions frmOptions1, frmLoginCreate frmLoginCreate)
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
//frmLoginCreate frmLoginCreate1 = new frmLoginCreate();
this.Hide();
frmLoginCreate1.Show();
}
private void frmHelp_Load(object sender, EventArgs e)
{
if (bAccessedFromForm == true)
{
btnOK.Hide();
btnReturnToForm.Show();
}
}
private void btnReturnToForm_Click(object sender, EventArgs e)
{
this.Hide();
frmOptions1.Show();
btnOK.Show();
}
}
这是我的Program.cs文件:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmHelp());
}
}
这是我在program.cs文件中遇到的错误:
没有任何论据符合所要求的形式 参数&#39; frmOptions1&#39; of&#39; frmHelp.frmHelp(frmOptions, frmLoginCreate)&#39;
答案 0 :(得分:1)
您正在尝试创建frmHelp的新实例,但不传递任何参数。
你的frmHelp类只有一个带有两个参数的构造函数:
public frmHelp(frmOptions frmOptions1, frmLoginCreate frmLoginCreate)
{
InitializeComponent();
}
简单的答案是将之前的内容更改为:
public frmHelp()
{
InitializeComponent();
}
因为你没有对这两个参数做任何事情
答案 1 :(得分:0)
这不直接回答您的问题,但可能有助于您的解决方案;
我更希望使用modal forms
而不是普通表格。
例如:(这只是一个展示如何完成的示例)
public class FormMain : Form
{
// you might 'remember' the previous login name
private string _previousUsername;
private void ButtonLogin_Click(object sender, EventArgs e)
{
// create the login form
using(var loginForm = new FormLogin())
{
// fillin the previous username
loginForm.Username = _previousUsername;
// this will block here, until the loginForm's DialogResult is set
var result = loginForm.ShowDialog();
// if the form was closed (other than pressing ok), cancel login
if(result != DialogResult.OK)
return;
// verify loginForm.Username, loginForm.Password
}
}
}
public class FormLogin : Form
{
// wrapper properties, it wraps the textbox.Text,
// you don't want to expose the textbox directly, you might want to
// use some other controls in future as editbox, which will change the contract
// for classes that are using this control/form, which causes modifying code
// on more than one place, (also affects testing again). This way you can keep it
// separated. Classes dependend on the FormLogin, shouldn't be dependend
// on how it is presented.(UI)
public string Username
{
get { return textboxUsername.Text; }
set { textboxUsername.Text = value; }
}
public string Password
{
get { return textboxPassword.Text; }
set { textboxPassword.Text = value; }
}
private void ButtonLogin_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
}
如果您想将数据传递到其他表单,可以使用DataHolder类。
类似的东西:
// dataholder class
public class MyDataHolder
{
public string UserName { get; set; }
public int MyId { get; set; }
public List<double> Whatever { get; set; }
public DateTime CreatedOn { get; set; }
}
// form1
public class Form1: Form
{
// a field with a reference to the dataholder
private MyDataHolder _myData;
public Form1()
{
// create the data
_myData = new MyDataHolder();
_myData.UserName = "Unknown";
_myData.CreatedOn = DateTime.Now;
}
private void Button1_Click(object sender, EventArgs e)
{
// create the form2 and pass the data
using(Form2 form2 = new Form2(_myData))
form2.ShowDialog();
}
}
public class Form2 : Form
{
// a field with a reference to the dataholder
private MyDataHolder _myData;
public Form2(MyDataHolder myData)
{
// don't forget to assign it to the field
_myData = myData;
}
private void Button1_Click(object sender, EventArgs e)
{
// show the username
MessageBox.Show(_myData.UserName);
}
}
现在两个表单都引用了相同的数据。这样就可以从Form2
Form1
上删除Form2
的相关性。因此,您也可以在其他表单中重复使用MyDataHolder
,只要您将有效Form2
传递给Form1
,它就会运行。使用Form2
的{{1}}和使用Form2
的{{1}}为您提供有限的可重用性,您希望重复使用已编写的代码而不是重写它; - )
我希望这会帮助你完成任务....