我使用以下代码调用我的winforms:
try
{
if (Application.OpenForms.OfType<frmBackupManager>().Count() > 0)
{
if (Application.OpenForms["frmBackupManager"].WindowState == FormWindowState.Minimized)
{
Application.OpenForms["frmBackupManager"].WindowState = FormWindowState.Normal;
Application.OpenForms["frmBackupManager"].BringToFront();
}
else
{
Application.OpenForms["frmBackupManager"].BringToFront();
}
}
else
{
// Show Backup Manager
frmBackupManager myBackupManager;
myBackupManager = new frmBackupManager();
myBackupManager.StartPosition = FormStartPosition.Manual;
myBackupManager.Location = new Point(this.Location.X + this.Width / 2 - myBackupManager.Width / 2, this.Location.Y + this.Height / 2 - myBackupManager.Height / 2);
myBackupManager.Show();
}
}
catch (Exception ex)
{
MessageBoxEx.Show("Unable to perform requested action: " + ex.Message.ToString(), "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
我想从一个方法或类中调用它,但是我一直收到错误,找不到类型或命名空间名称'FormName'(你是否缺少using指令或程序集引用?) 在每个使用 FormName 的地方。
private void btnBackupManager_Click(object sender, EventArgs e)
{
// Launch Backup Manager Form
LaunchForm("frmBackupManager", "MyBackupManager");
}
private void LaunchForm(string FormName, string MyForm)
{
try
{
if (Application.OpenForms.OfType<FormName>().Count() > 0)
{
if (Application.OpenForms[FormName].WindowState == FormWindowState.Minimized)
{
Application.OpenForms[FormName].WindowState = FormWindowState.Normal;
Application.OpenForms[FormName].BringToFront();
}
else
{
Application.OpenForms[FormName].BringToFront();
}
}
else
{
// Show Backup Manager
FormName myFormName;
myFormName = new FormName();
myFormName.StartPosition = FormStartPosition.Manual;
myFormName.Location = new Point(this.Location.X + this.Width / 2 - myFormName.Width / 2, this.Location.Y + this.Height / 2 - myFormName.Height / 2);
myFormName.Show();
}
}
catch (Exception ex)
{
MessageBoxEx.Show("Unable to perform requested action: " + ex.Message.ToString(), "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
答案 0 :(得分:3)
与其他海报提到的一样,您尝试将FormName变量用作类型。
尝试这样的事情:
private static void LaunchForm<T>() where T : Form, new()
{
var existing = Application.OpenForms.OfType<T>().FirstOrDefault();
if(existing != null) {
// Make sure existing is visible.
} else {
var newForm = new T();
// Initialize it here.
newForm.Show();
}
}
答案 1 :(得分:0)
您的问题是您使用变量作为类型。例如,这一行:
if (Application.OpenForms.OfType<FormName>().Count() > 0)
FormName
可能确实包含类型的名称,但它本身不是类型。
此问题的其他行包括:
FormName myFormName;
myFormName = new FormName();
您可以使用反射解决此问题:
if(Application.OpenForms.Where(form => form.GetType().Name == FormName).Count() > 0)
Form myFormName;
myFormName = (Form)Assembly.GetCallingAssembly().
GetType("YourNamespace." + FormName).
GetConstructor(new Type[] {}).
Invoke(new object[] {});
答案 2 :(得分:0)
例如: -
private void LaunchForm(Form formToOpen);
这样您就可以在需要时使用对象引用
所以所需功能的实现可以如下:
private void LaunchForm(Form formToOpen)
{
try
{
if (formToOpen != null)
{
if (Application.OpenForms.OfType<Form>().Count() > 0)
{
if (Application.OpenForms[formToOpen.Name].WindowState == FormWindowState.Minimized)
{
Application.OpenForms[formToOpen.Name].WindowState = FormWindowState.Normal;
Application.OpenForms[formToOpen.Name].BringToFront();
}
else
{
Application.OpenForms[formToOpen.Name].BringToFront();
}
}
else
{
Form formToLaunch;
formToLaunch = formToOpen;
formToLaunch.StartPosition = FormStartPosition.Manual;
formToLaunch.Location = new Point(this.Location.X + this.Width / 2 - formToLaunch.Width / 2, this.Location.Y + this.Height / 2 - formToLaunch.Height / 2);
formToLaunch.Show();
}
}
}
catch (Exception ex)
{
MessageBoxEx.Show("Unable to perform requested action: " + ex.Message.ToString(), "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
答案 3 :(得分:0)
您的问题是您使用类型(类名)作为变量。
错误是局部变量名和类名相同。 解决问题的最简单方法是将本地变量名称重命名为other。
这里,FormType是一个类名,用作类型,但这是类名的错误用法:
if (Application.OpenForms.OfType<FormName>().Count() > 0)
{
if (Application.OpenForms[FormName].WindowState == FormWindowState.Minimized)
{
Application.OpenForms[FormName].WindowState = FormWindowState.Normal;
Application.OpenForms[FormName].BringToFront();
}
else
{
Application.OpenForms[FormName].BringToFront();
}
}
切换到:
if (Application.OpenForms.OfType<FormName>().Count() > 0)
{
if (Application.OpenForms[childForm].WindowState == FormWindowState.Minimized)
{
Application.OpenForms[childForm].WindowState = FormWindowState.Normal;
Application.OpenForms[childForm].BringToFront();
}
else
{
Application.OpenForms[childForm].BringToFront();
}
}
其中 childForm 是方法的参数:
private void LaunchForm(string childForm, string MyForm)
{
...
}