Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
i have a program which opens excel and check for broken links in the given sheet. i want to display the user the list of given links and which ever is broken. i want to change foreground to RED. i have bound the values to observablecollection, the collection is just collection of strings(WEB LINKS) and is working fine. the logic is working good.
the part of binding with the list view is also working.

now i am trying to change the color of the URL's which are broke to RED. till now i am unsuccessful. and i tried few codes got when searching. and changed the XAML.
now the application is hanging when the code runs. particularly when it is checking for the websites.
Forgive my bad codding i am learning

XAML:-
<Window x:Class="URLValide.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:Local="clr-namespace:URL_Validater.URLValide"
    Title="URL Validator" Height="269" Width="525" FontSize="13"
    ResizeMode="NoResize">
    
    <Window.Resources>
        <ResourceDictionary>
            <Style x:Key="HighLight" TargetType="{x:Type Control}">
                <Setter Property="Background" Value="Honeydew"/>                
                <Style.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect BlurRadius="40" Color="Beige" Direction="50" Opacity="0.5"/>                                
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Background" Value="White"/>
                        <Setter Property="FontWeight" Value="UltraBlack"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
            
            <!--<Style TargetType="{x:Type ListViewItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ColorIndx}" Value="True">
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="Foreground" Value="White"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>-->
        </ResourceDictionary>
    </Window.Resources>
    
    <DockPanel LastChildFill="True">
        <Label DockPanel.Dock="Top" Name="WinHeader"  Height="30" Background="BurlyWood" FontFamily="Calibri" FontSize="20" FontWeight="Bold" FontStretch="Medium" VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="0,0,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Top">URL Validator</Label>
        <Grid DockPanel.Dock="Bottom" removed="Beige">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="20" MaxHeight="30"/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Text="Current URL:"></TextBlock>
            <TextBlock Grid.Column="1"  Text="{Binding strCrnUrl, UpdateSourceTrigger=PropertyChanged}"  Width="370"></TextBlock>
            <TextBlock Grid.Column="2"  Text="{Binding strTotlOf, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center"></TextBlock>
        </Grid>
        <Grid DockPanel.Dock="Right" Width="154">
            <Grid.ColumnDefinitions>
                <ColumnDefinition MaxWidth="300" Width="19*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition MaxHeight="55" MinHeight="10" Height="35*" />
                <RowDefinition Height="165*"  MinHeight="10"  />
            </Grid.RowDefinitions>
            
            <Label Name="LbleRight" BorderBrush="Black" BorderThickness="1" Margin="0,2" FontSize="13" Content="List Of URL's" Height="30" Grid.ColumnSpan="2" Grid.Row="0"></Label>
            <ListView Grid.Row="1" removed="Azure" ItemsSource="{Binding strPdfLst}" IsEnabled="True" Margin="4,0,0,1" Foreground="{Binding ColorIndx}">
                
            </ListView>            
        </Grid>
        
        <Grid DockPanel.Dock="Left">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="30*" />
                <RowDefinition Height="60*" />                
            </Grid.RowDefinitions>            
            <Label BorderBrush="Black" Name="lblFleSel" BorderThickness="1" Height="27" Margin="2,2,0,30" HorizontalAlignment="Left" Width="349">Select File To which Contains the list of URL's:</Label>
            <TextBox Name="txtbxFleNme" BorderBrush="Black" BorderThickness="1" Margin="2,34,0,0" Style="{StaticResource ResourceKey=HighLight}" HorizontalAlignment="Left" Width="272"></TextBox>
            <Button Name="btnFleSlec" Width="69" Height="27" Content="Browse" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="276,32,4,0"></Button>
            <Grid Grid.Row="1" Height="118" HorizontalAlignment="Left" Margin="0,1,0,0" Name="OptionGrid" VerticalAlignment="Top" Grid.RowSpan="2">
                <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal">
                    <Border BorderBrush="Black" BorderThickness="1" Height="117" Margin="3,1,0,0" HorizontalAlignment="Stretch" Width="350">
                        <Grid Height="auto" HorizontalAlignment="Stretch" Width="335">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="10*"/>
                                <RowDefinition Height="10*"/>
                                <RowDefinition Height="10*"/>
                                <RowDefinition Height="10*"/>
                                <RowDefinition Height="10*"/>
                            </Grid.RowDefinitions>
                            <CheckBox Name="chkboxHas" VerticalAlignment="Center" HorizontalAlignment="Center">Has Column Headers</CheckBox>
                            <Label Name="lblDesc" Height="auto" Margin="0,0,204,0" Grid.Row="1" HorizontalContentAlignment="Right" VerticalAlignment="Top">Description Column:</Label>
                            <ComboBox Grid.Row="1" Name="cmboxDescol"  Margin="131,0,0,0" Style="{StaticResource ResourceKey=HighLight}"></ComboBox>
                            <Label Name="lblUrlCol" Height="auto" Grid.Row="2" Margin="0,0,204,0" HorizontalContentAlignment="Right">URL's Column:</Label>
                            <ComboBox Name="cmboxUrlCol" Grid.Row="2" Margin="131,0,0,0" Style="{StaticResource ResourceKey=HighLight}"></ComboBox>
                            <Label Name="lblResCol" Height="auto" Grid.Row="3" Margin="0,0,196,0" HorizontalContentAlignment="Right">Result Column:</Label>
                            <ComboBox Name="cmboxResCol" Grid.Row="3" Margin="131,0,0,0" Style="{StaticResource ResourceKey=HighLight}"></ComboBox>
                            <Button Grid.Row="4" HorizontalAlignment="Center" VerticalAlignment="Center" Content="Validate URL links" Name="btnValidate"></Button>
                            <Button Grid.Row="4" HorizontalAlignment="Center" VerticalAlignment="Center" Content="Start" Name="btnstrt" Margin="244,0,56,0"></Button>
                        </Grid>
                    </Border>                    
                </StackPanel>
            </Grid>
        </Grid>
    </DockPanel>
    
</Window>


VB.net Class:-

VB
    Imports Microsoft
Imports Microsoft
Imports System
Imports System.Net
Imports System.Data
Imports System.Windows
Imports System.Windows.Forms
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Collections.ObjectModel
Imports System.ComponentModel

Namespace URLValide

    Public Class clsUrlCheck
        Implements INotifyPropertyChanged

        Public Event propertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

        Public exclApplic As New Excel.Application
        Public exclWkbOpe As Excel.Workbook
        Public exclWksAct As Excel.Worksheet
        Public exclRngUrl As Excel.Range
        Public exclRngDes As Excel.Range
        Public exclRngOut As Excel.Range
        Public intMaxRow As Long
        Public intCrtRow As Long
        Private _strTotlOf As String
        Private _strCrnUrl As String
        Private _ColorIndx As SolidColorBrush
        Private _strPdfLst As ObservableCollection(Of String)
        Private _strhdrPdf As ObservableCollection(Of String)

#Region "Region of poperties"
        Public Property strTotlOf() As String
            Get
                Return _strTotlOf
            End Get
            Set(ByVal value As String)
                _strTotlOf = value
                Me.OnPropertyChanged("strTotlOf")
            End Set
        End Property

        Public Property strCrnUrl() As String
            Get
                Return _strCrnUrl
            End Get
            Set(ByVal value As String)
                _strCrnUrl = value
                Me.OnPropertyChanged("strCrnUrl")
            End Set
        End Property

        Public Property strPdfLst As ObservableCollection(Of String)
            Get
                Return _strPdfLst
            End Get
            Set(ByVal value As ObservableCollection(Of String))
                _strPdfLst = value
                Me.OnPropertyChanged("strPdfLst")
            End Set
        End Property

        Public Property strhdrPdf As ObservableCollection(Of String)
            Get
                Return _strhdrPdf
            End Get
            Set(ByVal value As ObservableCollection(Of String))
                _strhdrPdf = value
                Me.OnPropertyChanged("strhdrPdf")
            End Set
        End Property

        Public Property ColorIndx As SolidColorBrush
            Get
                Return _ColorIndx
            End Get
            Set(ByVal value As SolidColorBrush)
                _ColorIndx = value
                Me.OnPropertyChanged("ColorIndx")
            End Set
        End Property

        Private Sub OnPropertyChanged(ByVal propertyName As String)
            RaiseEvent propertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub
#End Region

#Region "function of Validating the URL's"
        Public Function UrlCheck(ByVal strUrlReq As String)

            Try

                Dim WebReq As Net.HttpWebRequest = Net.HttpWebRequest.Create(strUrlReq)
                WebReq.Method = "Head"
                WebReq.Timeout = 5000
                Using Response = WebReq.GetResponse()
                    Return True
                End Using

            Catch ex As Exception
                ColorIndx = New SolidColorBrush(Colors.Red)
                Return False

            End Try

        End Function
#End Region

#Region "Start Funtion"
        Sub suStart()

            If exclRngDes.Value = "Make Desc" Then
                strPdfLst = makeList(exclRngUrl.Column, True)
                exclRngDes.Delete()
            Else
                strPdfLst = makeList(exclRngUrl.Column, False)
            End If
        
            Dim blnValid As Boolean
            For Each exclRngEch As Excel.Range In exclRngUrl
                strCrnUrl = exclRngEch.Text
                strTotlOf = exclRngEch.Row & "OF" & intMaxRow
                blnValid = UrlCheck(exclRngEch.Value.ToString)
                If blnValid Then
                    exclWksAct.Cells(exclRngEch.Row, exclRngOut.Column).value = "Web Page Present"
                Else
                    exclWksAct.Cells(exclRngEch.Row, exclRngOut.Column).value = "Web Page Error"
                End If

            Next


        End Sub

        Function makeList(ByVal intColNum As Long, ByVal blnMkeStr As Boolean) As ObservableCollection(Of String)

            Dim ObsColTem As ObservableCollection(Of String) = New ObservableCollection(Of String)

            If Not blnMkeStr Then
                For intLopCnt As Int32 = intCrtRow To intMaxRow
                    ObsColTem.Add(exclWksAct.Cells(intLopCnt, intColNum).Text)
                Next
            Else
                For intLopCnt As Int32 = intCrtRow To intMaxRow
                    Dim strDesc As String = Mid(exclWksAct.Cells(intLopCnt, intColNum).Text, InStrRev(exclWksAct.Cells(intLopCnt, intColNum).Text, "/", , CompareMethod.Text) + 1)
                    ObsColTem.Add(strDesc)
                Next
            End If
            Return ObsColTem
        End Function
#End Region

    End Class
End Namespace



Sorry if i was not clear.
The code is running correctly and i am getting the out put.
The code is not freezing or stopping the execution. but the WPF form window is going BLANK.
like when you go for step by step debug, you will see just a blank white window rite. like that it is coming. when the execution is complete the window comes alive and shows the related output correctly. event the Excel is updated.
So code is good.
Posted
Updated 5-Mar-13 15:53pm
v2
Comments
earloc 5-Mar-13 9:21am    
which line causes your app to hang?
Manu_vish 5-Mar-13 21:56pm    
I am not sure which line. but when debugging step by step. it was taking time with the webrespone step in URLCheck function. will that make the window go blank??
Sergey Alexandrovich Kryukov 5-Mar-13 18:23pm    
Use the debugger and provide more information. I doubt anyone would do this "research" for you...
—SA
Manu_vish 5-Mar-13 21:57pm    
i updated the question and did debugging. i THINK the webresponse is taking time.
Sergey Alexandrovich Kryukov 5-Mar-13 22:27pm    
Thank you.
—SA

1 solution

You are right in your assumption that HttpWebRequest.GetResponse can freeze the UI and cause some trouble. Look at your code: you are using 5 sec timeout; it means that your UI thread may hang for 5 sec. I'm not saying that this is the only problem, but this is one of the thing you should never do in your UI thread. You have to have a separate thread and do your check there, as with any other blocking, potentially blocking calls or long-lasting operations. Using a separate thread, you may also need thread synchronization and/or UI thread invocation using Dispatcher.

—SA
 
Share this answer
 
Comments
Manu_vish 5-Mar-13 22:48pm    
thanks. woha!!! this small program is giving me nice thing to learn. will try threads then. thanks alot. but that time out is necessary rite for me to wait till the web responds. so removing it (in the new thread) is a bad idea??
Sergey Alexandrovich Kryukov 5-Mar-13 23:00pm    
This is great.
Your timeout of course may make full sense, but in non-UI thread this timeout is not harmful. At least there is no need to remove the timeout. Do you see the idea?

If so, will you accept the answer formally (green button)?
In all cases, your follow-up questions are very welcome.

By the way, I appreciate that on my request you fixed your post to make it a valid question: you provided adequate code sample and explanation. Unfortunately, these days such reasonable inquirers are pretty rare...

—SA
Manu_vish 5-Mar-13 23:07pm    
Thanks for responding. i am learning WPF now. was working with forms and VBA. so i have lot of doubts and question. thank you very much for the answer.
I am a lone person working in .net tech and have no one to guide. that to as i am new to WPF it is really hard to learn. thanks to people like you and the forums that i am learning.
Thanks again(will come back with more questions ;))
Sergey Alexandrovich Kryukov 5-Mar-13 23:13pm    
My pleasure. Will be glad to help if you ask your further questions.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900