如何在 RichTextBox 上显示更多结果?

发布于 2024-11-04 04:47:04 字数 452 浏览 1 评论 0原文

我是 C# 的初学者,所以请对我温柔一点。

我在尝试在富文本框中显示更多结果时遇到问题,因为它一次只能显示一个结果。但是我想在单选按钮已经重置时显示下一个结果,并且上一个结果也应该仍然在富文本框中。下面是我的编码:

for(int i=0,i<30;i++)
{
  if(symptom1.Checked && symptom2.Checked)
  {
     Result.Text="";
     Result.Text += "Your Disease is: "+"Meningiomas";
  }

  if(symptom7.Checked && symptom8.Checked)
  {
     Result.Text="";
     Result.Text += "Your Disease is: "+"Pituitary Tumor";
  }
}

I'm a beginner in C# so please be gentle to me .

I'm having a problem trying to display more results in rich text box because it can only display one result at once. But I would like to display the next result when the radio buttons already being resetted and the previous result should also still in the rich text box. Below is my coding:

for(int i=0,i<30;i++)
{
  if(symptom1.Checked && symptom2.Checked)
  {
     Result.Text="";
     Result.Text += "Your Disease is: "+"Meningiomas";
  }

  if(symptom7.Checked && symptom8.Checked)
  {
     Result.Text="";
     Result.Text += "Your Disease is: "+"Pituitary Tumor";
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

別甾虛僞 2024-11-11 04:47:04

如果你不想删除之前的内容,删除这段代码:

Result.Text="";

我想,你想要实现的是:

  1. 清除RichTextBox
  2. 将所有匹配的疾病写入RichTextBox

这可以用这段代码来完成:

Result.Text="";

for(int i = 0; i < 30; ++i)
{
    if(symptom1.Checked && symptom2.Checked)
    {
        Result.Text += "Your Disease is: "+"Meningiomas";
    }

    if(symptom7.Checked && symptom8.Checked)
    {
        Result.Text += "Your Disease is: "+"Pituitary Tumor";
    }
}

If you don't want to delete the previous content, remove this code:

Result.Text="";

I think, what you want to achieve is:

  1. Clear the RichTextBox
  2. Write all matching diseases to the RichTextBox

This can be done with this code:

Result.Text="";

for(int i = 0; i < 30; ++i)
{
    if(symptom1.Checked && symptom2.Checked)
    {
        Result.Text += "Your Disease is: "+"Meningiomas";
    }

    if(symptom7.Checked && symptom8.Checked)
    {
        Result.Text += "Your Disease is: "+"Pituitary Tumor";
    }
}
心欲静而疯不止 2024-11-11 04:47:04

你可以试试这个:

richTextBox1.AppendText(Environment.NewLine + "Sample text");

You can try this:

richTextBox1.AppendText(Environment.NewLine + "Sample text");
如果没有你 2024-11-11 04:47:04

您一次只能显示一个结果的原因是您在添加下一行之前先清除文本框的当前内容。具体来说,这行代码是罪魁祸首:

Result.Text="";

由于您将 Text 属性设置为等于空字符串 (""),因此会清除已经存在的所有内容,本质上是删除文本框。

如果您只是从代码中删除该行,您应该会看到您想要的内容。下一行将附加到已经存在的内容上。然而,这只会将其粘贴到已经存在的任何东西上。
它不会将其插入新行。为此,您必须在开头插入换行符:

for (int i = 0; i < 30; i++)
{
   if (symptom1.Checked && symptom2.Checked)
   {
      Result.Text += Environment.NewLine + "Your Disease is: " + "Meningiomas";
   }

   if (symptom7.Checked && symptom8.Checked)
   {
      Result.Text += Environment.NewLine + "Your Disease is: " + "Pituitary Tumor";
   }
}

(请注意,Environment.NewLine 与转义序列 \r\n 相同。它只是插入一个新行。)

如果您想首先清除文本框的当前内容,然后附加 for< 中的所有值。 /code> 循环,您需要将代码行添加回函数的开始,for 循环之前。从风格上来说,我也会将其写为 string.Empty 而不是 "",但这并不重要。

Result.Text = string.Empty;

The reason you're only able to display one result at a time is because you clear the current contents of the textbox first, before adding the next line. Specifically, this line of code is the culprit:

Result.Text="";

Since you're setting the Text property equal to an empty string (""), that clears out whatever is already there, essentially erasing the textbox.

If you simply remove that line from your code, you should see exactly what you want. The next line is appended to whatever is already there. However, this will simply stick it onto whatever is already there.
It won't insert it onto a new line. To do that, you have to insert a line break at the beginning:

for (int i = 0; i < 30; i++)
{
   if (symptom1.Checked && symptom2.Checked)
   {
      Result.Text += Environment.NewLine + "Your Disease is: " + "Meningiomas";
   }

   if (symptom7.Checked && symptom8.Checked)
   {
      Result.Text += Environment.NewLine + "Your Disease is: " + "Pituitary Tumor";
   }
}

(Note that Environment.NewLine is the same thing as the escape sequences \r\n. It just inserts a new line. Same either way.)

If you want to clear the current contents of the textbox first, and then append all the values in your for loop, you need to add the line of code back at the beginning of the function, before the for loop. Stylistically, I would also write it as string.Empty rather than "", but it doesn't really matter.

Result.Text = string.Empty;
江挽川 2024-11-11 04:47:04
for(int i=0,i<30;i++)
{
 if(symptom1.Checked && symptom2.Checked)
  {
     //Result.Text=""; remove this
     Result.Text += "Your Disease is: "+"Meningiomas";
  }

if(symptom7.Checked && symptom8.Checked)
{
     //Result.Text=""; remove this
     Result.Text += "Your Disease is: "+"Pituitary Tumor";
}
}

结果.Text="";导致您的现有数据被空文本替换,删除它,您应该对结果感到满意。

希望这有帮助。

for(int i=0,i<30;i++)
{
 if(symptom1.Checked && symptom2.Checked)
  {
     //Result.Text=""; remove this
     Result.Text += "Your Disease is: "+"Meningiomas";
  }

if(symptom7.Checked && symptom8.Checked)
{
     //Result.Text=""; remove this
     Result.Text += "Your Disease is: "+"Pituitary Tumor";
}
}

Result.Text=""; is causing your existing data to be replaced by an empty text, remove that and you should be fine with your results.

Hope this helps.

花期渐远 2024-11-11 04:47:04

你的意思是一个可怜的不幸者可能有症状1、2、7和8吗?因此可能同时患有两种类型的肿瘤:

for(int i=0,i<30;i++) {
  Result.Text="Your Disease is:";
  if(symptom1.Checked && symptom2.Checked) {
     Result.Text += " Meningiomas";
  }
  if ( symptom7.Checked && symptom8.Checked ) {
     Result.Text += " Pituitary Tumor";
  }
}

干杯。基思.

PS:你的问题确实不太清楚。

Do you mean a poor unfortunate might have syptoms 1, 2, 7, and 8; and therefore probably has two types of tumor at once:

for(int i=0,i<30;i++) {
  Result.Text="Your Disease is:";
  if(symptom1.Checked && symptom2.Checked) {
     Result.Text += " Meningiomas";
  }
  if ( symptom7.Checked && symptom8.Checked ) {
     Result.Text += " Pituitary Tumor";
  }
}

Cheers. Keith.

PS: Your question really isn't very clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文