0

enter image description hereI have 3 forms, the main form (f1) has 2 buttons (Pain Reliever and Vitamins C) respectively. If you click the button Pain Reliever,it opens the form 2 alongside with the form 3 (child form). Form 2 (Pain Reliever) has another button for items like for exampleDiclofenac,Gebedol, etc.In the same manner, if you click the button Vitamins C,it opens Form 2 (Vitamins C) alongside with the form 3 (child form) and it has its own button items like Deplin,Vasculera,etc. Form 2 has a button Back which sends back to main form (f1).Form 3 has a datagridview values from either Pain Reliever or Vitamins C but not both. How can i keep the values of pain reliever if i click the button Back(f2) and adds items for Vitamins C ? if i click Back (f2) datagridview will be emptied. I want form 3 to accepts items from both Pain Reliever and Vitamins C.

I really appreciate any help. Thanks

// Here is the code from Form 1 --Load Button is responsible for Loading the buttons (Pain Reliever and Vitamins C) from my Database .

private void FrmMain_Load(object sender, EventArgs e)
{
    LoadButton();
} 


private void LoadButton()
{
        string str = "select [description],count(*) itemCount from dbo.ItmGrp_TBL group by [description]";

        using (SqlConnection stconnect = new SqlConnection(ConfigurationManager.ConnectionStrings["Pharmacy"].ConnectionString))
        {
            using (SqlCommand stc = new SqlCommand(str, stconnect))
            {
                stconnect.Open();

                SqlDataReader reader = stc.ExecuteReader();

                while (reader.Read())
                {
                    Button[] btn = new Button[ (int) reader["itemCount"] ];

                    for(int i=0; i < (int) reader["itemCount"];i++)
                    {
                        btn[i] = new Button() ;
                    }

                    int n = 0;

                    while (n < (int)reader["itemCount"])
                    {
                        btn[n].Width = 180;
                        btn[n].Height = 80;

                        btn[n].Dock = DockStyle.Fill;
                        btn[n].Text = reader["description"].ToString();

                        tlp.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
                        tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 50f));

                        tlp.Controls.Add(btn[n]);

                        btn[n].Click += new EventHandler(ClickButton);

                        n++;
                    }
                }
            }
        }
            
    }

    private void ClickButton(object sender,EventArgs e)
    {
        Button btn = (Button)sender;
        string str = btn.Text;

        frmMenuOrder fm = new frmMenuOrder();

        fm.PassValue = str;
        fm.Show();
        this.Hide();
    }

-- Here is the code for my Form 2 and the child form (Form 3)

    public string PassValue //getting the string values from frmMain
    {
        get { return GetStr; }
        set { GetStr = value; }

    }

   public frmOrder frm = new frmOrder() ;
    private void FrmMenuOrder_Load(object sender, EventArgs e)
    {

        frm.MdiParent = this;  *//Code to load the child form (form 3)*
        frm.Show();
                 
        btnBack.Dock = DockStyle.Bottom;
        LoadMenu();

    }

    private void LoadMenu()
    {
        string str = "select distinct(genericName),ItmGrp,onhand, code,price, count(*) RecCount from ItmPrice_TBL where ItmGrp = '" + GetStr +"'" +" group by GenericName,itmGrp,onhand,code,price  ";

        using (SqlConnection stconnect = new SqlConnection(ConfigurationManager.ConnectionStrings["Pharmacy"].ConnectionString))
        {
            using (SqlCommand stc = new SqlCommand(str, stconnect))
            {
               
                try
                {
                    stconnect.Open();

                    SqlDataReader reader = stc.ExecuteReader();
                    while (reader.Read())
                    {
                        Button[] btn = new Button[ (int)reader["RecCount"] ];

                        for(int i=0; i< (int)reader["RecCount"]; i++)
                        {
                            btn[i] = new Button();
                        }

                        int n = 0;

                        while (n < (int)reader["RecCount"])
                        {
                            btn[n].Left = 80;
                            btn[n].Height = 40;

                            btn[n].Dock = DockStyle.Fill;
                            btn[n].Font = new Font("Microsoft Sans Serif", 14F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
                            btn[n].Text = reader["genericName"].ToString();
                                                   
                            tlpMenu.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                            tlpMenu.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));

                            tlpMenu.Controls.Add(btn[n]);

                            ttStr = Convert.ToInt32(reader["onhand"]); //for tool tip string 

                            btn[n].MouseHover += new EventHandler(MouseOver) ;
                           
                            btn[n].Click += new EventHandler(ClickButton);                             

                            n++;
                        }
                    }

                }
                catch(Exception ex)
                {
                    MessageBox.Show("Unable to Load Menu." + ex.Message, "Load Menu Failed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }

    
  
    private void ClickButton(object sender , EventArgs e)
    {
        Button btn = (Button)sender;
        string str = btn.Text;


        string strSql = "select distinct(genericName),price,code from ItmPrice_TBL where genericName = '" + str + "'";


        using (SqlConnection stconnect = new SqlConnection(ConfigurationManager.ConnectionStrings["Pharmacy"].ConnectionString))
        {
            using (SqlCommand stc = new SqlCommand(strSql, stconnect))
            {

                try
                {
                    stconnect.Open();

                    SqlDataReader read = stc.ExecuteReader();

                    while (read.Read())
                    {
                        int row = frm.dgvw.Rows.Add();

                       frm.dgvw.Rows[row].Cells[0].Value = read["code"].ToString();
                        frm.dgvw.Rows[row].Cells[1].Value = read["genericName"].ToString();
                       frm.dgvw.Rows[row].Cells[2].Value = read["price"].ToString();
                    }
                }

                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + " " + ex.StackTrace, "error");
                }
            }
        }

        
    }
JohnG
  • 9,259
  • 2
  • 20
  • 29
Ronald
  • 1
  • 2
  • _”How can i keep the values of pain reliever if i click the button Back(f2) and adds items for Vitamins C ?”_ … ? … Have you considered “saving” this data as in a table or list? – JohnG Feb 17 '22 at 18:32
  • items from both pain reliever and vitamins C were coming from the table.... what I'm trying to imply here is, if I choose the button pain reliever all its items goes to form 3 datagrtdview and if I'm going to select vitamins C button it will add to the form 3 datagridview with an existing items from pain reliever... the scenario is just like a POS app... – Ronald Feb 18 '22 at 05:27
  • Well… there are many parts missing from your question and I am still not following what you are wanting to do. It sounds to me like you may need to pass the data to form 3 from whichever form is instantiating it. It is not 100% clear what form opens which form, it sounds like form 1 opens form 2 and form 3? How is the data stored in all this? How does form 3 get its data to fill the grid? When would this data change? You may consider some screen shots to clarify what you are asking. – JohnG Feb 18 '22 at 06:02
  • how can i attach screenshots here??? – Ronald Feb 18 '22 at 11:53
  • [edit] your question, then look towards the top where the formatting icons are. There should be one that looks like a little landscape and clicking that icon should allow you to drag n drop your images. If you post some images, then I suggest you also post the code that pertains to the images. Namely how form 3 is instantiated and how the other forms may be calling it. – JohnG Feb 18 '22 at 18:40
  • Hi JohnG, I've attached the screenshots from my original question...kindly browse through the screenshots with their respective filename on it to identify on which particular form. – Ronald Feb 20 '22 at 01:27
  • --- Code from Form 1 – Ronald Feb 20 '22 at 01:32
  • There are a lot of missing pieces to your code and it is difficult to reproduce. However, you state that … _”Form 2 has a button Back which sends back to main form (f1)”_ … and we can see this button in the second form… `btnBack.Dock = DockStyle.Bottom;` … HOWEVER, it’s Click event is missing in the code. Where is this “Back” buttons `Click` event code? – JohnG Feb 20 '22 at 04:06
  • I am very curious to see what happens in that code. Specifically… when the button is clicked on the first form to open the second form… the last line of code in that click event is… `this.Hide();` … ? … This will hide the first form, so… I am curious how the code in the second forms "Back" button click event is going to “un-hide” the first form. From what I can see…, calling `this.Hide()` is going to lose your reference to the first form and the form will basically be lost since nothing is going to be able to “un-hide” it. – JohnG Feb 20 '22 at 04:07
  • private void BtnBack_Click(object sender, EventArgs e) { frmMain fm = new frmMain(); fm.Show(); } – Ronald Feb 20 '22 at 13:04

0 Answers0