Following is the code I used to call the child window:
Dim c As New New_Contact()
c.Title = "New Contact Details"
AddHandler c.Closed, AddressOf NewContactClosed
c.Show()
Following is the XAML for the child window:
<controls:ChildWindow xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="AtomHelpers.New_Contact"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Width="662" Height="363"
Title="Add New Contact" FontSize="14">
<Grid x:Name="LayoutRoot" Margin="2" Height="320">
<Grid Margin="0,0,0,0">
<sdk:Label Content="Name" Height="18" HorizontalAlignment="Left" Margin="6,12,0,0" Name="Label1" VerticalAlignment="Top" Width="120" FontSize="14"/>
<sdk:Label Content="Type" Height="18" HorizontalAlignment="Left" Margin="6,43,0,0" Name="Label6" VerticalAlignment="Top" Width="120" FontSize="14" />
<sdk:Label Content="Reference" Height="18" HorizontalAlignment="Left" Margin="6,128,0,0" Name="Label7" VerticalAlignment="Top" Width="76" FontSize="14" />
<sdk:Label Content="Department" Height="18" HorizontalAlignment="Left" Margin="6,160,0,0" Name="Label8" VerticalAlignment="Top" Width="97" FontSize="14"/>
<sdk:Label Content="Office" Height="18" HorizontalAlignment="Left" Margin="6,190,0,0" Name="Label9" VerticalAlignment="Top" Width="120" FontSize="14" />
<sdk:Label Content="Profession" Height="18" HorizontalAlignment="Left" Margin="6,215,0,0" Name="Label10" VerticalAlignment="Top" Width="120" FontSize="14" />
<sdk:Label Content="Birthdate" Height="18" HorizontalAlignment="Left" Margin="6,248,0,0" Name="Label11" VerticalAlignment="Top" Width="120" FontSize="14" />
<sdk:Label Content="Primary Address" Height="18" HorizontalAlignment="Left" Margin="302,11,0,0" Name="Label12" VerticalAlignment="Top" Width="133" FontSize="14" />
<sdk:Label Content="Primary Numbers" Height="18" HorizontalAlignment="Left" Margin="302,131,0,0" Name="Label13" VerticalAlignment="Top" Width="133" FontSize="14" />
<sdk:Label Content="Allow Group" Height="18" HorizontalAlignment="Left" Margin="305,248,0,0" Name="Label14" VerticalAlignment="Top" Width="120" FontSize="14" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="95,8,0,0" Name="Namee" VerticalAlignment="Top" Width="163" MaxLength="10" IsReadOnly="True" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="95,156,0,0" Name="Department" VerticalAlignment="Top" Width="192" TabIndex="4" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="95,186,0,0" Name="Office" VerticalAlignment="Top" Width="192" TabIndex="5" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="95,215,0,0" Name="Profession" VerticalAlignment="Top" Width="192" TabIndex="6" />
<sdk:DatePicker Height="23" HorizontalAlignment="Left" Margin="95,244,0,0" Name="DatePicker1" VerticalAlignment="Top" Width="192" TabIndex="7" />
<TextBlock Height="114" HorizontalAlignment="Left" Margin="432,12,0,0" Name="AddressTextBlock" Text="" VerticalAlignment="Top" Width="172" TextWrapping="Wrap" />
<TextBlock Height="114" HorizontalAlignment="Left" Margin="432,131,0,0" Name="NumberTextBlock" Text="" VerticalAlignment="Top" Width="172" TextWrapping="Wrap" />
<CheckBox Height="16" HorizontalAlignment="Left" Margin="405,244,0,0" Name="GroupCheckBox" VerticalAlignment="Top" TabIndex="10" />
<Button Content="E" Height="23" HorizontalAlignment="Left" Margin="264,7,0,0" Name="NameButton1" VerticalAlignment="Top" Width="23" TabIndex="0" />
<ScrollViewer Height="69" HorizontalAlignment="Left" Margin="95,43,0,0" VerticalAlignment="Top" Width="163" TabIndex="1">
<StackPanel HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" />
</ScrollViewer>
<Button Content="E" Height="23" HorizontalAlignment="Left" Margin="264,39,0,0" Name="Typebutton2" VerticalAlignment="Top" Width="23" TabIndex="2" />
<Button Content="E" Height="23" HorizontalAlignment="Left" Margin="608,11,0,0" Name="AddressButton" VerticalAlignment="Top" Width="23" TabIndex="8" />
<Button Content="E" Height="23" HorizontalAlignment="Left" Margin="608,127,0,0" Name="NumberButton" VerticalAlignment="Top" Width="23" TabIndex="9" />
<CheckBox Height="16" HorizontalAlignment="Left" Margin="93,130,0,0" Name="ReferenceCheckBox" VerticalAlignment="Top" Content="Yes" TabIndex="3" FontSize="14"/>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="141,126,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="143" IsEnabled="False" TabIndex="3">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="2,0,0,0">
<TextBlock Text="{Binding Namee}" Tag="{Binding Key}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button x:Name="SaveOKButton" Content="Save" Width="75" Height="23" HorizontalAlignment="Left" Margin="226,288,0,0" TabIndex="11" VerticalAlignment="Top" />
<Button x:Name="CancelButton" Content="Cancel" Width="75" Height="23" HorizontalAlignment="Left" Margin="319,288,0,0" TabIndex="12" VerticalAlignment="Top" />
</Grid>
</Grid>
Following is the back end code for child window:
Partial Public Class New_Contact
Inherits ChildWindow
Dim contactObj As ContactObjs.Contact
Dim org As ContactObjs.Contact
Structure ComboItem
Private id As String
Private display As String
Property Key
Get
Return id
End Get
Set(ByVal value)
id = value
End Set
End Property
Property Namee
Get
Return display
End Get
Set(ByVal value)
display = value
End Set
End Property
End Structure
Public Sub New()
InitializeComponent()
contactObj = New ContactObjs.Contact
Dim serv As New WCFContactService.ContactsClient
AddHandler serv.GetTypesListCompleted, AddressOf TypeslistReceived
AddHandler serv.GetGroupingContactsCompleted, AddressOf ReferenceReceived
serv.GetTypesListAsync(App.Current.Resources("ClientID"), "Contacts Type")
serv.GetGroupingContactsAsync(App.Current.Resources("ClientID"))
End Sub
Public Sub New(ByVal contactID As String)
InitializeComponent()
'contactObj = New ContactObjs.Contact(contactID)
Dim serv As New WCFContactService.ContactsClient
AddHandler serv.GetTypesListCompleted, AddressOf TypeslistReceived
AddHandler serv.GetFullContactCompleted, AddressOf ContactReceived
AddHandler serv.GetGroupingContactsCompleted, AddressOf ReferenceReceived
serv.GetTypesListAsync(App.Current.Resources("ClientID"), "Contacts Type")
serv.GetGroupingContactsAsync(App.Current.Resources("ClientID"))
serv.GetFullContactAsync(contactID)
SaveOKButton.Content = "Update"
End Sub
Private Sub ReferenceReceived(ByVal sender As Object, ByVal e As WCFContactService.GetGroupingContactsCompletedEventArgs)
'Throw New NotImplementedException
ComboBox1.Items.Clear()
Dim result As New List(Of ComboItem)
For Each k As KeyValuePair(Of String, String) In e.Result
Dim c As New ComboItem
c.Key = k.Key
c.Namee = k.Value.ToString()
result.Add(c)
Next
ComboBox1.ItemsSource = result
End Sub
Private Sub AssignValues()
Namee.Text = contactObj.Inf.Namee.ToString()
'Reference
If contactObj.Inf.Referenece = "" Then
ReferenceCheckBox.IsChecked = False
Else
ReferenceCheckBox.IsChecked = True
For i = 0 To ComboBox1.Items.Count - 1
Dim c As ComboItem
c = ComboBox1.Items(i)
If c.Key = contactObj.Inf.Referenece Then
ComboBox1.SelectedIndex = i
End If
Next
'ComboBox1.SelectedValue = contactObj.Inf.Referenece
End If
Department.Text = contactObj.Inf.Department
Office.Text = contactObj.Inf.Office
Profession.Text = contactObj.Inf.Profession
Try
DatePicker1.SelectedDate = contactObj.Inf.Birthdate
Catch ex As Exception
DatePicker1.SelectedDate = Nothing
End Try
GroupCheckBox.IsChecked = contactObj.Inf.Group
If Not contactObj.Inf.PrimaryAddressKey = "" Then
AddressTextBlock.Text = contactObj.Inf.Addresses(contactObj.Inf.PrimaryAddressKey).ToString()
End If
If Not contactObj.Inf.PrimaryNumberKey = "" Then
NumberTextBlock.Text = contactObj.Inf.Numbers(contactObj.Inf.PrimaryNumberKey).ToString()
End If
'Types
If Not contactObj.Inf.ContactType = "" Then
Dim l As New List(Of String)
Dim t As String = ""
For Each k As Char In contactObj.Inf.ContactType
If k = "," Then
l.Add(t)
t = ""
Else
t = t + k.ToString()
End If
Next
Dim tc As New List(Of CheckBox)
For Each c As CheckBox In stackPanel1.Children
If l.Contains(c.Content.ToString()) Then
c.IsChecked = True
End If
tc.Add(c)
Next
stackPanel1.Children.Clear()
For Each k As CheckBox In tc
stackPanel1.Children.Add(k)
Next
End If
End Sub
Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles CancelButton.Click
Me.DialogResult = False
Me.Close()
End Sub
Private Sub NameButton1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles NameButton1.Click
Dim c As New FullName()
If Not contactObj.Inf.Namee.ToString() = "" Then
c = New FullName(contactObj.Inf.Namee)
End If
AddHandler c.Closed, AddressOf FullNameFormClosed
c.Show()
End Sub
Private Sub FullNameFormClosed(ByVal sender As Object, ByVal e As EventArgs)
'Throw New NotImplementedException
Dim k As New FullName
k = sender
With contactObj.Inf.Namee
.Title = k.namee.Title
.FirstName = k.namee.FirstName
.MiddleName = k.namee.MiddleName
.LastName = k.namee.LastName
.Suffic = k.namee.Suffic
Namee.Text = contactObj.Inf.Namee.ToString()
End With
End Sub
Private Sub Typebutton2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Typebutton2.Click
Dim c As New Types()
AddHandler c.Closed, AddressOf TypesFormClosed
c.Show()
End Sub
Private Sub TypesFormClosed(ByVal sender As Object, ByVal e As EventArgs)
'Throw New NotImplementedException
Dim c As Types = sender
If c.DialogResult = True Then
Dim t As New CheckBox
t.Content = c.TextBox1.Text
t.IsChecked = True
stackPanel1.Children.Add(t)
End If
End Sub
Private Sub TypeslistReceived(ByVal sender As Object, ByVal e As WCFContactService.GetTypesListCompletedEventArgs)
'Throw New NotImplementedException
stackPanel1.Children.Clear()
If Not e.Result.Count = 0 Then
For Each k As String In e.Result
Dim c As New CheckBox
c.Content = k
c.IsChecked = False
stackPanel1.Children.Add(c)
Next
End If
End Sub
Private Sub AddressButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddressButton.Click
If contactObj.Inf.Addresses.Count = 0 Then
Dim c As New Addresses(contactObj.Inf.ID)
AddHandler c.Closed, AddressOf AddressFormClosed
c.Show()
Else
Dim c As New Addresses(contactObj.Inf.ID, contactObj.Inf.Addresses)
AddHandler c.Closed, AddressOf AddressFormClosed
c.Show()
End If
End Sub
Private Sub AddressFormClosed(ByVal sender As Object, ByVal e As EventArgs)
'Throw New NotImplementedException
AddressTextBlock.Text = ""
Dim c As New Addresses("")
c = sender
If c.DialogResult = True Then
contactObj.Inf.Addresses = c.addr
For Each t As ContactObjs.Address In contactObj.Inf.Addresses.Values
If t.PrimaryAddress = True Then
AddressTextBlock.Text = t.ToString()
contactObj.Inf.PrimaryAddressKey = t.AddressID
End If
Next
End If
End Sub
Private Sub NumberButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles NumberButton.Click
If contactObj.Inf.Numbers.Count = 0 Then
Dim c As New Numbers(contactObj.Inf.ID)
AddHandler c.Closed, AddressOf NumbersFormClosed
c.Show()
Else
Dim c As New Numbers(contactObj.Inf.ID, contactObj.Inf.Numbers)
AddHandler c.Closed, AddressOf NumbersFormClosed
c.Show()
End If
End Sub
Private Sub NumbersFormClosed(ByVal sender As Object, ByVal e As EventArgs)
'Throw New NotImplementedException
NumberTextBlock.Text = ""
Dim c As New Numbers("")
c = sender
If c.DialogResult = True Then
contactObj.Inf.Numbers = c.num
For Each t As ContactObjs.Number In contactObj.Inf.Numbers.Values
If t.PrimaryNumber = True Then
NumberTextBlock.Text = t.ToString()
contactObj.Inf.PrimaryNumberKey = t.NumberID
End If
Next
End If
End Sub
Private Sub ReferenceCheckBox_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ReferenceCheckBox.Checked
If ReferenceCheckBox.IsChecked = True Then
ComboBox1.IsEnabled = True
Else
ComboBox1.IsEnabled = False
End If
End Sub
Private Sub SaveOKButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles SaveOKButton.Click
Select Case SaveOKButton.Content
Case "Save" 'Contact
contactObj.Inf.ContactType = ""
For Each k As CheckBox In stackPanel1.Children
If k.IsChecked = True Then
contactObj.Inf.ContactType = contactObj.Inf.ContactType + "," + k.Content + ","
End If
Next
If ReferenceCheckBox.IsChecked = True Then
Try
Dim c As New ComboItem
c = ComboBox1.SelectedItem
contactObj.Inf.Referenece = c.Key
Catch ex As Exception
End Try
Else
contactObj.Inf.Referenece = ""
End If
'dept, offi, profe
With contactObj.Inf
.Department = Department.Text
.Office = Office.Text
.Profession = Profession.Text
If IsNothing(DatePicker1.SelectedDate) = False Then
.Birthdate = DatePicker1.SelectedDate
End If
.Group = GroupCheckBox.IsChecked
End With
contactObj.Save()
DialogResult = True
Case "Update"
contactObj.Inf.ContactType = ""
For Each k As CheckBox In stackPanel1.Children
If k.IsChecked = True Then
contactObj.Inf.ContactType = contactObj.Inf.ContactType + "," + k.Content + ","
End If
Next
If ReferenceCheckBox.IsChecked = True Then
Try
Dim c As New ComboItem
c = ComboBox1.SelectedItem
contactObj.Inf.Referenece = c.Key
Catch ex As Exception
End Try
Else
contactObj.Inf.Referenece = ""
End If
'dept, offi, profe
With contactObj.Inf
.Department = Department.Text
.Office = Office.Text
.Profession = Profession.Text
If IsNothing(DatePicker1.SelectedDate) = False Then
.Birthdate = DatePicker1.SelectedDate
Else
.Birthdate = Nothing
End If
.Group = GroupCheckBox.IsChecked
End With
contactObj.Update(org)
DialogResult = True
End Select
Me.Close()
End Sub
Private Sub ChildWindow_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyUp
If e.Key = Key.Enter Then
SaveOKButton_Click(Nothing, Nothing)
End If
End Sub
Private Sub ContactReceived(ByVal sender As Object, ByVal e As WCFContactService.GetFullContactCompletedEventArgs)
'Throw(New NotImplementedException)
If IsNothing(e.Error) Then
contactObj = New ContactObjs.Contact(e.Result)
AssignValues()
org = New ContactObjs.Contact(e.Result)
Else
MessageBox.Show("Error Retreving data, please try again.", "Error", MessageBoxButton.OK)
End If
End Sub
End Class
First time and second time the child window operates flawlessly, but after closing the second time, the app gets stopped without any error. Could you please tell me where I went wrong, I am stuck here from days together.
I really need to thank @xscape for providing me a solution for the problem, it is working like a charm now. However, I would like to update the source code so that I could know what mistake I have performed earlier to cause it disabled. Files are at : enter link description here
Also, I am happy that I got the solution in a matter of hrs here. which is nothing compared to others.
A temporary solution that seems to work is to add this code to your childwindow class.
End Sub