label not updating/refreshing

L

Leo Foederer

Hi,

I tried everything, but cannot accomplish this simple task:
I have two forms. On Form1 I have a Public Sub that pulls some data from a
database and subsequently updates a Label1.Text property. The sub on Form1
is called by a Button_Click event another form: Form2. I made a reference to
Form1 in Form2.
Label1.Text = NewData does not work. I cannot in any way get the label
reflect the new data. A Label.Refresh makes no difference. What is wrong?

Leo
 
J

Jeff Gaines

Hi,

I tried everything, but cannot accomplish this simple task:
I have two forms. On Form1 I have a Public Sub that pulls some data from a
database and subsequently updates a Label1.Text property. The sub on Form1
is called by a Button_Click event another form: Form2. I made a reference to
Form1 in Form2.
Label1.Text = NewData does not work. I cannot in any way get the label
reflect the new data. A Label.Refresh makes no difference. What is wrong?

Leo

Leo

Are you trying to get the data from an existing Form1 or
starting a new instance?

The following code in Form2 seems to work:

// Variable to hold Form1
Form1 m_frm1;

private void Form2_Load(object sender, System.EventArgs e)
{
m_frm1 = new Form1();
m_frm1.Show();
}

private void btnGet_Click(object sender, System.EventArgs e)
{
m_frm1.JGetData();
lblResult.Text = m_frm1.lblData.Text;
}


This means that Form2 is referring to a specific instance of
Form1 that is started when Form2 loads.

If you want to link up to an existing instance of Form1 you will
need a mechanism so that Form2 knows which instance of Form1 you
are referring to. e.g. if in your 'Get Data' function you refer
to a new Form1 then it is that form that will be called - not
any existing Form1 - and if you don't add a call to Form1.Show()
you won't see the form let alone the data:)

Does this help?

Do you want to expand a it on how and when you start the 2
forms?
 
L

Leo Foederer

Jeff,

Thank you for looking at my problem.
Indeed I want to refer to an already existing Form1.
The GetData function is in Form1. It is called from Form2.
My pseudo code:

Form1:
Private Sub GetSomeData
NewData = GetDataFrom Database
Label1.Text = NewData
End Sub

Form 2:
Public frm As New Form1
Private Sub Button1_Click
Call frm.GetSomeData
End Sub

I think there is a mistake in Form2 declaring a New Form2 there. But if I
leave the word New out, the Sub GetSomeData throws an exception.

Hope you have any ideas.

Leo
 
J

Jeff Gaines

Jeff,

Thank you for looking at my problem.
Indeed I want to refer to an already existing Form1.
The GetData function is in Form1. It is called from Form2.
My pseudo code:

Form1:
Private Sub GetSomeData
NewData = GetDataFrom Database
Label1.Text = NewData
End Sub

Form 2:
Public frm As New Form1
Private Sub Button1_Click
Call frm.GetSomeData
End Sub

I think there is a mistake in Form2 declaring a New Form2 there. But if I
leave the word New out, the Sub GetSomeData throws an exception.

Hope you have any ideas.

Leo

Leo

Do you open Form2 from a running Form1 or do you start it
independently?

If you start 2 from 1 you can do:

// In Form2:
// Module wide variable to hold Form1
Form1 m_frm1;

// Amend declaration of Form2:
public Form2(Form1 frm1)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

m_frm1 = frm1;
}

private void btnGet_Click(object sender, System.EventArgs e)
{
m_frm1.JGetData();
lblResult.Text = m_frm1.lblData.Text;
}

In this way Form2 'knows' which instance of Form1 to call.

You would start Form2 from Form1 as follows:

// This starts Form2 from Form1
private void btnStart2_Click(object sender, System.EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}


In fact your code is working but since you don't call frm.Show()
you can't see it.

If you want to start Form2 independently it's a whole new ball
game since somehow you need Form2 to be aware of which instance
of Form1 to call. You would need to use something like the API
call FindWindow to find Form1 then probably use SendMessage from
Form2 and then override WndProc in Form1 to pick up your message
- it could be done but it's much more complex.

You're actually very close I think - as long as you are happy to
start Form2 from Form1 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top