Reputation: 117
I created a class, and want to use the constructor Rtb(),
public class Rtb
{
public RichTextBox newRTB;
public Rtb()
{
newRTB = new RichTextBox();
newRTB.IsReadOnly = true;
newRTB.MouseDoubleClick += new MouseButtonEventHandler(newRTB_MouseDoubleClick);
}
... ...
Then I use Rtb() in below code which is to add a richtextbox after click the menu,
List<Rtb> rtba = new List<Rtb>();
private void menuBox_Click(object sender, RoutedEventArgs e)
{
BlockUIContainer newBlockUC = new BlockUIContainer();
newBlockUC.Margin = new Thickness(50, 10, 50,10);
mainWin.Document.Blocks.Add(newBlockUC); // add to mainWin, which is a big richtextbox
rtba.Add(new Rtb()); // add to list
Rtb newRichTB = new Rtb();
newBlockUC.Child = newRichTB.newRTB; //add to newBlockUC
}
My problem is, how to assign (serialized) name to each new created richtextbox? (for example, box1, box2, box3...)
And if the user delete one richtextbox manually at runtime(for example, use backspace key to delete box2), how to change the name dynamically? (then box3 becomes box2, box4 becomes box3...)
Upvotes: 0
Views: 76
Reputation: 374
You could add a static property to your Rtb class that is used by the Rtb constructor to issue a unique name to each RichTextBox that gets added. (I am not sure why you would feel the need to "rename" them if one got removed without additional context to your question.)
public class Rtb
{
private static int _NextID = -1;
public static int NextID
{
get
{
_NextID++;
return _NextID;
}
}
public RichTextBox newRTB;
public Rtb()
{
newRTB = new RichTextBox();
newRTB.IsReadOnly = true;
newRTB.MouseDoubleClick += new MouseButtonEventHandler(newRTB_MouseDoubleClick);
newRTB.Name = "Box" + NextID.ToString();
}
}
Upvotes: 1
Reputation: 2185
RichTextBox
has a Name
property which can be set programmatically. To expose it, you could add this property to your Rtb
class.
public string Name
{
get { return newRTB.Name; }
set { newRTB.Name = value; }
}
Upvotes: 0