Here is a short code snippet that creates an MLeader based on an existing MLeaderStyle. For the MLeader's text to correctly follow any changes made to the MLeaderStyle, it is necessary to clone the MLeaderStyle.DefaultText and use it as the MLeader's text. Creating a new MText without relying on the DefaultText can cause the text to not reflect any later changes that are made to the MLeaderStyle.
Thanks to Xin Xu from the AutoCAD engineering team for providing this tip.
[CommandMethod("MLTest")] public void MLTestMethod() { Editor ed = default(Editor); ed = Application.DocumentManager.MdiActiveDocument.Editor; PromptPointResult ppr1 = default(PromptPointResult); ppr1 = ed.GetPoint( new PromptPointOptions("Select start point")); if (ppr1.Status != PromptStatus.OK) return; PromptPointResult ppr2 = default(PromptPointResult); ppr2 = ed.GetPoint( new PromptPointOptions("Select end point")); if (ppr2.Status != PromptStatus.OK) return; Database db = HostApplicationServices.WorkingDatabase; ObjectId myMLeaderId = ObjectId.Null; using (Transaction trans = db.TransactionManager.StartTransaction()) { ObjectId myleaderStyleId = db.MLeaderstyle; MLeaderStyle mlstyle = trans.GetObject(myleaderStyleId, OpenMode.ForRead) as MLeaderStyle; using (MLeader myMLeader = new MLeader()) { myMLeader.SetDatabaseDefaults(); myMLeader.PostMLeaderToDb(db); myMLeaderId = myMLeader.ObjectId; myMLeader.MLeaderStyle = db.MLeaderstyle; int leaderIndex = myMLeader.AddLeader(); int leaderLineIndex = myMLeader.AddLeaderLine(leaderIndex); myMLeader.AddFirstVertex( leaderLineIndex, ppr1.Value); myMLeader.AddLastVertex( leaderLineIndex, ppr2.Value); MText myMText = mlstyle.DefaultMText.Clone() as MText; if (myMText != null) { myMText.SetContentsRtf("Autodesk"); myMLeader.MText = myMText; } } trans.Commit(); } }