Question Control not working in a new child form as expected

Joined
Dec 8, 2014
Messages
5
Programming Experience
1-3
I am using the below code to open a new form:

VB.NET:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  

Dim NewMDIChild As New mdgeneral
        'Set the Parent Form of the Child window.  
NewMDIChild.MdiParent = Me      

  'Display the new form.       
 NewMDIChild.Show()      
  NewMDIChild.Height = 375      
  NewMDIChild.Width = 550       
 NewMDIChild.Location = New Point(150, 0)     
 NewMDIChild.FormBorderStyle = Windows.Forms.FormBorderStyle.None 
 End Sub

All the text fields in the form 'mdgeneral' show the output as expected when the form loads. But, when I click on a button inside the same form it does not do anything but it should ideally execute a code.

Below is the code for click event of the button which is not working in the new child form:

VB.NET:
Private Sub Button1_Click(sender As Object, e As EventArgs)

sb.Append("$upn= get-mailbox -Identity " + mailboxpop.TextBox1.Text + " | select *userprincipalname* ")   
sb.Append(vbNewLine)        sb.Append("Set-Msoluser -FirstName " + TextBox6.Text + " | where {$_.UserPrincipalName -eq $upn.UserPrincipalName}  ")
sb.AppendLine()

Dim strFilePath = (mydocpath & "\save.txt") 

If System.IO.File.Exists(strFilePath) Then         
System.IO.File.Delete(strFilePath)        
End If        

Using outfile As New StreamWriter(mydocpath & "\save.txt")          
outfile.Write(sb.ToString())      
End Using

        strFilePath = (mydocpath & "\save.ps1")       
 If System.IO.File.Exists(strFilePath) Then         
   System.IO.File.Delete(strFilePath)     
   End If
        System.IO.File.Move((mydocpath & "\save.txt"), (mydocpath & "\save.ps1"))

        Try            TextBox13.Text = Runcmd(mydocpath & "\save.ps1")

            MsgBox("Done!")

        Catch            MsgBox("There was an error updating the information for this mailbox")      
  End Try

    End Sub

Am I missing something really important? Any help to fix this will be appreciated. Thanks.
 
Last edited:
Yes, you are missing something really important. Specifically, you're missing the bit that actually makes the method an event handler. Have a look at your for method:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
and now look at your second method:
Private Sub Button1_Click(sender As Object, e As EventArgs)
Do you notice something different between the two?

By the way, take a look at this code:
  'Display the new form.       
 NewMDIChild.Show()      
  NewMDIChild.Height = 375      
  NewMDIChild.Width = 550       
 NewMDIChild.Location = New Point(150, 0)     
 NewMDIChild.FormBorderStyle = Windows.Forms.FormBorderStyle.None 
I have two things to say about that. Firstly, why display the form first and then configure it? Logic dictates that you configure it first and then display it, so that it's already in the desired state when it's displayed.

Secondly, why set the Height and Width separately rather than setting the Size but then set the Location rather than setting the Top and Left separately? There is no logical reason to do it like that. While that code is not wrong, in consistency is always bad unless there is a good reason for it. If it is logical to set the Location with a new Point then it's logical to set the Size with a new Size. If it's logical to set the Height and Width separately then it's logical to set the Top and Left separately. Pick one or the other and stick to it.
 

Latest posts

Back
Top