C# 为什么不显示MessageBox?
我试图写这个程序来告诉用户,他输入的名字是否在最流行的名字列表中,是女孩还是男孩。我遇到的问题是,我的按钮单击不会显示我已编码的消息框。我已经在这个问题上纠缠了一段时间,我似乎无法把它表现出来C# 为什么不显示MessageBox?,c#,messagebox,C#,Messagebox,我试图写这个程序来告诉用户,他输入的名字是否在最流行的名字列表中,是女孩还是男孩。我遇到的问题是,我的按钮单击不会显示我已编码的消息框。我已经在这个问题上纠缠了一段时间,我似乎无法把它表现出来 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void LoadNames(object sender, EventArgs e
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void LoadNames(object sender, EventArgs e)
{
const int size = 200;
string[] names = new string[size];
string[] names1 = new string[size];
int index = 0;
int index1 = 0;
StreamReader inputfile1;
StreamReader inputfile2;
inputfile1 = File.OpenText(@"F:\C# HW\CH07_HW_07\BoysNames.txt");
inputfile2 = File.OpenText(@"F:\C# HW\CH07_HW_07\GirlsNames.txt");
while (!inputfile1.EndOfStream && index < names.Length)
{
names[index] = inputfile1.ReadLine();
index++;
}
while (!inputfile2.EndOfStream && index1 < names1.Length)
{
names1[index1] = inputfile2.ReadLine();
index1++;
}
}
private Boolean FindBoyname()
{
const int size = 200;
string[] BoyNames = new string[size];
int index = 0;
string boyname = textBox1.Text;
Boolean Boyname = false;
StreamReader inputfile1;
inputfile1 = File.OpenText(@"F:\C# HW\CH07_HW_07\BoysNames.txt");
while (!inputfile1.EndOfStream && index < BoyNames.Length)
{
BoyNames[index] = inputfile1.ReadLine();
if (String.Equals(boyname, BoyNames[index], StringComparison.OrdinalIgnoreCase) == true)
{
Boyname = true;
}
index++;
}
return Boyname;
}
private Boolean FindGirlname()
{
const int size = 200;
string[] GirlNames = new string[size];
int index = 0;
string girlname = textBox2.Text;
Boolean Girlname = false;
StreamReader inputfile1;
inputfile1 = File.OpenText(@"F:\C# HW\CH07_HW_07\GirlsNames.txt");
while (!inputfile1.EndOfStream && index < GirlNames.Length)
{
GirlNames[index] = inputfile1.ReadLine();
if (String.Equals(girlname, GirlNames[index], StringComparison.OrdinalIgnoreCase) == true)
{
Girlname = true;
}
index++;
}
return Girlname;
}
private void button1_Click(object sender, EventArgs e)
{
Boolean boy;
Boolean girl;
boy = FindBoyname();
girl = FindGirlname();
if (boy.Equals(true))
{
MessageBox.Show(textBox1.Text + " is among the most popular boy names!");
}
if (boy.Equals(false))
{
MessageBox.Show(textBox1.Text + " is not among the most popular boy names.");
}
if (girl.Equals(true))
{
MessageBox.Show(textBox2.Text + " is among the most popular girl names!");
}
if (girl.Equals(false))
{
MessageBox.Show(textBox2.Text + " is not among the most popular girl names.");
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Boolean boy;
Boolean girl;
if (boy.Equals(true))
{
MessageBox.Show(textBox1.Text + " is among the most popular boy names!");
}
if (boy.Equals(false))
{
MessageBox.Show(textBox1.Text + " is not among the most popular boy names.");
}
if (girl.Equals(true))
{
MessageBox.Show(textBox2.Text + " is among the most popular girl names!");
}
if (girl.Equals(false))
{
MessageBox.Show(textBox2.Text + " is not among the most popular girl names.");
}
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有void加载名(对象发送方、事件参数e)
{
常数int size=200;
字符串[]名称=新字符串[大小];
字符串[]名称1=新字符串[大小];
int指数=0;
int index1=0;
StreamReader输入文件1;
StreamReader输入文件2;
inputfile1=File.OpenText(@“F:\C#HW\CH07_HW_07\BoysNames.txt”);
inputfile2=File.OpenText(@“F:\C#HW\CH07_HW_07\GirlsNames.txt”);
而(!inputfile1.EndOfStream&&index
最有可能的是按钮1.Click
事件未使用此无效按钮1\u Click
事件处理程序订阅。如果删除了与显示消息框无关的所有代码,请在“设计代码”或“可视化设计器”中选中它:
我试图写这个程序来告诉用户,他输入的名字是否在最流行的名字列表中,是女孩还是男孩。我遇到的问题是,我的按钮单击不会显示我已编码的消息框。我已经在这个问题上纠缠了一段时间,我似乎无法把它表现出来
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void LoadNames(object sender, EventArgs e)
{
const int size = 200;
string[] names = new string[size];
string[] names1 = new string[size];
int index = 0;
int index1 = 0;
StreamReader inputfile1;
StreamReader inputfile2;
inputfile1 = File.OpenText(@"F:\C# HW\CH07_HW_07\BoysNames.txt");
inputfile2 = File.OpenText(@"F:\C# HW\CH07_HW_07\GirlsNames.txt");
while (!inputfile1.EndOfStream && index < names.Length)
{
names[index] = inputfile1.ReadLine();
index++;
}
while (!inputfile2.EndOfStream && index1 < names1.Length)
{
names1[index1] = inputfile2.ReadLine();
index1++;
}
}
private Boolean FindBoyname()
{
const int size = 200;
string[] BoyNames = new string[size];
int index = 0;
string boyname = textBox1.Text;
Boolean Boyname = false;
StreamReader inputfile1;
inputfile1 = File.OpenText(@"F:\C# HW\CH07_HW_07\BoysNames.txt");
while (!inputfile1.EndOfStream && index < BoyNames.Length)
{
BoyNames[index] = inputfile1.ReadLine();
if (String.Equals(boyname, BoyNames[index], StringComparison.OrdinalIgnoreCase) == true)
{
Boyname = true;
}
index++;
}
return Boyname;
}
private Boolean FindGirlname()
{
const int size = 200;
string[] GirlNames = new string[size];
int index = 0;
string girlname = textBox2.Text;
Boolean Girlname = false;
StreamReader inputfile1;
inputfile1 = File.OpenText(@"F:\C# HW\CH07_HW_07\GirlsNames.txt");
while (!inputfile1.EndOfStream && index < GirlNames.Length)
{
GirlNames[index] = inputfile1.ReadLine();
if (String.Equals(girlname, GirlNames[index], StringComparison.OrdinalIgnoreCase) == true)
{
Girlname = true;
}
index++;
}
return Girlname;
}
private void button1_Click(object sender, EventArgs e)
{
Boolean boy;
Boolean girl;
boy = FindBoyname();
girl = FindGirlname();
if (boy.Equals(true))
{
MessageBox.Show(textBox1.Text + " is among the most popular boy names!");
}
if (boy.Equals(false))
{
MessageBox.Show(textBox1.Text + " is not among the most popular boy names.");
}
if (girl.Equals(true))
{
MessageBox.Show(textBox2.Text + " is among the most popular girl names!");
}
if (girl.Equals(false))
{
MessageBox.Show(textBox2.Text + " is not among the most popular girl names.");
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Boolean boy;
Boolean girl;
if (boy.Equals(true))
{
MessageBox.Show(textBox1.Text + " is among the most popular boy names!");
}
if (boy.Equals(false))
{
MessageBox.Show(textBox1.Text + " is not among the most popular boy names.");
}
if (girl.Equals(true))
{
MessageBox.Show(textBox2.Text + " is among the most popular girl names!");
}
if (girl.Equals(false))
{
MessageBox.Show(textBox2.Text + " is not among the most popular girl names.");
}
}
}
此代码不会在任何地方连接您的事件,因此请查看InitializeComponent
内部,看看它是否在那里连接
您正在寻找一行内容如下:
button1.Click += button1_Click;
如果它不在那里,那么这就是代码没有显示任何内容的原因。您尝试过调试吗?如果在
button1\u-Click
方法中设置了断点,程序是否曾命中它?请尝试以下操作:在Form1
构造函数中,添加button1.Click+=button1\u-Click
。