我想在另一个打开时禁用我的表单,并在关闭时启用。但是另一种形式可能会关闭它自己并打开新的表单,所以我会得到一个函数,它获取布尔值并启用和禁用表单和键。当打开新的时它可以工作但是当关闭时这种形式不起作用。
这是主要形式:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Automation_v1_0_0
{
public partial class FormMain : Form
{
bool keysActivated = true;
public FormMain()
{
InitializeComponent();
}
public void anotherForm(bool anotherform)
{
if (anotherform)
{
this.Enabled = false;
keysActivated = false;
}
else
{
this.Enabled = true;
keysActivated = true;
}
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (keysActivated)
{
if (e.KeyCode == Keys.F7)
{
FormSettings settings = new FormSettings();
anotherForm(true); // Enters and working.
settings .Show();
}
}
}
}
}
这是我的设置表单:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Automation_v1_0_0
{
public partial class FormAyarlarMenu : Form
{
public FormSettings()
{
InitializeComponent();
}
private void FormSettings_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
{
FormMain fm = new FormMain();
fm.anotherForm(false); // Enters but not working.
this.Close();
}
}
}
}
答案 0 :(得分:2)
你正在使这个很复杂,而且很简单
你的主体中的执行此操作:
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F7)
{
using (FormSettings settings = new FormSettings())
{
settings.ShowDialog();
}
}
}
然后放弃你不需要的所有其他代码。
删除方法另一种形式,你不需要它。
只需执行此操作
private void FormSettings_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
{
this.Close();
}
}
答案 1 :(得分:1)
主要形式:
public partial class FormMain : Form
{
bool keysActivated = true;
public FormMain()
{
InitializeComponent();
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (keysActivated)
{
if (e.KeyCode == Keys.F7)
{
this.Hide();
FormSettings settings = new FormSettings(this);
settings.Show();
}
}
}
}
设置表格:
public partial class FormSettings : Form
{
private FormMain formMain;
public FormSettings(FormMain formMain)
{
InitializeComponent();
this.formMain = formMain;
}
private void FormSettings_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
{
this.Close();
formMain.Show();
}
}
}
您的问题是您实例化了新的FormMain
对象,而不是使用现有的对象!解决方案是您将FormMain
的当前实例传递给设置表单以使用它。
答案 2 :(得分:0)
我已经解决了问题。这是我的代码:
namespace Automation_v1_0_0
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F7)
{
FormSettings settings = new FormSettings();
settings.ShowDialog();
}
}
}
}