Hester Hester - 1 year ago 178
C# Question

In C# windows forms how can I use TextBox.Text as another property name to display text on a textbox?

In C# windows forms how can I use TextBox.Text property as another property named TextBox.WritePublicText to display text on a multiline textbox? I am editing C# game code that cannot be modified. I am trying to simulate the game code exactly in Visual Studio to edit and then copy directly into the game code. Also I am new to C# so try to dumb it down.

This code displays the text on the windows form textbox named textBox1:

textBox1.Text = "Text to display\r\n"
+ "More Text\r\n"
+ "More Text2\r\n";

This is the code displays the text in game. But I cannot figure out how make it display the text in a windows form textbox.

textBox1.WritePublicText = "Text to display\r\n"
+ "More Text\r\n"
+ "More Text2\r\n";

Other Ideas:

It might be possible to display multiline text another way on windows forms that will allow me to do this.

Answer Source

Create a class and inherit from Windows.Forms.TextBox.

Like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication
    public class CustomTextBox : TextBox
        public String WritePublicText
            get { return Text; }
            set { Text = value; }

This tool now is exactly like a normal TextBox, but now it contais a new method called "WritePublicText" that sets and returns a String.

Rebuild your application, then you will see a new tool in your toolBox called CustomTextBox, just drag and drop like any other component.

If you already have to many TextBoxes in you application you could go in your designer file and change the 'TextBox' to 'CustomTextBox' and then rebuild again.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download