Hi All,

I am developing an windows application in .Net 2005 with C# coding. One part of my work, the functionality is

1. Use USB barcode scanner.
2. When something is scanned, it records the item number and automatically pulls the item price from the database.
3. After an item is scanned( In textchanged event), it should automatically add a new product field ( dynamically create multiple textbox) and pulls the data.
4. If i changed the quantity field, it should multiply the price and place into appropriate textbox.

Eg:
ItemNumber Quantity Price

0123455678 1 102

Quantity field should be editable, If i changed into 2, price will be 204.

I struck in the final step, I could not place the the multiplied price in the appropriate textbox. ( You will be clear when check with my code).
Because i am not give the unique id to the dynamically created textbox, So that i cannot place into that. Actually my code is not worked in that.

Below is my code,

public partial class Form1 : Form
    {
        int x = 8;
        int y = 8;

        int a = 185;
        int k = 560;
        int s = 653;
        double totalprice = 0;

        TextBox txtpdtprice1 = new TextBox();
        string pricefrmdb = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TextBox txtbarcode1 = new TextBox();
            txtbarcode1.Size = new Size(157, 23);
            txtbarcode1.Location = new Point(x, y);
            txtbarcode1.Text = "";
            txtbarcode1.TextChanged += new EventHandler(this.TextBox_TextChanged);

            TextBox txtquantity1 = new TextBox();
            txtquantity1.Size = new Size(87, 23);
            txtquantity1.Location = new Point(k, y);
            txtquantity1.Name = "tquantity1";
            txtquantity1.Text = "1";            

            TextBox txtprice1 = new TextBox();
            txtprice1.Size = new Size(87, 23);
            txtprice1.Location = new Point(s, y);
            txtprice1.Text = "";
            txtprice1.Name = "tprice1";
            txtpdtprice1 = txtprice1;

            pnl1.Controls.Add(txtbarcode1);
            pnl1.Controls.Add(txtquantity1);
            pnl1.Controls.Add(txtprice1);

            txtquantity1.TextChanged += new EventHandler(this.QuantityTextBox_TextChanged);
            
        }

        private void TextBox_TextChanged(object sender, System.EventArgs e)
        {
            TextBox oldBarcodeTextBox = (sender as TextBox);
            int item = Convert.ToInt16(oldBarcodeTextBox.Text);

            Getdata(item, txtpdtprice1);
            findtotal(txtpdtprice1);

            TextBox newBarcodeTextBox = new TextBox();
            newBarcodeTextBox.Location = new Point(oldBarcodeTextBox.Location.X, oldBarcodeTextBox.Location.Y + oldBarcodeTextBox.Height + 70);
            newBarcodeTextBox.Size = newBarcodeTextBox.Size;
            newBarcodeTextBox.TextChanged += new EventHandler(TextBox_TextChanged);

            TextBox newTextqty = new TextBox();
            newTextqty.Size = new Size(87, 23);
            newTextqty.Location = new Point(newBarcodeTextBox.Location.X + 553, newBarcodeTextBox.Location.Y);
            newTextqty.Text = "1";
            newTextqty.TextChanged += new EventHandler(this.QuantityTextBox_TextChanged);

            TextBox newTextprice = new TextBox();
            newTextprice.Size = new Size(87, 23);
            newTextprice.Location = new Point(newBarcodeTextBox.Location.X + 646, newBarcodeTextBox.Location.Y);
            newTextprice.Text = "";
            txtpdtprice1 = newTextprice;

            this.pnl1.Controls.Add(newBarcodeTextBox);
            this.pnl1.Controls.Add(newTextqty);
            this.pnl1.Controls.Add(newTextprice);

        }

        private void QuantityTextBox_TextChanged(object sender, System.EventArgs e)
        {
            TextBox oldQtyTextBox = (sender as TextBox);
            int noofqty = Convert.ToInt16(oldQtyTextBox.Text);
            if (noofqty > 1)
            {
                findprice(noofqty, pricefrmdb);
            }

        }
        public void findprice(int qty,string price)
        {
            double val = Convert.ToDouble(price);
            double total;
            total = qty * val;
            txtpdtprice1.Text = Convert.ToString(total);
        }
public void Getdata(int pdtnumber, TextBox price)
        {
            try
            {
                string constr = "provider=MicroSoft.jet.OLEDB.4.0;" + "Data Source=...\\db1.mdb;" + "persist Security Info=false";
                con.ConnectionString = constr;
                con.Open();
                string strquery = "SELECT itemnumber, price FROM item where itemnumber = " + pdtnumber + "";
                OleDbDataAdapter da = new OleDbDataAdapter(strquery, con);
                DataSet ds = new DataSet();
                da.Fill(ds, "item");
                DataTable dt = ds.Tables["item"];
                price.Text = dt.Rows[0][2].ToString();
                pricefrmdb = price.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
        }

Can anyone give me the solution and also Can give me the suggestion for how to integrate the USB barcode scanner in my application???

Kindly tell me if any fault in this thread too....

Have A Nice Day :)

If you can, look at using a UserControl. You can build your layout and tie all your events together inside the control.
To access the internal data from the outside, use public properties.
To publish internal events and data, use delegates and events.

If you cant or don't want to use a usercontrol, you can do the same thing with a Class but its just a little more work.

Finally, if you want to make it easy to iterate and clean up after yourself, you can maintain a list of these in a generic list. Of course you can do the same thing by iterating the controls on the panel, but the generic list allows you to search, compare, sort, etc.

Hi

Sorry i cannot understand your reply...this is my first windows application. Actually i cannot get it clearly. Can give me a sample?

Or Can't make it use of my code??

Thanks

In your solution window, right click on the project, select Add Item, from the Dialog box select UserControl. When it displays, stretch it to 450,22.

On that uc, put 3 textboxes. If you want to leave room for labels, make it taller than 22.

Then add these three accessors

public string Txtbx1
        {
            get { return txtbx1.Text; }
            set { txtbx1.Text = value; }
        }

        public string Txtbx2
        {
            get { return txtbx2.Text; }
            set { txtbx2.Text = value; }
        }

        public string Txtbx3
        {
            get { return txtbx3.Text; }
            set { txtbx3.Text = value; }
        }

now add the rest of the logic you need to complete computations between these textboxes

next you want to have logic to add to your main form as needed. Here I used a button, but you appear to have some logic you want to perform for adding

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private List<UCTxtBx> txtLst;
        public Form1()
        {
            InitializeComponent();
            txtLst = new List<UCTxtBx>();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UCTxtBx txtrw = new UCTxtBx();
            txtrw.Txtbx1 = "somevalue";

            // assign the pnl as parent
            txtrw.Parent = pnl;
            // position it on the panel
            txtrw.Top = (txtLst.Count * txtrw.Height);
            // now add it to the list for easy reference in the future
            txtLst.Add(txtrw);  

        }
    }
}

Finally on the main form, finish with the logic that finds the price, also, you can set the price this way

txtLst.Txtbx2 = "0.00";

goodluck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.