silverlight OpenFileDialog opens a second time - silverlight

I'm using the openFileDialog in sliverlight 2.0 with vb.net in the back end. I have it wired up and it is working as it is suppose to except that the click event seems to fire twice. It fires the first time, i select the files and click ok. it does the processing. But as soon as i click ok, the click event fires a second time and the dialog box comes up again. that is not what i want and i'm not sure what i'm doing wrong that it comes up that second time. Here is the code.. hopefully someone see's what i'm doing wrong.
<Button x:Name="bOpenFileDialog" Content="2. Import CSV"
Height="30" Width="200" Margin="0,96,0,0"
HorizontalAlignment="Left" VerticalAlignment="Top"
Click="bOpenFileDialog_Click" />
<TextBlock Height="19" Margin="246,26,261,0" VerticalAlignment="Top" Text="TextBlock" TextWrapping="Wrap" x:Name="lblMsg"/>
Private Sub bOpenFileDialog_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles bOpenFileDialog.Click
Me.bOpenFileDialog.IsEnabled = False
' Create an instance of the open file dialog box.
Dim openFileDialog1 As OpenFileDialog = New OpenFileDialog
' Set filter options and filter index.
openFileDialog1.Filter = "LOG Files (*.log)|*.log|All Files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.Multiselect = True
' Call the ShowDialog method to show the dialogbox.
Dim UserClickedOK As Boolean = CBool(openFileDialog1.ShowDialog)
' Process input if the user clicked OK.
If (UserClickedOK = True) Then
Dim rows As Integer = openFileDialog1.Files.Count - 1
ReDim aryIISLogs(rows)
For i As Integer = 0 To openFileDialog1.Files.Count - 1
aryIISLogs(i) = openFileDialog1.Files(i).Name
Next
Process1File()
End If
End Sub
Thanks
Shannon

I think this is because you register the event twice: you have it in the button definition:
Click="bOpenFileDialog_Click"
and in the method definition:
Private Sub bOpenFileDialog_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles bOpenFileDialog.Click
Each of these triggers the event, so you have two popups. If you remove either "Click=" or "handles" you'll get the event to fire only once.

Related

How to write out button event without using WPF controls?

I do not know how to specify in the title, this is in WPF visual basic. I want to know how do I write a code so that when a button is clicked, the tabcontrol selection will be = 1
Here is what I have in my MainWindow, RightWindowCommands:
<Button Content="Information" Cursor="Hand" Click="InformationButton_OnClick"
ToolTip="View the information"
x:Name="InformationView"/>
However, I did not use the WPF tools' Button, as this is a GUI that I have to place the button at RightWindowCommands, I want to know how to come out with the code so that InformationButton_OnClick gives me tabControl.SelectedIndex = 1. Please guide me on writing this code out
Here is a nice example for onclick event handling a picture, you can change it to a number instead of picture.
`AddHandler pic.Click, AddressOf pic_Click'
Private Sub pic_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim pic As PictureBox = DirectCast(sender, PictureBox)
' etc...
End Sub
Source : add on click event to picturebox vb.net

host WPF windows in another WPF window

here is my problem: i have created a main window called mainWindow in XAML (vb.net) and inside i have 2 buttons (valid and stop) and a grid in the center.
I have two others little windows (valid window and stop window) written in XAML (vb.net) which have buttons, textbox...
I want, when i click on valid button or stop button, display the valid window or the stop window inside the grid of my mainWindow, so i have this code in my mainWindow.vb:
enter code here
Private Sub valid_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles valid.Click
Dim content As Object = valid_.Content /*Classe valid_ (a window in xaml)*/
valid_.Content = Nothing
Me.Grid.Children.Add(content)
End Sub
Private Sub stop_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles stop.Click
Dim content As Object = stop_.Content
stop_.Content = Nothing
Me.Grid.Children.Add(content)
End Sub
So when i click on button valid in my mainWindow, it's ok it displays the valid window in my grid.
First problem: then when i click on button stop in my mainWindow, the stop window is placed just above the valid window, it is not nice, is there a way to clear the grid before display this second window?
And finally, the biggest problem: i need to click many times on valid button or stop button but when i click the second time i have a null reference exception: Me.Grid.Children.Add(content) content is null after the first call so i am only able to click one time on my button.
How can i fix it in order to click many times on my buttons please?
I give you thanks.
Yet again, someone writes some invalid code and then says why isn't this code working? If you look at your code, you should see something:
Private Sub valid_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles valid.Click
Dim content As Object = valid_.Content /*Classe valid_ (a window in xaml)*/
Me.Grid.Children.Add(content)
End Sub
I'm guessing that you have a private variable for your Valid Window as you call it: valid. On this line, what are you doing to your variable?:
valid_.Content = Nothing
That's right! You're setting its Content property to null. Therefore, I'm not really sure why you were surprised that it was null after the first attempt.
How can i fix it in order to click many times on my buttons please?
Try removing the line that sets the Content property to null.
UPDATE >>>
Your problem is really caused by the fact that you cannot display any UI element in two places at once. Your whole idea of copying the Window.Content to your DataGrid is entirely wrong, but in the name of brevity, your fix is simply to move your content back to the Window, rather than setting it to null each time. Try something like this:
Private Sub valid_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles valid.Click
If Me.Grid.Children.Count > 0 Then 'Put content back where it belongs
Dim oldContent = Me.Grid.Children(0)
stop.Content = oldContent
Me.Grid.Children.Remove(oldContent)
EndIf
Dim content As Object = valid_.Content /*Classe valid_ (a window in xaml)*/
valid_.Content = Nothing
Me.Grid.Children.Add(content)
End Sub
Please forgive any code mistakes, as I don't write VB.NET. Also, you'll need to update your other method likewise.

close wpf user control in VB.NET

I'm stuck on this issue, please help.
I created an user control named "CreateNewCase_uc" within which I created a button Close named "btnClose"
In my MainWindow, I created a Grid named "grid1" and a button Open named "btnCreateNewCase" with this code
Private Sub btnCreateNewCase_Click(sender As Object, e As RoutedEventArgs)
Dim cnc As CreateNewCase_uc = New CreateNewCase_uc
grid1.Children.Clear()
grid1.Children.Add(cnc)
End Sub
My question : which Code I need to Write for my bntClose button which is inside the user control to close or make disappeared the user control in VB.NET
Thanks in advance for your help.
You need to get the Parent control then remove it.
' from inside your close button on UC.
Dim parent = TryCast(Me.Parent, Grid))
If Not parent Is Nothing Then
parent.Children.Remove(Me)
End If
You may simply set the UserControl's Visibility to Collapsed:
Private Sub btnCreateNewCase_Click(sender As Object, e As RoutedEventArgs)
Me.Visibility = System.Windows.Visibility.Collapsed
End Sub

WPF: Databinding and the keypress handlers

This is my textbox:
<TextBox TextAlignment="Right" Text="{Binding PriorityScore, StringFormat=N2}"
Name="PriorityScoreBox" TextChanged="PriorityScoreBox_TextChanged" />
When I click on the Save button everything works fine.
When I press Control-S, it doesn't save the pending changes in the textbox. This is done via a page-level key press handler.
I cannot use UpdateSourceTrigger=PropertyChanged because it interfers with the string formatting. (The user types "4" and the box auto-changes to "4.00".)
Perhaps there is a way to commit the outstanding changes in a key press event?
Private Sub PriorityScoreBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
If e.Key = Input.Key.S And CBool(e.KeyboardDevice.Modifiers And ModifierKeys.Control) Then
Dim bx As BindingExpression = CType(sender, TextBox).GetBindingExpression(TextBox.TextProperty)
bx.UpdateSource()
End If
End Sub

How to get ElementHost control, given one of WPF control's content

I am trying to get a reference to ElementHost control. For example in the below code I need to initially use the “testImage” content of the WPF user control to lunch the event. The WPF control is added at run-time, so does the ElementHost control, so I can’t use the WPF control’s name or ElementHost’s name.
My logic is to get the parent WPF user control of the “testImage”, and then get the parent ElementHost of the WPF’s user control.
But I am having troubles writing it in code. Please advise. Thanks.
<UserControl x:Class="WpfTest”
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300">
<Grid>
<Label FontSize="10" Height="24" Margin="74,16,0,0" Name="testLabel" VerticalAlignment="Top" />
<Image Name="testImage" Stretch="Uniform" HorizontalAlignment="Left" Width="64" Height="81" VerticalAlignment="Top" Margin="8,0,0,0"/>
</Grid>
</UserControl>
Here is some code that might help you. The key points are:
Name the ElementHost when you create it at runtime
Make use the the help function FindVisualChildByName() to search the WPF tree to get the desired control
I Hope this helps!
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ElementHost1 As New System.Windows.Forms.Integration.ElementHost
Dim WpfTest1 As New WindowsApplication1.WPFTest
ElementHost1.Dock = DockStyle.Fill
ElementHost1.Name = "ElementHost1"
ElementHost1.Child = WpfTest1
Me.Controls.Add(ElementHost1)
End Sub
Private Sub GetImageReference_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ElementHost1 As System.Windows.Forms.Integration.ElementHost = Me.Controls("ElementHost1")
Dim TheGrid As System.Windows.Controls.Grid = CType(ElementHost1.Child, WPFTest).MyGrid
Dim ImageTest As System.Windows.Controls.Image = FindVisualChildByName(TheGrid, "testImage")
Stop
End Sub
Public Function FindVisualChildByName(ByVal parent As System.Windows.DependencyObject, ByVal Name As String) As System.Windows.DependencyObject
For i As Integer = 0 To System.Windows.Media.VisualTreeHelper.GetChildrenCount(parent) - 1
Dim child = System.Windows.Media.VisualTreeHelper.GetChild(parent, i)
Dim controlName As String = child.GetValue(System.Windows.Controls.Control.NameProperty)
If controlName = Name Then
Return child
Else
Dim res = FindVisualChildByName(child, Name)
If Not res Is Nothing Then
Return res
End If
End If
Next
Return Nothing
End Function

Resources