我是一个全新的n00bie在视觉c#和我遇到了一个奇怪的障碍,这是驾驶我疯狂!这是有问题的代码(是的,一个Hello World程序):如果在按钮c#中的语句#
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals("Goodbye Cruel World"))
{
textBox1.Text = ("Hello World!");
}
else { textBox1.Text = ("Goodye Cruel World"); }
}
}
}
我还试图用textBox1.Text == “再见残酷的世界”;作为if语句在编译器中没有错误的评估参数(按照我使用Visual Studio 2012 Ultimate的方式)
程序运行良好。我将textbox文本属性初始化为“Hello World!”使用VS的设计GUI。我面临的问题是代码只在用户第一次单击按钮时起作用。任何时候按钮没有任何时间。
我调试了代码,并确保在第一次用户单击按钮时适当更改了文本框文本属性。当用户第二次点击该按钮(或之后的任何时间)时,一旦代码到达if语句,它将跳过它,就好像对内部表达式的评估是FALSE一样。实际上,跟上调试工具,按钮只会继续执行else块内的代码,即使我知道我正在使用的TextBox.Text属性之前已被适当地更改过。
我在这里丢失什么?为什么按钮只是在我硬编码的两个字符串之间切换文本框的文本值?
拼写错误。 ''Goodye Cruel World''你错过了'b'。 – Yahya
你在这里缺少一个尖锐的眼睛。 –
这就是为什么你应该总是为这样的事情使用一个'private const string'字段,而不是一遍又一遍地输入字符串。 :) –