简体   繁体   中英

Inserting a text box dynamically

Till now I had only to display some check boxes on a stack panel.Thus I was proceeding as

to build the content of the check box.

 public void addAlarmcl(string argComparator, string value, string argColor)
        {
            if ((!string.IsNullOrEmpty(argComparator)) && (!string.IsNullOrEmpty(value)) && (!string.IsNullOrEmpty(argColor)))
            {
                //hexadecimal of color chosen
                Color color = (Color)ColorConverter.ConvertFromString(argColor);
                //this is to be displayed in stackpenl
                string TheOneYouJustBuilt = "If statistics are /" + argComparator + "/" + value + "/ ,notify by /" + color + "/.";
                addToStackPanel(TheOneYouJustBuilt);
            }

        }  

and then added it to the stack panel as

//this will display the built alarms on the stackpanel
        public void addToStackPanel(string argBuiltAlarm)
        { 
            CheckBox checkQueries = new CheckBox() { Content = argBuiltAlarm };
            stackPanel1.Children.Add(checkQueries);
            AlarmList.Add(checkQueries);
            //storing the built queries alongside FOR DELETION (OR OTHER REFERENCES)
            AlarmThatIsBeingDisplayed.Add(argBuiltAlarm.ToString());
        }

But now I need a textbox at the end of the checkboxes content.How to modify my codes to add that textbox ?

I know that to declare a new text box we do

 var textbox = new TextBox();
 //thn we set its properties and all

But How do I append it just after the checkboxes content ?

Try this code

public void addToStackPanel(string argBuiltAlarm)
{
   //creating a stackpanel with orientation horizontal
   StackPanel stackPanel=new StackPanel
        {
           Orientation =System.Windows.Controls.Orientation.Horizontal
        };

   TextBox textBox=new TextBox{ Text =  "your text"};

   CheckBox checkQueries = new CheckBox() { Content = argBuiltAlarm };

   stackPanel.Children.Add(checkQueries);
   stackPanel.Children.Add(textBox);

   //adding the stackpanel containing both checkbox & textbox to the stackPanel1
   stackPanel1.Children.Add(stackPanel);

   //remaining codes here

 }

You need to wrap checkbox and textbox in some container: StackPanel or Grid:

var sp = new StackPanel() 
{
    Orientation = Orientation.Horizontal
};
sp.Children.Add(checkQueries);
sp.Children.Add(textbox);
stackPanel1.Children.Add(sp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM