Forum guidelines
AuthorQuestion
Closing one form while keeping other form open
Posted By ullas K on 02 Dec 2012
Hi,
    I have a Main Form which is the parent form.I open Form1 inside the parent form and Form1 has a textbox textbox1.On textbox1_textchanged event i should open Form2 and  pass a value to it keeping Form1 opened.Depending upon that value the datagridview on Form2 gets populated.Up to here it is fine.Now what i need is when i leave the Textbox1(ie,textbox1_leave event) i should close the Form2.How can i acheive that

My code for Textchanged event in Form1 is

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            bookname = textBox1.Text;
            Form2 obj = new Form2();
            obj.Show();
            textBox1.Focus();
 
        }

Thanks in advance

Shaju

AuthorReply
Vulpes
  • 0
  • 0
accepted
Re: Closing one form while keeping other form open
Posted By Vulpes on 02 Dec 2012   Accepted Answer
I'd convert your Form2 variable, obj, into a field in the Form1 class, rather than a local variable, so that it can be accessed by the textBox1_Leave method. Your code can then be changed as follows:

  private Form2 obj = null;

  private void textBox1_TextChanged(object sender, EventArgs e)
  {
      bookname = textBox1.Text;
      if (obj == null)
      {
         obj = new Form2();
         obj.Show();
         textBox1.Leave += textBox1_Leave; // add handler
         textBox1.Focus();
      }
  }

  // add this manually, not via the designer
  private void textBox1_Leave(object sender, System.EventArgs e)
  {
      if (obj != null)
      {
         obj.Close();
         obj == null;
         textBox.Leave -= textBox1_Leave; // remove handler
      }
  }

Re: Closing one form while keeping other form open
Posted By ullas K on 03 Dec 2012  
Thank you very much Vulpes.The code is working fine.But now the problem is in the textbox1_Textchanged event i am passing a value to form2 and depending on that value the datagridview in form2 is populated(I am using LIKE in query).Now when i am entering a text in textbox1 all the records starting with that letter is populated in datagridview but it is not filtering as i enter more characters in textbox1. If i change the entir text entered in textbox1 and enter another text then the datagridview is showing old records ie, datagridview is not refreshing

In the Form_load event of form2 my code is as follows

string bookname = form1.bname();
  string catcode = form1.category();
  LibraryLib.Library obj = new LibraryLib.Library();
  DataTable dt;
  dt = obj.select("select Book_no, E_Name from Books where E_Name like'" + bookname.ToUpper() + "%' and Catcode='"+catcode+"'");
  dataGridView1.DataSource = dt;
  dataGridView1.Refresh();

Shaju

Re: Closing one form while keeping other form open
Posted By Vulpes on 03 Dec 2012  
I'd move that code out of Form2_Load and into a new method in Form2 - RefreshGrid, say.

internal void RefreshGrid()
{
  string bookname = form1.bname();
  string catcode = form1.category();
  LibraryLib.Library obj = new LibraryLib.Library();
  DataTable dt;
  dt = obj.select("select Book_no, E_Name from Books where E_Name like'" + bookname.ToUpper() + "%' and Catcode='"+catcode+"'");
  dataGridView1.DataSource = dt;
  dataGridView1.Refresh();
}

Your code in Form2_Load can then be reduced to calling this method:

private void Form2_Load(object sender, EventArgs e)
{
   RefreshGrid();
}

Finally, a change is needed to Form1_TextChanged to call the RefreshGrid method when Form2 is already open:

private void textBox1_TextChanged(object sender, EventArgs e)
{
   bookname = textBox1.Text;
   if (obj == null)
   {
      obj = new Form2();
      obj.Show();
      textBox1.Leave += textBox1_Leave; // add handler
      textBox1.Focus();
   }
   else
   {
      obj.RefreshGrid();
   } 
}

Re: Closing one form while keeping other form open
Posted By ullas K on 03 Dec 2012  
Thank you Vulpes very much for your support.Now it is working as i expected

Shaju

Re: Closing one form while keeping other form open
Posted By Pravin Ghadge on 03 Dec 2012  
Hi,
Form2 obj;

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            bookname = textBox1.Text;           
            obj = new Form2();
            obj.Show();
            textBox1.Focus();
 
        }



       private void textBox1_Leave(object sender, EventArgs e)
        {
            bookname = textBox1.Text;
           if (obj!=null)
          {         
            obj = new Form2();
            obj.Close();
         
            }
        }


Re: Closing one form while keeping other form open
Posted By ullas K on 04 Dec 2012  
 Sir,
      Can you please help me to solve out this.I have asked this question but have'nt received an answer yet.

i have a datagridview in Form1  with columns Sl.No , Book no,Book name ,issue date, due date.
when i Enter a book no in Book No column, then all the  details of the books
 are populated in other columns of datagridview and a new row is added  automatically (Datagridview changes to edit mode)where i can enter other book no in book no column of that row  to get its details displayed and so on .
For this to happen i am writing my code in Cell Value changed event of datagridview
My code is

private void dataGridViewEx1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                if (dataGridViewEx1.Rows[e.RowIndex].Cells[1].Value != null)
                {

                    con = db.open();
                    BookNumber = dataGridViewEx1.CurrentRow.Cells[1].Value.ToString();
                   
                    SqlDataAdapter adp = new SqlDataAdapter("SELECT E_NAME FROM BOOKS WHERE BOOK_NO='" + BookNumber.ToString() + "' ", con);

                    DataSet ds = new DataSet();
                    adp.Fill(ds, "s");
                   
                    if (ds.Tables["s"].Rows.Count >= 1)
                    {
                        dataGridViewEx1.CurrentRow.Cells[2].Value = ds.Tables["s"].Rows[0].ItemArray[0];
                       
                        SendKeys.Send("{TAB}");
                        SendKeys.Send("{TAB}");
                        SendKeys.Send("{TAB}");

                        dataGridViewEx1.CurrentRow.Cells[3].Value = DateTime.Now;
                        dataGridViewEx1.CurrentRow.Cells[4].Value = DateTime.Now.AddDays(holddays);
                    }
                    else
                    {
                        MessageBox.Show("No Such Book Exists.Please check the Book Number", "easyLib", MessageBoxButtons.OK);
                        SendKeys.Send("{left}");
                    }
                    db.close();
                }
            }

        }

This is working fine.

Now consider a situation where  i do not know the book No to enter.In such a situation  i have written code to display all the book No ,Book Name details in a datagridview
 in other form(Say FORM2)
. when i press F1 key in Book No column in Form1
 form2 is displayed. i select the required book no from Form2 and when i press enter that book no should be displayed in my Form1 book no column.I was able to do that.For that My code in Form1 is

internal void PopulateDGVEX1Row(string prodCode )
        {
         
                    con = db.open();

                    dataGridViewEx1.CurrentRow.Cells[1].Value = prodCode.ToString();

                    SqlDataAdapter adp = new SqlDataAdapter("SELECT E_NAME FROM BOOKS WHERE BOOK_NO='" + prodCode.ToString() + "' ", con);

                    DataSet ds = new DataSet();
                    adp.Fill(ds, "s");
                    if (ds.Tables["s"].Rows.Count >= 1)
                    {

                      //  dataGridViewEx1.CurrentRow.Cells[1].Value = ds.Tables["s"].Rows[0].ItemArray[0];
                        dataGridViewEx1.CurrentRow.Cells[2].Value = ds.Tables["s"].Rows[0].ItemArray[0];
                        SendKeys.Send("{TAB}");
                        SendKeys.Send("{TAB}");
                        SendKeys.Send("{TAB}");
                        dataGridViewEx1.CurrentRow.Cells[3].Value = DateTime.Now;
                        dataGridViewEx1.CurrentRow.Cells[4].Value = DateTime.Now.AddDays(holddays);
                    }
                    else
                    {
                        MessageBox.Show("No Such Book Exists.Please check the Book Number", "easyLib", MessageBoxButtons.OK);
                        SendKeys.Send("{left}");
                    }

                    dataGridViewEx1.RefreshEdit();

                    db.close();
               
        }

and in Form2 my code is

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                prodCode = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                f1.PopulateDGVEX1Row(prodCode);
              
                this.Dispose();
                this.Close();
             

            }
        }


I am getting the Book No in Form1 and its details in other Columns.But My problem is in this case a new row is not added in Form1 when i am getting book no from  Form2 unless i enter any value in a datagridview cell in form1.(When i enter a Book No in Column1 a new row is added and i was able to add another book no).How can i make it add a new row automatically to datagridview in Form1 when i get book no from other form(Form2).ie i should be able to work in both ways.If i know the book no it's ok but for those which i do not know i should fetch it from other form and should get a new row added in case if there is more book details to be entered

How can i do this .
Thanks in advance


Shaju

Re: Closing one form while keeping other form open
Posted By Vulpes on 04 Dec 2012  
Not sure but I'd try adding this line at the end of the PopulateDGVEX1Row method:

dataGridViewEx1.RowCount++;



Re: Closing one form while keeping other form open
Posted By ullas K on 04 Dec 2012  
Thank you Vulpes. New Row is adding but just above the current row.ie,The current row is pushed down

Shaju

Re: Closing one form while keeping other form open
Posted By Vulpes on 05 Dec 2012  
In that case, I'd try manipulating the Rows collection directly with:

dataGridViewEx1.Rows.Add();





Re: Closing one form while keeping other form open
Posted By ullas K on 05 Dec 2012  
Thanks Vulpes.That too won't work but i got a solution for it.I added  dataGridViewEx1.NotifyCurrentCellDirty(true) 
to my  PopulateDGVEX1Row method and after that i inserted a new row
Now it is working as desired

Once again thanks for your support

Shaju

Re: Closing one form while keeping other form open
Posted By Vulpes on 05 Dec 2012  
Glad to hear that you've solved it now :)
Re: Closing one form while keeping other form open
Posted By Phani Raj on 29 Dec 2012  
this.close();

Re: Closing one form while keeping other form open
Posted By ullas K on 12 Apr 2013  
Sir can you please help to sort out this

      I have a Datagridview with Columns Product Name, Unit Price,Qty ,Total and Status.Status column is invisible. The value of Status Column will be "Y" or "N" depending upon the Product which is fetched from the database.What i need is to get the Grand total of Products with Status="Y" in Textbox1 and the Total of Products with Status="N" in TextBox2. This is what i should get when i enter value in QTY column

Product       Unit Price     Qty   Total      Status

   A                   10             2       20.00      Y
   B                    20            1       20.00      N
   C                    50            2     100.00      Y
   D                   30             1       30.00      Y
   E                    25            1       25.00      N



                            TextBox1=150.00
                            TextBox2=  45.00

How to acheive this.Pls help and thanks in advance

Shaju

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.