我如何获得一个按钮打开一个新表单,但只有文本框填写 [英] How Do I Get A Button To Open A New Form But Only If Textboxes Are Filled In
问题描述
大家好,好吧,所以我有这个布局/一个按钮和2个文本字段,我的计划是当两个文本字段都是空的时候你点击按钮一个错误信息打开,我的问题是,当我点击按钮我得到消息,当我单击确定时,下一个表单仍然打开。我的代码
if(string.IsNullOrEmpty(txtPassword.Text)||(string.IsNullOrEmpty(textbox8.Text)))
MessageBox.Show(field空。,错误,MessageBoxButtons.OK,MessageBoxIcon.Warning);
文件mf = new Files();
隐藏();
mf.ShowDialog();
关闭();
谢谢,
// reffer下面的代码......
private void btn_Click( object sender,EventArgs e)
{
Files mf = new Files(); < span class =code-comment> // 表单对象
如果 ( string .IsNullOrEmpty(txtPassword.Text)||( string .IsNullOrEmpty(textbox8.Te) xt)))
{
MessageBox.Show( field empty。 , 错误,MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else
{
mf.ShowDialog(); // show dialog
}
}
简单 - 添加else子句:
if (< span class =code-keyword> string .IsNullOrEmpty(txtPassword.Text)||( string .IsNullOrEmpty(textbox8.Text)))
MessageBox.Show( field empty。, 错误,MessageBoxButtons.OK,
else
{
MessageBoxIcon.Warning);
文件mf = 新文件();
隐藏();
mf.ShowDialog();
关闭();
}BTW:这是一个好主意(特别是当你刚刚开始时)总是添加大括号,即使它只是一行:
< pre lang =c#> if ( string .IsNullOrEmpty(txtPassword.Text)||( string .IsNullOrEmpty(textbox8.Text)))
{
MessageBox.Show( 字段为空。, 错误 ,MessageBoxButtons.OK,
}
else
{
MessageBoxIcon.Warning);
文件mf = 新文件();
隐藏();
mf.ShowDialog();
关闭();
}这样,很明显会发生什么事情。
请自己帮个忙,并停止使用Visual Studio默认名称 - 你可能还记得今天的TextBox8是手机号码,但是当你必须在三周内修改它时,你会这样吗?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键......
Hi all, okay so i have this layout/ a button and 2 textfields, my plan is when both text fields are empty and you click the button a error message opens up, my problem is that when i click the button i get the message , when i click ok the next form still opens. my code"
if (string.IsNullOrEmpty(txtPassword.Text) || (string.IsNullOrEmpty(textbox8.Text))) MessageBox.Show("field empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); Files mf = new Files(); Hide(); mf.ShowDialog(); Close();
thanks,
//reffer below code......
private void btn_Click(object sender, EventArgs e) { Files mf = new Files(); //form object if (string.IsNullOrEmpty(txtPassword.Text) || (string.IsNullOrEmpty(textbox8.Text))) { MessageBox.Show("field empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { mf.ShowDialog(); //show dialog } }
Simple - add an else clause:
if (string.IsNullOrEmpty(txtPassword.Text) || (string.IsNullOrEmpty(textbox8.Text))) MessageBox.Show("field empty.", "Error", MessageBoxButtons.OK, else { MessageBoxIcon.Warning); Files mf = new Files(); Hide(); mf.ShowDialog(); Close(); }BTW: it's a good idea (especially when you are just starting out) to always add the curly brackets, even if it's just one line:
if (string.IsNullOrEmpty(txtPassword.Text) || (string.IsNullOrEmpty(textbox8.Text))) { MessageBox.Show("field empty.", "Error", MessageBoxButtons.OK, } else { MessageBoxIcon.Warning); Files mf = new Files(); Hide(); mf.ShowDialog(); Close(); }That way, it's immediately obvious what is expected to happen.
And please, do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
这篇关于我如何获得一个按钮打开一个新表单,但只有文本框填写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!