Sreenivasa Chary
Sreenivasa Chary

Reputation: 1

VB.net 2010 - Getting child form initial values from main form

I am trying out a little application in VB.Net 2010 and am facing a small problem. My app has two forms - one main and one secondary. The main form has a progress bar. And so does the secondary form.When the app starts, only the main form is visible. Here i set the progress bar value to some percentage. Next, i open the child form, and i want the progress bar to reflect the same value as that of the main form. How can i achieve this ? I tried setting the progress bar value on the child form using the value from the main form, in the load and shown methods.But it does not work, the child form's progress bar comes up with zero value. Any ideas to do this are welcome..

Upvotes: 0

Views: 553

Answers (2)

S.Serpooshan
S.Serpooshan

Reputation: 7450

How did you open the second form and how did you get the progressbar value of main form? you need a public method/property to get access to main form controls. for example, you can do what you need as following:

in main form (MainForm):

    private void btnOpenChildForm_Click(object sender, EventArgs e)
    {
        ChildForm f = new ChildForm();
        f.Show(this);
    }

    public ProgressBar GetMyProgressBar() { return progressBar1; }

and in second form (ChildForm):

    private void ChildForm_Load(object sender, EventArgs e)
    {
        MainForm parent = this.Owner as MainForm;
        progressBar1.Value = parent.GetMyProgressBar().Value;
    }

if you want, i can send you a VB.Net version

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54562

This example is using a Property to show you how to change the second Forms ProgressBar when you first show it and also how to update it from Form1. With out seeing your code I have no idea what you are doing wrong, but in most of the questions I have seen especially with vb.net the problem is that you are addressing 2 different instances of your second form so the changes never show up where you are expecting them to.

Form1

Public Class Form1
    Dim frm2 As Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ProgressBar1.Value += 1
        If Not IsNothing(frm2) Then
            frm2.SetProgress = ProgressBar1.Value
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        frm2 = New Form2
        frm2.SetProgress = ProgressBar1.Value
        frm2.Show()
    End Sub
End Class

Form2

Public Class Form2
    Public Property SetProgress As Integer
        Get
            Return ProgressBar1.Value
        End Get
        Set(value As Integer)
            ProgressBar1.Value = value
        End Set
    End Property
End Class

Upvotes: 0

Related Questions