Click here to Skip to main content
14,920,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working in .Net Framework Compact Edition to design pocket pc application. I have 2 forms named frmData, frmMain. I have list view in frmMain. In list view I have 4 columns. ID, Room, Location, Time. I am getting value of ID, Room, location and time on frmData. I want to get that data and put it into listview of frmMain. On frmData there might be more than 1 records to be saved in list. When I click on btnUpdate on frmData, frmData will get closed and frmMain gets activated.

What I have tried:

This code is for frmData
C#
public static ListViewItem item = new ListViewItem();

private void btnSave_Click(object sender, EventArgs e)
{
    DateTime dt = DateTime.Now.ToUniversalTime();
    string format = dt.ToString("yyyy-MM-dd HH:mm:ss");
    string[] row = { txtEPC.Text, cmbRoom.Text, txtShelf.Text, format };
    
    item.Text = txtEPC.Text;
    item.SubItems.Add(cmbRoom.Text);
    item.SubItems.Add(txtShelf.Text);
    item.SubItems.Add(format);
}

private void btnUpdateMain_Click(object sender, EventArgs e)
{
    this.close();
}


This code is for frmMain

C#
private void frmMain_Activated(object sender, EventArgs e)
{
    try
    {
        listView1.Items.Clear();
        listView1.Items.Add(frmData.item);
    }
    catch (Exception)
    {
                    
    }   
}




I also tried with this one
frmMain objform1= new frmMain();
private void btnSave_Click(object sender, EventArgs e)
{
    DateTime dt = DateTime.Now.ToUniversalTime();
    string format = dt.ToString("yyyy-MM-dd HH:mm:ss");
    string[] row = { txtEPC.Text, cmbRoom.Text, txtShelf.Text, format };
    listViewItem lst = new ListViewItem;
    objform1.listview1.Items.add(lst); //changed modifier of listview1 to public
    
    item.Text = txtEPC.Text;
    item.SubItems.Add(cmbRoom.Text);
    item.SubItems.Add(txtShelf.Text);
    item.SubItems.Add(format);
}
Posted
Updated 4-Apr-18 16:46pm
v2

1 solution

Based on what presented here, I think this is what you looking for. On button update click, close frmData, show frmMain and populate the ListView with the item from frmData

C#
private void btnUpdateMain_Click(object sender, EventArgs e)
{
    this.Hide();
    frmMain frm2 = new frmMain();
    frm2.Closed += (s, args) => this.Close();
    frm2.Show();
}


or
C#
private void btnUpdateMain_Click(object sender, EventArgs e)
{
     this.Hide();
    frmMain frm2 = new frmMain();
    frm2.ShowDialog();
    this.Close();
}



Source: winforms - c# open a new form then close the current form? - Stack Overflow[^]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900