Changes were made to the original code, here is the latest...
[CommandMethod("MTED")]
static public void mtexteditor()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptEntityResult res = ed.GetEntity("\nSelect a MText: ");
if (res.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
MText text = tr.GetObject(res.ObjectId, OpenMode.ForWrite, false) as MText;
if (text == null) return;
string origText = text.Contents;
using (MTextForm form = new MTextForm(origText))
{
// save original text
form.OrigMText = origText;
acadApp.ShowModalDialog(form);
text.Contents = form.Contents;
}
tr.Commit();
}
}
namespace MTextEditor
{
public partial class MTextForm : Form
{
private string orig;
public string OrigMText
{
get { return orig; }
set { orig = value; }
}
public MTextForm()
{
InitializeComponent();
}
public MTextForm(string mtextContents) : this()
{
textBox1.Text = mtextContents;
}
// remove color from the mtext
private void OnClick_RemoveColor(object sender, EventArgs e)
{
string txtstr = textBox1.Text;
char[] s = txtstr.ToCharArray();
int cont = 0, flag = 0;
string txt1 = "";
for (int j = 0; j < txtstr.Length; j++)
{
try
{
char let = s[cont];
if (let == '\\')
{
if (char.ToUpper(s[cont + 1]) == 'C')
{
int i = (cont + 2);
while (s[i] != ';')
i++;
// remove true and book colors
if (s[i + 1] == '\\' && s[i + 2] == 'c')
{
i = i + 2;
while (s[i] != ';')
i++;
}
txt1 = txt1 + s[i + 1];
cont = i + 2;
}
else
flag = 1;
}
else // color format found
{
txt1 = txt1 + let;
// remove the color blocks
int found;
found = txt1.IndexOf("{");
if (found >= 0)
txt1 = txt1.Remove(found, 1);
found = txt1.IndexOf("}");
if (found >= 0)
txt1 = txt1.Remove(found, 1);
cont = 1 + cont;
}
if (flag == 1) // no color format found
{
txt1 = txt1 + let;
cont = 1 + cont;
flag = 0;
}
}
catch{}
}
textBox1.Text = txt1; // change the contents
}
private void button2_Click(object sender, EventArgs e)
{
}
public string Contents
{
get { return textBox1.Text; }
}
private void MTextForm_Load(object sender, EventArgs e)
{
textBox1.Focus();
textBox1.SelectAll(); // onLoad form select all text
}
private void MTextForm_Closed(object sender, FormClosedEventArgs e)
{
textBox1.Text = OrigMText;
}
private void OnClick_ApplyChanges(object sender, EventArgs e)
{
textBox1.Text = Contents;
OrigMText = Contents;
Close();
}
}
}
TO-DO:
1. Add one button to undo the mtext with the original contents (showing the color formatting)
2. Add the Uppercase, Lowercase, Uppercase first characters.