开发者

How can I display more results on a RichTextBox?

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(sympto开发者_Python百科m7.Checked && symptom8.Checked)
  {
     Result.Text="";
     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";
    }
}


You can try this:

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


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;


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜