0

I am creating this simple video game ordering application. I have create an userControl named productControl. Code:

public partial class productControl : UserControl
{
    private string pName;
    private float pPrice;
    private string pDesc;
    private string pImgUrl;

    public productControl() => InitializeComponent();

    public string getName
    {
        get => pName;
        set
        {
            pName = value;
            productName.Text = pName;
        }
    }

    public float getPrice
    {
        get => pPrice;
        set
        {
            pPrice = value;
            productPrice.Text = pPrice.ToString("c");
        }
    }

    public string getDescription
    {
        get => pDesc;
        set
        {
            pDesc = value;
            descriptionText.Text = pDesc;
        }
    }

    public string getImage
    {
        get => pImgUrl;
        set
        {
            pImgUrl = value;
            prodImage.Load(pImgUrl);
            prodImage.SizeMode = PictureBoxSizeMode.StretchImage;
        }
    }

    public int getProdId { get; set; }

    private void buyButton_Click(object sender, EventArgs e)
    {
        MessageBox.Show(pPrice.ToString("c"));
        orderConfirmation o = new orderConfirmation();
        o.Show();
    }
}

And a Windows Forms named accountMain. Which retrieves data from SQL and calls the productControl to fill it up.

private string productRetriever = "SELECT prodId, prodName, prodPrice, prodImg, prodDesc FROM [dbo].[products]";

private void accountMain_Load(object sender, EventArgs e)
{
        createFunctions();
}
private void createFunctions()
{
    connection.Open();
    SqlCommand retProduct = new SqlCommand(productRetriever, connection);
    panel1.Controls.Clear()

    using (var reader = retProduct.ExecuteReader())
    {
        int count = 0;
        while(reader.Read())
        {
            p[count] = new productControl();
            p[count].Name = count.ToString();
            p[count].getProdId = (int)reader[0];
            p[count].getName   = (string)reader[1];
            p[count].getPrice  = (float)reader[2];
            p[count].getImage  = (string)reader[3];
            p[count].getDescription = (string)reader[4];
            panel1.Controls.Add(p[count]);
            count++;
        }
    }
    connection.Close();
}

Above I am using a while loop to populate the panel1 with productControl inside of the accountMain Form. How can I get userControl to hide the accountForm and show another form (passing the values into the new form like title and price) when I click the "Buy" button which is inside the userControl.

Also I am new to implementing SQL into C# is it the correct way to populate the panel1 in accountMain using executeReader() or is there a better approach of retrieving data from SQL and showing it in userControl?

julealgon
  • 7,072
  • 3
  • 32
  • 77
Anmol Brar
  • 15
  • 3
  • You need to use an instance of the form class. See my two form project : https://stackoverflow.com/questions/34975508/reach-control-from-another-page-asp-net – jdweng Jun 30 '18 at 23:58
  • 2
    It is never the job of a little soldier like UserControl to demand what the main window should look like. Raise an event, the big dog can respond to it. https://stackoverflow.com/questions/3486377/how-to-add-an-event-to-a-usercontrol-in-c – Hans Passant Jul 01 '18 at 00:15
  • Why are you naming everything with a lowercase p? Also, please don't name your properties with "get". Does `getFoo = "foo"` make any sense? No, it doesn't. `Foo = "foo"` makes much more sense. `getFoo` sounds more like a function or a method than a variable and it would be constantly throwing me off as I'd be doing `getFoo()` and I'd be getting errors. – AustinWBryan Jul 01 '18 at 01:58
  • Is this WinForms, ASP.NET WebForms, or WPF? – Dai Jul 01 '18 at 02:01

0 Answers0