简体   繁体   中英

How can i do add textbox dynamically in UserControl and how to focus in Wpf..?

I have developed an invoice application and I have added a Usercontrol with more than 2 textbox to my WPF Application.i'm using for search option in itemcode for textbox1 and itemname showing textbox2 for getting value from database data is showing successfully,now i need to textbox how to add dynamically in Usercontrol and focus to next row,if filled the 1 row then focus to next row.please find the below code what i made the mistake in the application please help me friends.Thank you advance

**Usercontrol.xaml**

<UserControl x:Class="InvoiceApp.UserControltest"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="250" d:DesignWidth="560" VerticalAlignment="Top" Loaded="UserControl_Loaded">

    <Grid x:Name="ItemHolder"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="250" Width="560">

        <TextBox x:Name="txtbx1" Height="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="111" KeyDown="txtbx1_KeyDown" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtbx2" Height="20" HorizontalAlignment="Left" Margin="110,0,0,0"  VerticalAlignment="Top" Width="92" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtbx3" Height="20" HorizontalAlignment="Left" Margin="201,0,0,0" VerticalAlignment="Top" Width="85" BorderBrush="#FFC5C7CB"/>
        <TextBox x:Name="txtbx4" Height="20" HorizontalAlignment="Left" Margin="285,0,0,0" VerticalAlignment="Top" Width="85" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtbx5" Height="20" HorizontalAlignment="Left" Margin="369,0,0,0" VerticalAlignment="Top" Width="85" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtbx6" Height="20" HorizontalAlignment="Left" Margin="453,0,0,0" VerticalAlignment="Top" Width="78" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtId" Height="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="22" Margin="536,1,0,0" BorderBrush="#FFC5C7CB" />

    </Grid>

</UserControl>

**Usercontroltest.cs**



 private void txtbx1_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox txtbx = (TextBox)sender;
            if (e.Key == Key.Enter)
            {
                SqlCommand cmd = new SqlCommand("Select * from tbl_Tax WHERE Tax=@tax", InvoiceApp.FrmBill.con);
                cmd.Parameters.AddWithValue("@tax", txtbx.Text);
               // SqlDataReader dr = cmd.ExecuteReader();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        txtbx2.Text = ds.Tables[0].Rows[0]["Id"].ToString();
                       // txtbx3.Text = dr[3].ToString();
                        txtbx3.Text = ds.Tables[0].Rows[0]["Tax"].ToString();
                        txtbx4.Text = ds.Tables[0].Rows[0]["TaxAmount"].ToString();
                        txtbx5.Text = ds.Tables[0].Rows[0]["Description"].ToString();
                      //  txtbx6.Text = ds.Tables[0].Rows[0]["Description"].ToString();
                       // txtId.Text = ds.Tables[0].Rows[0]["Id"].ToString();
                        Items.Rows.Add(txtbx2.Text, txtbx3.Text, txtbx4.Text, txtbx5.Text);

                       // dataGrid1.ItemsSource = Items.DefaultView;
                        //dataGrid1.DataContext = Items;
                       // dattt();
                    }

                    //if (grdQustion.Rows.Count >= 1)
                    //{
                    //    int i = grdQustion.CurrentRow.Index + 1;
                    //    if (i >= -1 && i < grdQustion.Rows.Count)
                    //        grdQustion.CurrentCell = grdQustion.Rows[i].Cell[0];
                    //}
                }
                ds.Dispose();
            }

You can add elements as children to an existing element. First create it, then at anytime change its properties and add into the element, here a Grid.

TextBox newchild = new TextBox();
ItemHolder.Children.Add(newchild);

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