将多个结果合并到单个消息框中

时间:2018-03-06 05:59:20

标签: c# visual-studio

我有一系列我要检查的条件,并且根据结果的组合,我的Windows窗体应用程序中应该出现一个不同的消息框。

if (pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix) 
    MessageBox.Show("CPL Filename Updated in ASSETMAP.");
if (!pklsizefix && cplhashfix && !oplhashfix && !cplpathamfix) 
    MessageBox.Show("OPL Filename Updated in ASSETMAP.");
if (!pklsizefix && !cplhashfix && oplhashfix && !cplpathamfix) 
    MessageBox.Show("PKL Filename Updated in ASSETMAP.");
if (!pklsizefix && cplhashfix && oplhashfix && !cplpathamfix) 
    MessageBox.Show("CPL and OPL Filename Updated in ASSETMAP.");

如何将所有可能的结果合并到一个消息框中,因此我不必为每种可能的组合创建单独的消息框案例?还有更多可能的组合 - 为了简洁起见,我将其简化为4行。

更新

让我试着更好地解释一下。我很好地创建了多个消息案例,我只是想减少列表,因为我正在检查12个不同的bool,并且每个组合都有一个稍微不同的消息。

下面的一个答案接近我想要的,但是当我尝试这个时,我只是得到一个空白的消息框:

var message = "";

if (!pklsizefix && !cplhashfix && !oplhashfix && cplpathamfix && !oplpathamfix)
     message = "CPL Size Updated in ASSETMAP.";
if (!pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix && oplpathamfix)
     message = "OPL Size Updated in ASSETMAP.";
if (!pklsizefix && cplhashfix && !oplhashfix && !cplpathamfix && oplpathamfix)
     message = "CPL Hash Updated in PKL.";
if (!pklsizefix && !cplhashfix && oplhashfix && !cplpathamfix && oplpathamfix)
     message = "OPL Hash Updated in PKL.";

MessageBox.Show(message);

我想要的是一个简单的消息框结果列表,如下所示:

CPL Updated in ASSETMAP.
OPL Updated in ASSETMAP.
CPL Hash Updated in PKL.
OPL Hash Updated in PKL.

类似于你在控制台中看到的,如果你只是打印出所有结果。还有比我在这里提出的更多独特案例。

以下是一些显示复杂性的当前代码示例:

if (pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix && !oplpathamfix && !pklpathamfix && !cplofnfixpkl && !oplofnfixpkl && !cplsizefixpkl && oplsizefixpkl && cplsizeamfix && !oplsizeamfix) 
    MessageBox.Show("OPL Size Value Updated in PKL. \nCPL and PKL Size Values Updated In ASSETMAP.");
if (pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix && !oplpathamfix && !pklpathamfix && !cplofnfixpkl && !oplofnfixpkl && cplsizefixpkl && !oplsizefixpkl && cplsizeamfix && oplsizeamfix)
    MessageBox.Show("CPL Size Value Updated in PKL and ASSETMAP. \nOPL and PKL Size Values Updated In ASSETMAP.");
if (pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix && !oplpathamfix && !pklpathamfix && !cplofnfixpkl && !oplofnfixpkl && cplsizefixpkl && oplsizefixpkl && !cplsizeamfix && !oplsizeamfix) 
    MessageBox.Show("CPL and OPL Size Values Updated in PKL. \nPKL Size Value Updated In ASSETMAP.");
if (pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix && !oplpathamfix && !pklpathamfix && !cplofnfixpkl && !oplofnfixpkl && cplsizefixpkl && !oplsizefixpkl && cplsizeamfix && !oplsizeamfix) 
    MessageBox.Show("CPL Size Value Updated in PKL and ASSETMAP. \nPKL Size Value Updated In ASSETMAP.");
if (pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix && !oplpathamfix && !pklpathamfix && !cplofnfixpkl && !oplofnfixpkl && !cplsizefixpkl && oplsizefixpkl && !cplsizeamfix && oplsizeamfix) 
    MessageBox.Show("OPL Size Value Updated in PKL and ASSETMAP. \nPKL Size Value Updated In ASSETMAP.");
if (pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix && !oplpathamfix && !pklpathamfix && !cplofnfixpkl && !oplofnfixpkl && cplsizefixpkl && oplsizefixpkl && cplsizeamfix && oplsizeamfix) 
    MessageBox.Show("CPL and OPL Size Values Updated in PKL And ASSETMAP. \nPKL Size Value Updated In ASSETMAP.");

3 个答案:

答案 0 :(得分:3)

将字符串生成与调用Left-Alt

分开
MessageBox.Show

答案 1 :(得分:0)

基于更新的问题:

var updatedFiles = new List<string>();

if (pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix)
    updatedFiles.Add("CPL Filename Updated in ASSETMAP.");
if (!pklsizefix && cplhashfix && !oplhashfix && !cplpathamfix)
    updatedFiles.Add("OPL Filename Updated in ASSETMAP.");
if (!pklsizefix && !cplhashfix && oplhashfix && !cplpathamfix)
    updatedFiles.Add("PKL Filename Updated in ASSETMAP.");
if (!pklsizefix && cplhashfix && oplhashfix && !cplpathamfix)
    updatedFiles.Add("CPL and OPL Filename Updated in ASSETMAP.");

if (updatedFiles.Count > 0)
    MessageBox.Show(string.Join("\r\n", updatedFiles));
else
{
    MessageBox.Show("No file updated.");
}

如果您想避免重复邮件,可以从List更改为HashSet。

答案 2 :(得分:0)

您可以使用任何类型的集合根据您的情况收集邮件,并将其显示在一个消息框中。

下面的代码使用List of string

显示它
var messages = new List<string>();  

if (pklsizefix && !cplhashfix && !oplhashfix && !cplpathamfix)  
    messages.Add("CPL");  
if (!pklsizefix && cplhashfix && !oplhashfix && !cplpathamfix)  
    messages.Add("OPL");  
if (!pklsizefix && !cplhashfix && oplhashfix && !cplpathamfix)  
    messages.Add("PKL");  
if (!pklsizefix && cplhashfix && oplhashfix && !cplpathamfix)  
{       
    messages.Add("CPL");  
    messages.Add("OPL");  
}
if (messages.Count > 0)  
    MessageBox.Show(string.Join(", ", messages) + " Filename Updated in ASSETMAP.");
相关问题