accessing windows forms textbox properties

This is a discussion on accessing windows forms textbox properties within the C# Programming forums, part of the General Programming Boards category; i have a class that outputs text to the console, i want to output the same text to a textbox, ...

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    accessing windows forms textbox properties

    i have a class that outputs text to the console, i want to output the same text to a textbox, however the console method is in a different class, i have been raeding Deitel C# how to program and see various ways to achieve this but im not sure which way is best. here are the classes involved in outputing the text

    Code:
    using System;
    using System.Collections;
    
    namespace TrafficControlSim
    {
    public class TrafficQueue
    	{
    	private Queue vehicleQueue;//fifo object
        
    	// constructor - one integer input  
    	public TrafficQueue(int maximumQueueSize)
    		{
    		vehicleQueue = new Queue(maximumQueueSize);
    		}
    
    	public void Arrive(Vehicle arrival)
    		{
    		vehicleQueue.Enqueue(arrival);//add vehicle to queue
    		}
    
    	public int TrafficQueueSize()
    		{
    		return vehicleQueue.Count;
    		}
    
    	
    	public Vehicle Depart()
    	{
    		Vehicle aVehicle;
    		if (vehicleQueue.Count>0)
    		{	
    			aVehicle=(Vehicle)vehicleQueue.Dequeue();
    			vehicleQueue.TrimToSize();
    			return (aVehicle);
    		}
    		else
    			return null;
    	}
    
    	public int TrafficInQueue()
    		{
    		if (vehicleQueue.Count>0)
    			foreach(Vehicle aVehicle in vehicleQueue)
    				aVehicle.ShowType();/* i want to send this text to the textbox in Form1 class */
    
    		else
    			
    			Console.WriteLine("Traffic Queue is Empty.");
    		return vehicleQueue.Count;
    		}
    	}
    }
    Code:
    using System;
    
    namespace TrafficControlSim
    {
    	public class Vehicle
    	{
    		private string registrationNumber;
    		private string vehicleType;
    
    		public Vehicle()
    		{
    			registrationNumber="TBD";
    			vehicleType="TBC"; 
    		}
    		public virtual void ShowDetails()
    		{
    			Console.WriteLine("Vehicle is a "+vehicleType);
    			Console.WriteLine("Registration number is "+registrationNumber);
    		}
    
    		public void SetRegistrationNumber(string number)
    		{
    			registrationNumber=number;
    		}
    		public void SetVehicleType(string type)
    		{
    			vehicleType=type;
    		}
    		public string GetRegistrationNumber()
    		{
    			return registrationNumber;
    		}
    		public string GetVehicleType()
    		{
    			return vehicleType;
    		}
    		public string ShowType()
    		{
    			return vehicleType+" ";
    		}
    
    	}/* end class */
    
    }/* end namespace */
    the Form1 class is rather large which inherits from System.Windows.Forms.Form this class has the textboxs i want to display . I have been working on this for a few days now and am getting quite frustrated due to my lack on knowledge around OOP

    thanks in advance
    Last edited by luigi40; 04-11-2005 at 03:42 AM.

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Code:
    		public virtual void ShowDetails()
    		{
    			Console.WriteLine("Vehicle is a "+vehicleType);
    			Console.WriteLine("Registration number is "+registrationNumber);
    		}
    You need to let this method return a String, then you can set the text field according to whatever ShowDetails returns...

    ( thats the way i would do it in J# and i dont think it will be much different in C# ).

    Also when using a form you should really look at your whole project in an OO way.
    You can write your own mutator for the text field you want to update and call it from within your method ShowDetails -- i think...

  3. #3
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61
    can you explain in simpler terms as i am a neophyte to C#, i have modified the base method to
    Code:
    using System;
    
    namespace TrafficControlSim
    {
    	public class Vehicle 
    	{
    		private string vehicleType;
    
    		public Vehicle()
    		{
    			vehicleType="TBC"; 
    		}
    		public virtual string ShowDetails()
    		{
    			return vehicleType;
    		}
    		
    		public void SetVehicleType(string type)
    		{
    			vehicleType=type;
    		}
    		
    		public string GetVehicleType()
    		{
    			return vehicleType;
    		}
    		
    		public void ShowType()
    		{
    			Console.Write(vehicleType+" ");
    		}
    
    	}/* end class */
    
    }/* end namespace */
    however i dont understand what you mean, i had to modify the derived classes methods to

    Code:
    public override string ShowDetails()
    		{
    			base.ShowDetails();
    			return null;
    		}
    in order to compile
    im still not sure what to do now, i would like to print out the vehicleQueue text fields in the Form1 class, but i cant fathom out how to access the individual elements while in the Form1 class's scope. i have tried creating a reference to a same type object (Queue) object and passing the object etc, the object gets passed to Form1 ok with the correct details but how do i access the text fields, there are no methods that i can use
    Last edited by luigi40; 04-11-2005 at 06:07 AM.

  4. #4
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    He means something like this:

    Code:
    		public virtual string ShowDetails()
    		{
    			return "Vehicle is a " + vehicleType + "\n" +
    			"Registration number is " + registrationNumber + "\n";
    		}

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Quote Originally Posted by Frobozz
    He means something like this:

    Code:
    		public virtual string ShowDetails()
    		{
    			return "Vehicle is a " + vehicleType + "\n" +
    			"Registration number is " + registrationNumber + "\n";
    		}
    And if that one doesnt work then you can pass the textbox as argument to the method ShowDetails , then you can set your text from there as well ( then your method ShowDetails wont have to return anything... ).

    It all depends on how you want it to be implemented in your classes and what suits your needs.

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Theres another way to do it but it's somewhat advanced - if you don't know what a Stream is stop reading. You can set the TextStreams that the Console methods write to.

    Create your own stream by inheriting from StreamWriter. Give your clas a collection of other streams (you'll attatch the consoles StdOut to this collection). Then, overload the Write methods:
    Code:
    // MyListeningStreams is a public property of this class that holds a collection of 
    // streams, mainly StdOut, but you could also add another stream for your text 
    // box to listen on
    public override void Write(string s)
    {
        foreach (Stream stream in MyListeningStreams) 
        {
            stream.Write(s);
        }
        // Alternatively, you could fire an Event here that your text box could listen on
    }
    When your program starts, do something like
    Code:
        Stream theConsolesStream = Console.Out;   // get the old Write stream
        Console.SetOut(myNewSuperStream);
        myNewSuperStream.MyListeningStreams.Add(theConsolesStream);
    
        // Now you can just do Console.WriteLine and both the console and the text
        // text box will see what you write

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good introduction to Windows Forms?
    By jcafaro10 in forum C# Programming
    Replies: 1
    Last Post: 05-19-2009, 07:11 PM
  2. accessing file properties
    By subodh_dg in forum Windows Programming
    Replies: 2
    Last Post: 12-26-2005, 12:23 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21