This is the first time I have really done any GUI work. I had a console app in C++ that I rewrote into C# and am trying to make a GUI for it. However I am not sure how to display my classes show methods in the Text box. I can make it display text form within the form class. But I want it to be able to call the display method from my other classes and print out the output in a rich text box I have. I also want to clear the output if a user selects a different tab. I have not been able to find much info on printing from other classes.

What I have is one namespace with my forms class and several other classes in the namespace. I can write to the textbox from with the forms class but I am unable to write to the textbox from within my other classes.

Any help is appreciated.

Thanks

post some of the code that you have, what you need to do is make sure that the text box you are printing to is public.

also you may run into some issues with the fact that you are working between two different classes see.

we come into scope issues really (i dont quite know a better way to explain it).

class myForm : Form
{
  //some designer code with form stuff
}
 
class modifyForm
{
  //some methods to modify the myform class
}

now if your myForm is called from something else, IE it starts when the application loads then myclass is going to run into issues such as

myForm modifyForm = new myForm();

because the moment you do that you are instantiating a new copy of the myform so any changes you make to this form are not going to be recognized by the old form.

so one possible method is to work with a global setting for the RichTextBox

IE go into the properties/settings and add a new settings type. then on either form add an event type that watches for the change of settings and update the box as needed.

but there are other methods of doing this.

the most basic method is this

class MyFirstClass : Form
{
  //intializer
  public  MyFirstClass()
  {
      //your initializer code
  }
  public void OpenNextForm() {
    ModifierClass foo = new ModifierClass(this);
    foo.Show();
  }
}
 
//assume we are another file, or just further down the same file with a new class.  
 
//Okay so at this point we want to use this new class to modify 
//MyFirstClass, This is quite simple actually
class ModifierClass : Form
{
  public ModifierClass(MyFirstClass sender) {
    this.x_MyFirstClass = sender;
  }
  private MyFirstClass x_MyFirstClass;
}

okay so lets analyze what we have done.

we have MyFirstClass, this is the main form in which you want to modify from the second class (or form)

When our application runs it loads up MyFirstClass (the first form) and let us say that when a button is clicked it runs the private method that calls ModifierClass (or the second form)

we set the initializer in our code on the ModifierClass to require that MyFirstClass be passed to it.

Doing so allows us to modify MyFirstClass from our second class.

we can now write method using the x_MyFirstClass having access to all of the standard public methods and properties of that class.

This may not be the exact end result you are looking for but you can see how this is done now.

you could call any class and ask that the new class that is being instantiated be passed another class so that it may have access to anything set within the current class at a given time.

Thank you, this does help a lot. I thought it had some thing to do with passing the form class to my other classes. I still think in terms of console. I understand what you are saying but still am having problems. My temp solution was just turn my Output from my other classes into strings and then pass them to the form and write them directly from there. I did try what you said about passing the form but my classes still do not know about the rich text box. I tried to right click on the rick text box and go properties and change the modifier to public but my classes still do not know about it. I also tried using public class patient : application but it still did not know about my rich text box on the form.

This is how I have form now

namespace MedApp
{
        public partial class Application : Form
        {
            //Methods
            //Button Show Calls the Show All Method of the Meds
            //I want it to display this method in the rich text box
        }
        
        public structure provider
       {
           //Methods
       }

       public class patient
       {
           //Methods
       }

       public class meds : base patient
      {
             void ShowAll(Provider PI, others)
            {
                  //This method displays the complete listing
                 
            }
      }
}

so as i gather it you have this rich text box on your main form,

you want to be able to process information from one class and have it output on your main form? IE:

class MyMainClass : Form
{
  public partial class MyMainClass()
  {
      InitializeComponent();
      //your initializer code here
  }
  private void someButton_Click(object sender, EventArgs e) {
    //from this button we will call another classes function? 
    //what this class returns needs to be placed into our text box
  }
}
 
class CustomFunction
{
  public CustomFunction()
  {
    //some initializer code here
  }
  pubic string SomeFunction(string foo, string bar) {
  }
}

so as i gather it (i may sound a bit repetative here :lol: ) you want CustomFunction class to modify the contents of our main form?

if so that is much simpler than it may seem.

you can create a function with a return type (like that of SomeFunction) and call it from your main class and store its returned value into your richtextbox

if however i seem to be missing what you are saying just let me know :D

code would look similar to this

class MyMainClass : Form
{
  public partial class MyMainClass()
  {
      InitializeComponent();
      //your initializer code here
  }
  private void someButton_Click(object sender, EventArgs e) {
    //from this button we will call another classes function? 
    //what this class returns needs to be placed into our text box
    this.richTextBox1.Text = new CustomFunction().SomeFunction("shorter", "LongestString");
  }
}
 
class CustomFunction
{
  public CustomFunction()
  {
    //some initializer code here
  }
  pubic string SomeFunction(string foo, string bar) {
    //function returns the longer of the two strings
    string longestString = string.empty; //the string to return
    if(foo.Length > bar.Length)
    {
       longestString = foo;
    }else 
    {
      if(foo.Length < bar.Length)
      {
         longestString = bar;
      }
    }
     return longestString;  //returns null if strings are same size
  }
}

upon clicking the button the richTextBox would recieve the text "LongestString"

we can also take a look at what i did!

//on the line
this.richTextBox1.Text = new CustomFunction().SomeFunction("shorter", "LongestString");
 
//im stating that the text in richTextBox1 will be equal to what is
//returned by our SomeFunction.
//It is important to note the keyword new in front of customFunction
//By stating the new keyword i am creating a new class that will
//exist within the scope of the function and it's resources will be
//released upon leaving the method
 
//another option could have been to write
CustomFunction functionClass = new CustomFunction();
 
//then i would be able to access all of CustomFunctions
//methods using the "." operator 
 
this.richTextBox1.Text = functionClass.SomeFunction("string1", "string2");

i hope this helps you out some!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.