0

I am facing a weird problem

I have 3 forms: MainForm, Form1, Form2
MainForm has 1 Panel: Panel1
Form1 has 1 Label: NameLbl and Button: ChangeBtn
Form2 has 1 textbox: NameTxt and Button: SaveBtn

I used the following code to open form1 inside Panel1 in mainform

 Panel1.Controls.Clear()
 Dim FormInPanel As New Form1()
 FormInPanel.TopLevel = False
 Panel1.Controls.Add(FormInPanel)
 FormInPanel.Show()

On ChangeBtn.Click Form2 opens as showdialog
I want NameLbl.text to change to NameLbl.text when SaveBtn is clicked But normal code doesnt work.

Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
    Form1.NameLbl.text=NameTxt.text
End Sub

What Should I do? Any suggestions? Given that i need to open the forms in panels for certain reasons.
Please keep in mind that this is just an example. I have multiple controls in Form1 which i want to change on form2.SaveBtn.click

I have also tried this but it does nothing

Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click

    For Each c As Control In MainForm.Panel1.Controls(0).Controls
        If c.Name="NameLbl" Then
            c.Text = NameTxt.Text
        End If
    Next
End Sub

Please Somebody tell me how it do it!

5 Answers5

0

Form2 doesn't seem to have any connection back to Form1 or MainForm. You'd need to raise an event from Form2 which MainForm handles or another class handles and can pass to MainForm

Edit:

Sorry, I've just seen how you're calling Form2. There are lots of ways to get a value back from Form2 after calling ShowDialog(). One is to create a property called Result and check Result if ShowDialog() == DialogResult.OK. Something like the following.

Public Class Form2
    Inherits System.Windows.Forms.Form
    Public Property Result() As String
        Get
            Return m_Result
        End Get
        Set
            m_Result = Value
        End Set
    End Property
    Private m_Result As String
End Class

Public Class Form1
    Inherits System.Windows.Forms.Form
    Public ChangeBtn As Button
    Public NameLbl As Label
    Public Sub New()
        Me.ChangeBtn = New Button()
        AddHandler Me.ChangeBtn.Click, AddressOf ChangeBtn_Click
        Me.NameLbl = New Label()
    End Sub

    Private Sub ChangeBtn_Click(sender As Object, e As EventArgs)
        Dim form As New Form2()
        Dim dr = New form.ShowDialog()
        If dr = DialogResult.OK Then
            Me.NameLbl.Text = form.Result
        End If
    End Sub
End Class

I'd like to add that if you plan on growing this application much larger you'll run into issues with maintenance. Look into some patterns for dealing with UI logic like MVC, MVP, MVVM if you're interested.

Ciaran
  • 1,878
  • 5
  • 18
  • 31
0

you can try this code:

Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
   panel1.Controls(0).NameLbl.text=NameTxt.text '"0" is the index of forminpanel in panel1,maybe it need to change.
End Sub
Experdot
  • 1
  • 2
0

Form1 is contained in Panel1, so you can not access it via

  Form1.

Only MainForm is visible from Form2:

Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
    For Each c As Control In MainForm.Panel1.Controls(0).Controls
        If TypeOf c Is TextBox Then
            c.Text = NameTxt.Text
        End If
    Next
End Sub
0

Try this:

For Each form1 As Form1 In MainForm.OwnedForms.OfType(Of Form1)
    Form1.NameLbl.text = NameTxt.text
Next
cramopy
  • 3,459
  • 6
  • 28
  • 42
0

I've faced same issue and I've fixed with this

     Dim f As FormInPanel
     f = Form.Panel1.Controls(0)
     f.transection = True
     f.NameLbl.text=NameTxt.text
Phoenix
  • 1
  • 1