Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a C# file which displays output in output window. and I have created a Windows form with richtext Box.
I want C# output in that RichtextBox when form is loaded.
How to do this?

What I have tried:

Here is my C# file:
class JsonMain
   {
     public void JsonArr()
       {

           using (var webClient = new System.Net.WebClient())
           {
               string json = webClient.DownloadString("http://www.mywedjat.com/services/apis/get_doctor?id=2http://www.mywedjat.com/services/apis/login_api?email=darwin@mail.com&password=123456&mobile_type=ios&UDID=3454543");



               Console.WriteLine("-------------------------------------------------------------------------------------------");

               JObject json1 = JObject.Parse(json.ToString());
            // Console.WriteLine(json1);
               var tmp = JsonConvert.DeserializeObject<RootObject>(json);
               var successes = tmp.status.success;
               var specialityObjects = successes.Select(success => success.Speciality).ToList();
               var users = successes.Select(success => success.User).ToList();
               Console.WriteLine("-------------------------------------------------------------------------------------------");

               Console.WriteLine("Name: " + users[0].username + "\n" + "Email: " + users[0].email + "\n" + "Qualification: " + users[0].qualification + "\t");
               Console.WriteLine("-------------------------------------------------------------------------------------------");

               Console.WriteLine("Name: " + users[1].username + "\n" + "Email: " + users[1].email + "\n" + "Qualification: " + users[1].qualification + "\t");
               Console.WriteLine("-------------------------------------------------------------------------------------------");

               Console.WriteLine("Name: " + users[2].username + "\n" + "Email: " + users[2].email + "\n" + "Qualification: " + users[2].qualification + "\t");

               //Console.WriteLine(users[1].id + " " + users[1].username);
               //Console.WriteLine(users[2].id + " " + users[2].username);

               //Console.WriteLine(tmp.status.success[0].User);
               Console.WriteLine("-------------------------------------------------------------------------------------------");


               Console.ReadLine();
           }

       }


   }
Posted
Updated 20-Sep-17 1:05am
v2

1 solution

Instead of using Console.WriteLine, create a StringBuilder and use the AppendLine method to add your text to that.
You can then do this:
C#
myRichTextBox.Text = myStringBuilder.ToString();
 
Share this answer
 
v2
Comments
Nishisansi 20-Sep-17 7:13am    
It does not works.
created stringbuilder like:

StringBuilder sb =new StringBuilder("Name: " + users[0].username + "\n" + "Email: " + users[0].email + "\n" + "Qualification: " + users[0].qualification + "\t");

and called this file:
public Form1()
{
InitializeComponent();

JsonMain jm = new JsonMain();
richTextBox1.Text = sb.Tostring();

}
showing error in sb
OriginalGriff 20-Sep-17 7:18am    
Where did you create the StringBuilder, and what error did you get?
Nishisansi 21-Sep-17 1:11am    
Created stringBuilder in C# file. and called it in Windows form Code behind.
OriginalGriff 21-Sep-17 2:06am    
Are you assuming that I can see your screen?
Because I can't. So unless you tell me what you did in ways that mean I understand exactly what you did, I can't help you, and more than a garage can help you if you break down in the middle of nowhere and just scream "HELP!" down the phone at them.

So help me out here! Show me where you create the StringBuilder, where you use it, and what error you get!
Nishisansi 21-Sep-17 2:13am    
This is my .cs file:

class JsonMain
{


public void JsonArr()
{

using (var webClient = new System.Net.WebClient())
{
string json = webClient.DownloadString("http://www.mywedjat.com/services/apis/get_doctor?id=2http://www.mywedjat.com/services/apis/login_api?email=darwin@mail.com&password=123456&mobile_type=ios&UDID=3454543");
JObject json1 = JObject.Parse(json.ToString());
var tmp = JsonConvert.DeserializeObject<rootobject>(json);
var successes = tmp.status.success;
var specialityObjects = successes.Select(success => success.Speciality).ToList();
var users = successes.Select(success => success.User).ToList();
StringBuilder sb = new StringBuilder("Name: " + users[0].username + "\n" + "Email: " + users[0].email + "\n" + "Qualification: " + users[0].qualification + "\t");
StringBuilder sb1 = new StringBuilder("Name: " + users[1].username + "\n" + "Email: " + users[1].email + "\n" + "Qualification: " + users[1].qualification + "\t");
StringBuilder sb2 = new StringBuilder("Name: " + users[2].username + "\n" + "Email: " + users[2].email + "\n" + "Qualification: " + users[2].qualification + "\t");
Console.ReadLine();
}

}


}
In windows form i just dragged a rich text box. and code behind of this form is.

namespace JsonForm
{

public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
JsonMain jm = new JsonMain();
jm.JsonArr();
richTextBox1.Text = jm.sb.ToString();


}
}
and the error is: JsonMain does not contain a definition for 'sb' and no extension method 'sb' accepting a first argument of type 'JsonMain' could not be found(are you missing a using directive?)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900