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

问题描述 投票:0回答:3

我有一系列条件,我正在检查,并根据结果的组合,我的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.");
c# visual-studio
3个回答
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。


3
投票

将字符串生成与调用MessageBox.Show分开

var message = "";
if(...)
  message = "....";
else if(..)
  message = "//"

MessageBox.Show(message);

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.");
© www.soinside.com 2019 - 2024. All rights reserved.