复制客户字段到发货信息字段私有void btn_Click c#
本文关键字:void btn Click 信息字段 字段 客户 复制 | 更新日期: 2023-09-27 18:10:44
我有一个使用c#创建Visual Basic 2008的表单我想添加一个按钮,点击时,它会检查多个字段,如果包含文本,然后粘贴到其他文本框,如果它们是空的这是我目前的代码,但它不工作
private void btnCopytoBAN_Click(object sender, EventArgs e)
{
if (BAddress.Text.Length < 0)
{BAddress.Text = MAddress.Text;}
else
if (BCity.Text.Length < 0)
{BCity.Text = MCity.Text;}
else
if (BState.Text.Length < 0)
{BState.Text = MState.Text;}
else
if (BZIP.Text.Length < 0)
{BZIP.Text = MZIP.Text;}
else
if (BEmail.Text.Length < 0)
{BEmail.Text = MEmail.Text;}
else
if (BName.Text.Length < 0)
{BName.Text = MFName.Text + " " + MLName.Text;}
else
if (TEmail.Text.Length < 0)
{TEmail.Text = MEmail.Text;}
else
if (TName.Text.Length < 0)
{TName.Text = MFName.Text + " " + MLName.Text;}
else
if (MWork.Text.Length < 0)
if (MWork.Text.Length > 0)
{BPhone.Text = MWork.Text;
TPhone.Text = MWork.Text; ;
}
else if (MMobile.Text.Length > 0)
{BPhone.Text = MMobile.Text;
TPhone.Text = MMobile.Text; ;}
else if (MHome.Text.Length > 0)
{BPhone.Text = MHome.Text;
TPhone.Text = MHome.Text; ;}
}
文字。长度不能小于0,如果Text为空则可以为0。我想这就是你代码的问题所在。
可以使用String。IsNullOrEmpty代替Text。长度如下。
if (String.IsNullOrEmpty(BAddress.Text)) //(BAddress.Text.Length < 0)
{
BAddress.Text = MAddress.Text;
}
我对你的if-else语句感到困惑,确保你正确地做了。你没有具体说明你的问题。你的if-else应该像下面这样吗?你真的需要其他条款吗?
if (BAddress.Text.Length = 0)
{BAddress.Text = MAddress.Text;}
if (BCity.Text.Length = 0)
{BCity.Text = MCity.Text;}
if (BState.Text.Length = 0)
{BState.Text = MState.Text;}
if (BZIP.Text.Length = 0)
{BZIP.Text = MZIP.Text;}
...............
if (MWork.Text.Length > 0)
{BPhone.Text = MWork.Text;
TPhone.Text = MWork.Text;}
if (MMobile.Text.Length > 0)
{BPhone.Text = MMobile.Text;
TPhone.Text = MMobile.Text;}
if (MHome.Text.Length > 0)
{BPhone.Text = MHome.Text;
TPhone.Text = MHome.Text;}
感谢大家的帮助,这是我现在使用的工作完美的代码。
private void btnCopytoBAN_Click(object sender, EventArgs e)
{
if (BAddress.Text.Length == 0)
BAddress.Text = MAddress.Text;
if (BCity.Text.Length == 0)
BCity.Text = MCity.Text;
if (BState.Text.Length == 0)
BState.Text = MState.Text;
if (BZIP.Text.Length == 0)
BZIP.Text = MZIP.Text;
if (BEmail.Text.Length == 0)
BEmail.Text = MEmail.Text;
if (BName.Text.Length == 0)
BName.Text = MFName.Text + " " + MLName.Text;
if (TEmail.Text.Length == 0)
TEmail.Text = MEmail.Text;
if (TName.Text.Length == 0)
TName.Text = MFName.Text + " " + MLName.Text;
if (BPhone.Text.Length == 0)
{
BPhone.Text = MWork.Text;
if (BPhone.Text.Length == 0)
BPhone.Text = MMobile.Text;
if (BPhone.Text.Length == 0)
BPhone.Text = MHome.Text;
}
if (TPhone.Text.Length == 0)
{
TPhone.Text = MWork.Text;
if (TPhone.Text.Length == 0)
TPhone.Text = MMobile.Text;
if (TPhone.Text.Length == 0)
TPhone.Text = MHome.Text;
}
}