Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to assign names to new objects dynamically at runtime?

$
0
0

I created a class, and want to use the constructor Rtb(),

public class Rtb:RichTextBox
    {
        public RichTextBox newRTB;

        public Rtb()
        {
            newRTB = new RichTextBox();
            newRTB.IsReadOnly = true;
            newRTB.MouseDoubleClick += new MouseButtonEventHandler(newRTB_MouseDoubleClick);
        }
    ...  ...

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 name or serialized name to each new created richtextbox? (for example, box1, box2, box3...)

And if the user delete one richtextbox manually (for example, use backspace key to delete box2), how to change the name dynamically? (then box3 becomes box2, box4 becomes box3...)


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>