我试图创建一个添加按钮和一些信息文本框的表单。 单击“添加”按钮时,应读取文本框中给出的所有信息,并在列表视图和树视图中显示。 这是我的添加按钮单击事件代码。
private void btnAdd_Click(object sender, EventArgs e) { if(txtBYear.Text == "" || txtGender.Text == "" || txtMSSV.Text == "" || txtName.Text=="" || cBoxClasses.Text == "") { MessageBox.Show("Missing information!", "Error"); return; } if (txtGender.Text !="Female" || txtGender.Text!="Male") { MessageBox.Show("Male or Female only!", "Error"); return; } var mssv = txtMSSV.Text; var name = txtName.Text; uint bYear; bool gender; var addr = txtAddress.Text; uint cl = (uint)cBoxClasses.SelectedValue; var clName = cBoxClasses.SelectedText; if (txtGender.Text == "Female") gender = true; else gender = false; if (uint.TryParse(txtBYear.Text, out bYear)) { MessageBox.Show("Incorrect Birth Year!", "Error"); return; } var newStudent = new Student(cl, mssv, name, bYear, gender, addr); Classes x; if(classes.GetClassesById(cl, out x)) { x.AttendingStudents.Add(newStudent); } else { var tmp = new Classes(cl, clName); classes.Add(tmp); tmp.AttendingStudents.Add(newStudent); } lViewMain.Update(); tViewMain.Update(); }我尝试在txtGender文本框中键入“女性”并执行按钮单击,但是我的比较返回true ,这会使Messagebox弹出。
为什么我的检查textGender.Text != "Female" ||txtGender.Text != "Male"返回意想不到的价值?
I'm trying to create a form with an add button and some information text box. When I click the add button, all information given in text box should be read and show in list view and tree view. Here is my add button click event code.
private void btnAdd_Click(object sender, EventArgs e) { if(txtBYear.Text == "" || txtGender.Text == "" || txtMSSV.Text == "" || txtName.Text=="" || cBoxClasses.Text == "") { MessageBox.Show("Missing information!", "Error"); return; } if (txtGender.Text !="Female" || txtGender.Text!="Male") { MessageBox.Show("Male or Female only!", "Error"); return; } var mssv = txtMSSV.Text; var name = txtName.Text; uint bYear; bool gender; var addr = txtAddress.Text; uint cl = (uint)cBoxClasses.SelectedValue; var clName = cBoxClasses.SelectedText; if (txtGender.Text == "Female") gender = true; else gender = false; if (uint.TryParse(txtBYear.Text, out bYear)) { MessageBox.Show("Incorrect Birth Year!", "Error"); return; } var newStudent = new Student(cl, mssv, name, bYear, gender, addr); Classes x; if(classes.GetClassesById(cl, out x)) { x.AttendingStudents.Add(newStudent); } else { var tmp = new Classes(cl, clName); classes.Add(tmp); tmp.AttendingStudents.Add(newStudent); } lViewMain.Update(); tViewMain.Update(); }I try type "Female" in txtGender text box and execute the button click but my comparison returns true which makes Messagebox pop up.
Why does my check textGender.Text != "Female" ||txtGender.Text != "Male" return that unexpected value?
最满意答案
代码执行您告诉它的操作。 您的条件textGender.Text != "Female" ||txtGender.Text != "Male"说“如果文本不是'女'或文本不是'男'。
既然它不是'男',它会进入if区块。 你需要的是&&操作符
请尝试使用以下条件
textGender.Text != "Female" && txtGender.Text != "Male"
The code does what you told it to do. Your condition textGender.Text != "Female" ||txtGender.Text != "Male" says "if the text is not 'Female' or is the text is not 'Male'.
Since it is not 'Male', it gets in the if block. What you need is the && operator
Try using the following condition instead
textGender.Text != "Female" && txtGender.Text != "Male"
更多推荐
发布评论