0

I want to refresh a listview1 located in form1. I created code that fires a public void to refresh it or make it listview1.visible = false; nothing works except messagebox.show("test");

How do I make it work?

public void RefeshListView()
   {

       this.listView1.BeginUpdate();

       MessageBox.Show("s");//this shows! only:\ !?!?!?
       listView1.Visible = false;
       listView1.Height = 222;
       listView1.EndUpdate();
       listView2.Clear();

   }
2
  • What else are you showing??? Commented Jul 17, 2013 at 23:22
  • Which form is above code located in?
    – Mark Hall
    Commented Jul 18, 2013 at 1:16

2 Answers 2

0

I am a little unclear about what you are trying to do, but from your title it sounds like you want to influence the ListView on Form1 from Form2. I am assuming that Form2 is created from Form1. In your case you have two ways I can think of to do this, the first would be to create a Custom Constructor and pass the Form Instance to it or assign ownership when you show the form. The second would be to create a Custom Event on Form2 and subscribe to that in Form1.

The First Method:

in Form1 when you show Form2 use frm2.Show(this);
in Form2 when you want to call the refresh method use ((Form1)Parent).RefreshListView();

or create a Custom Constructor for Form2

Form1

public partial class Form1 : Form
{
    Form2 frm2;
    public Form1()
    {
        InitializeComponent();
        frm2 = new Form2(this);
        frm2.Show();
    }

    void frm2_RefreshList(object sender, EventArgs e)
    {
        RefreshListView();
    }

    public void RefreshListView()
    {

        this.listView1.BeginUpdate();

        MessageBox.Show("s");//this shows! only:\ !?!?!?
        listView1.Visible = false;
        listView1.Height = 222;
        listView1.EndUpdate();
        listView1.Clear();

    }
}

Form2

public partial class Form2 : Form
{
    Form1 frm1;
    public Form2()
    {
        InitializeComponent();
    }
    public Form2( Form frm)
    {
        InitializeComponent();
        frm1 = (Form1)frm;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        frm1.RefreshListView();
    }
}

The Second Method:

Form1

public partial class Form1 : Form
{
    Form2 frm2;
    public Form1()
    {
        InitializeComponent();
        frm2 = new Form2();
        frm2.RefreshList += new EventHandler(frm2_RefreshList);
        frm2.Show();
    }

    void frm2_RefreshList(object sender, EventArgs e)
    {
        RefreshListView();
    }

    public void RefreshListView()
    {

        this.listView1.BeginUpdate();

        MessageBox.Show("s");//this shows! only:\ !?!?!?
        listView1.Visible = false;
        listView1.Height = 222;
        listView1.EndUpdate();
        listView1.Clear();

    }
}

Form2

public partial class Form2 : Form
{
    public event EventHandler RefreshList;

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        RefreshList(this, EventArgs.Empty);
    }
}
0
0

Maybe need refresh.

public void RefeshListView()
{

   this.listView1.BeginUpdate();

   MessageBox.Show("s");//this shows! only:\ !?!?!?
   listView1.Visible = false;
   listView1.Height = 222;
   listView1.EndUpdate();
   listView2.Clear();
   listView2.refresh();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.