Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Xml
Namespace DevExpress.XtraBars.Demos.BrowserDemo
Public Partial Class frmMain
Inherits DevExpress.XtraEditors.XtraForm
Public Sub New()
InitializeComponent()
AddHandler webBrowser1.StatusTextChanged, AddressOf webBrowser1_StatusTextChanged
AddHandler webBrowser1.CanGoBackChanged, AddressOf webBrowser1_CanGoBackChanged
AddHandler webBrowser1.CanGoForwardChanged, AddressOf webBrowser1_CanGoForwardChanged
barManager1.ForceLinkCreate()
If System.IO.File.Exists(linksName) Then
Dim doc As XmlDocument = New XmlDocument()
Try
doc.Load(linksName)
Catch
End Try
If Not doc.DocumentElement Is Nothing AndAlso doc.DocumentElement.Name = "Items" Then
LoadLinks(doc.DocumentElement.ChildNodes(0).ChildNodes)
LoadFavorites(doc.DocumentElement.ChildNodes(1).ChildNodes)
End If
End If
If System.IO.File.Exists(layoutName) Then
barManager1.RestoreFromXml(layoutName)
End If
AddHandler barManager1.GetController().Changed, AddressOf ChangedController
iFavorites.Down = dockPanel1.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Visible
ips_Init()
InitSkins()
Me.Focus()
End Sub
Private layoutName As String = "layout.xml"
Private skinMask As String = "Skin: "
Private skinProcessing As Boolean = False
Private currentAddress As String = ""
Private tc As Integer = 0
Private linksName As String = "links.xml"
#Region "Skins"
Private Sub InitSkins()
barManager1.ForceInitialize()
iPaintStyle.ImageIndex = 0
iPaintStyle.ImageIndex = -1
If barManager1.GetController().PaintStyleName = "Skin" Then
iPaintStyle.Caption = skinMask & DevExpress.LookAndFeel.UserLookAndFeel.Default.ActiveSkinName
iPaintStyle.Hint = iPaintStyle.Caption
End If
For Each cnt As DevExpress.Skins.SkinContainer In DevExpress.Skins.SkinManager.Default.Skins
Dim item As BarButtonItem = New BarButtonItem(barManager1, skinMask & cnt.SkinName)
iPaintStyle.AddItem(item)
AddHandler item.ItemClick, AddressOf OnSkinClick
Next cnt
End Sub
Private Sub OnSkinClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
Dim skinName As String = e.Item.Caption.Replace(skinMask, "")
DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName)
barManager1.GetController().PaintStyleName = "Skin"
ctrlFavorites1.barManager1.GetController().PaintStyleName = "Skin"
iPaintStyle.Caption = e.Item.Caption
iPaintStyle.Hint = iPaintStyle.Caption
iPaintStyle.ImageIndex = -1
End Sub
Private Sub ChangedController(ByVal sender As Object, ByVal e As EventArgs)
If skinProcessing Then
Return
End If
Dim paintStyleName As String = barManager1.GetController().PaintStyleName
If "DefaultSkin".IndexOf(paintStyleName) >= 0 Then
DevExpress.Skins.SkinManager.EnableFormSkins()
Else
DevExpress.Skins.SkinManager.DisableFormSkins()
End If
skinProcessing = True
DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged()
skinProcessing = False
End Sub
#End Region
Private Sub LoadLinks(ByVal list As XmlNodeList)
Dim i As Integer = 0
Do While i < list.Count
If list.Item(i).Name = "Link" Then
AddNewItem(list(i).InnerText)
End If
i += 1
Loop
End Sub
Private Sub LoadFavorites(ByVal list As XmlNodeList)
Dim i As Integer = 0
Do While i < list.Count
If list.Item(i).Name = "Favorite" Then
AddFavoriteItem(list(i).InnerText, list(i).Attributes(0).Value, True)
End If
i += 1
Loop
ChangeFavorites(True)
End Sub
Private Sub AddNewItem(ByVal s As String)
If s <> "" Then
Dim isAdded As Boolean = False
For i As Integer = 0 To repositoryItemComboBox1.Items.Count - 1
If repositoryItemComboBox1.Items(i).ToString() = s Then
isAdded = True
Exit For
End If
Next i
If (Not isAdded) Then
repositoryItemComboBox1.Items.Add(s)
End If
End If
End Sub
Private Sub GoToItem(ByVal address As String)
If address Is Nothing Then
Return
End If
If currentAddress <> address Then
eAddress.EditValue = address
webBrowser1.Navigate(address)
End If
End Sub
Private ReadOnly Property Address() As String
Get
If Not barManager1.ActiveEditor Is Nothing AndAlso Not barManager1.ActiveEditor.EditValue Is Nothing Then
Return barManager1.ActiveEditor.EditValue.ToString()
End If
Return Nothing
End Get
End Property
Private Sub repositoryItemComboBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles repositoryItemComboBox1.Validating
GoToItem(Address)
End Sub
Private Sub repositoryItemComboBox1_CloseUp(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.CloseUpEventArgs) Handles repositoryItemComboBox1.CloseUp
GoToItem(Address)
End Sub
Private Sub repositoryItemComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles repositoryItemComboBox1.KeyDown
Dim edit As DevExpress.XtraEditors.ComboBoxEdit = TryCast(sender, DevExpress.XtraEditors.ComboBoxEdit)
If e.KeyData = Keys.Escape Then
e.Handled = True
edit.SelectAll()
End If
If e.KeyData = Keys.Enter Then
barManager1.ActiveEditItemLink.PostEditor()
edit.SelectAll()
e.Handled = True
GoToItem(eAddress.EditValue.ToString())
End If
End Sub
Private Sub SaveXML()
Dim tw As XmlTextWriter = New XmlTextWriter(linksName, System.Text.Encoding.UTF8)
tw.Formatting = Formatting.Indented
tw.WriteStartElement("Items")
tw.WriteAttributeString("version", "1.0")
tw.WriteAttributeString("application", Application.ProductName)
tw.WriteStartElement("Links")
Dim i As Integer = 0
Do While i < repositoryItemComboBox1.Items.Count
tw.WriteElementString("Link", repositoryItemComboBox1.Items(i).ToString())
i += 1
Loop
tw.WriteEndElement()
tw.WriteStartElement("Favorites")
For i = 0 To barManager1.Items.Count - 1
If barManager1.Items(i).Category Is barManager1.Categories("Favorites") Then
tw.WriteElementString("Favorite", barManager1.Items(i).Tag.ToString(), barManager1.Items(i).Caption)
End If
Next i
tw.WriteEndElement()
tw.WriteEndElement()
tw.Close()
barManager1.SaveToXml(layoutName)
End Sub
Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
SaveXML()
End Sub
Private Sub webBrowser1_ProgressChanged(ByVal sender As Object, ByVal e As WebBrowserProgressChangedEventArgs) Handles webBrowser1.ProgressChanged
If e.MaximumProgress = repositoryItemProgressBar1.Minimum Then
repositoryItemProgressBar1.Maximum = CInt(Fix(e.MaximumProgress + (1)))
Else
repositoryItemProgressBar1.Maximum = CInt(Fix(e.MaximumProgress + (0)))
End If
eProgress.EditValue = e.CurrentProgress
End Sub
Dim blankString As String = "about:blank"
Private Sub webBrowser1_Navigated(ByVal sender As Object, ByVal e As WebBrowserNavigatedEventArgs) Handles webBrowser1.Navigated
Dim s As String = e.Url.AbsoluteUri
If s = blankString Then Return
If Not barManager1.ActiveEditor Is Nothing Then
barManager1.ActiveEditItemLink.CloseEditor()
End If
If CorrectAddress(s) Then
eAddress.EditValue = s
currentAddress = s
AddNewItem(s)
End If
End Sub
Private Sub webBrowser1_StatusTextChanged(ByVal sender As Object, ByVal e As EventArgs)
iText.Caption = webBrowser1.StatusText
End Sub
Private Sub webBrowser1_CanGoForwardChanged(ByVal sender As Object, ByVal e As EventArgs)
iForward.Enabled = webBrowser1.CanGoForward
End Sub
Private Sub webBrowser1_CanGoBackChanged(ByVal sender As Object, ByVal e As EventArgs)
iBack.Enabled = webBrowser1.CanGoBack
End Sub
Private Function CorrectAddress(ByVal name As String) As Boolean
Dim names As String() = New String() { "javascript:" }
For Each s As String In names
If name.IndexOf(s) = 0 Then
Return False
End If
Next s
Return True
End Function
Private Sub iGo_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iGo.ItemClick
GoToItem(eAddress.EditValue.ToString())
End Sub
Private Sub iBack_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iBack.ItemClick
Try
webBrowser1.GoBack()
Catch
End Try
End Sub
Private Sub iForward_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iForward.ItemClick
Try
webBrowser1.GoForward()
Catch
End Try
End Sub
Private Sub iStop_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iStop.ItemClick
webBrowser1.Stop()
End Sub
Private Sub iRefresh_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iRefresh.ItemClick
webBrowser1.Refresh()
End Sub
Private Sub iHome_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iHome.ItemClick
webBrowser1.GoHome()
End Sub
Private Sub iSearch_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iSearch.ItemClick
webBrowser1.GoSearch()
End Sub
Private Sub iAbout_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iAbout.ItemClick
DevExpress.Utils.About.AboutForm.Show(New DevExpress.Utils.About.ProductInfo(String.Empty, GetType(frmMain), DevExpress.Utils.About.ProductKind.DXperienceWin, DevExpress.Utils.About.ProductInfoStage.Registered))
End Sub
Private Sub iExit_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iExit.ItemClick
Close()
End Sub
Private Sub iOpen_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iOpen.ItemClick
Dim dlg As OpenFileDialog = New OpenFileDialog()
dlg.Filter = "HTML Files|*.htm; *.html|" & "GIF Files|*.gif|" & "JPEG Files|*.jpg;*.jpeg|" & "XML Files|*.xml|" & "All Files |*.*"
dlg.Title = "Open"
If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
GoToItem(dlg.FileName)
End If
End Sub
Private Sub iPrint_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iPrint.ItemClick
Try
Dim pd As System.Drawing.Printing.PrintDocument = New System.Drawing.Printing.PrintDocument()
'todo create pd
Dim dlg As PrintDialog = New PrintDialog()
dlg.Document = pd
If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
pd.Print()
End If
Catch
End Try
End Sub
Private Sub OpenNotepad()
Dim p As System.Diagnostics.Process = New System.Diagnostics.Process()
Dim s As String = System.Environment.SystemDirectory & "\Notepad.exe"
If System.IO.File.Exists(s) Then
p.StartInfo.FileName = s
p.Start()
End If
End Sub
Private Sub iEdit_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iEdit.ItemClick
OpenNotepad()
End Sub
Private Sub iFavorites_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iFavorites.ItemClick
If iFavorites.Down Then
dockPanel1.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Visible
Else
If dockPanel1.Dock = DevExpress.XtraBars.Docking.DockingStyle.Float Then
dockPanel1.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Hidden
Else
dockPanel1.Visibility = DevExpress.XtraBars.Docking.DockVisibility.AutoHide
End If
End If
End Sub
Private Sub AddFavoriteItem(ByVal locationName As String, ByVal locationURL As String)
AddFavoriteItem(locationName, locationURL, False)
End Sub
Private Sub AddFavoriteItem(ByVal locationName As String, ByVal locationURL As String, ByVal init As Boolean)
Dim item As BarItem = New BarButtonItem()
AddHandler item.ItemClick, AddressOf Favorite_Click
item.Category = barManager1.Categories("Favorites")
item.Caption = locationName
item.Tag = locationURL
barManager1.Items.Add(item)
If (Not init) Then
ChangeFavorites()
End If
End Sub
Private Sub AddFavorite()
Dim f As frmAddFavorites = New frmAddFavorites(webBrowser1.DocumentTitle, webBrowser1.Url.AbsoluteUri, imageList1.Images(2))
If f.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim add As Boolean = True
Dim i As Integer = 0
Do While i < barManager1.Items.Count
Dim item As BarItem = barManager1.Items(i)
If item.Category Is barManager1.Categories("Favorites") AndAlso item.Caption = f.LocationName Then
If DevExpress.XtraEditors.XtraMessageBox.Show("The name specified for the shortcut already exists in your Favorites list. Would you like to overwrite it?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
item.Tag = f.LocationURL
End If
add = False
Exit Do
End If
i += 1
Loop
If add Then
AddFavoriteItem(f.LocationName, f.LocationURL)
End If
End If
End Sub
Private Sub iAdd_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles iAdd.ItemClick
AddFavorite()
End Sub
Private Sub Favorite_Click(ByVal sender As Object, ByVal e As ItemClickEventArgs)
GoToItem(e.Item.Tag.ToString())
End Sub
Private Sub ChangeFavorites()
ChangeFavorites(False)
End Sub
Private Sub ChangeFavorites(ByVal init As Boolean)
siFavorites.ClearLinks()
ctrlFavorites1.DeleteItems()
siFavorites.AddItem(iAdd)
Dim i As Integer = 0
Do While i < barManager1.Items.Count
Dim item As BarItem = barManager1.Items(i)
If item.Category Is barManager1.Categories("Favorites") Then
siFavorites.AddItem(item)
ctrlFavorites1.AddItem(item, init)
End If
i += 1
Loop
If siFavorites.ItemLinks.Count > 1 Then
siFavorites.ItemLinks(1).BeginGroup = True
End If
End Sub
Private Sub ctrlFavorites1_AddNewFavorite(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctrlFavorites1.AddNewFavorite
AddFavorite()
End Sub
Private Function ItemByName(ByVal name As String) As BarItem
Dim i As Integer = 0
Do While i < barManager1.Items.Count
Dim item As BarItem = barManager1.Items(i)
If item.Category Is barManager1.Categories("Favorites") AndAlso item.Caption = name Then
Return item
End If
i += 1
Loop
Return Nothing
End Function
Private Sub ctrlFavorites1_DeleteFavorite(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctrlFavorites1.DeleteFavorite
Dim s As String = sender.ToString()
If DevExpress.XtraEditors.XtraMessageBox.Show("Are you sure you want to remove shortcut?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Dim item As BarItem = ItemByName(s)
If Not item Is Nothing Then
barManager1.Items.Remove(item)
ChangeFavorites()
End If
End If
End Sub
Private Sub ctrlFavorites1_EditFavorite(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctrlFavorites1.EditFavorite
Dim s As String = sender.ToString()
Dim item As BarItem = ItemByName(s)
If Not item Is Nothing Then
Dim f As frmAddFavorites = New frmAddFavorites(item.Caption, item.Tag.ToString(), imageList1.Images(1), False)
If f.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
item.Caption = f.LocationName
item.Tag = f.LocationURL
ChangeFavorites()
End If
End If
End Sub
Private Sub ctrlFavorites1_OpenFavorite(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctrlFavorites1.OpenFavorite
Dim item As BarItem = ItemByName(sender.ToString())
If Not item Is Nothing Then
Favorite_Click(item, New ItemClickEventArgs(item, Nothing))
End If
End Sub
Private Sub dockPanel1_VisibilityChanged(ByVal sender As Object, ByVal e As DevExpress.XtraBars.Docking.VisibilityChangedEventArgs) Handles dockPanel1.VisibilityChanged
iFavorites.Down = e.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Visible
End Sub
Private Sub ips_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles ipsDefault.ItemClick, ipsWXP.ItemClick, ipsOXP.ItemClick, ipsO2K.ItemClick, ipsO3.ItemClick
barManager1.GetController().PaintStyleName = e.Item.Description
InitPaintStyle(e.Item)
barManager1.GetController().ResetStyleDefaults()
DevExpress.LookAndFeel.UserLookAndFeel.Default.SetDefaultStyle()
End Sub
Private Sub InitPaintStyle(ByVal item As BarItem)
If item Is Nothing Then
Return
End If
iPaintStyle.ImageIndex = item.ImageIndex
iPaintStyle.Caption = item.Caption
iPaintStyle.Hint = item.Description
ctrlFavorites1.barManager1.GetController().PaintStyleName = barManager1.GetController().PaintStyleName
End Sub
Private Sub ips_Init()
Dim item As BarItem = Nothing
For i As Integer = 0 To barManager1.Items.Count - 1
If barManager1.Items(i).Description = barManager1.GetController().PaintStyleName Then
item = barManager1.Items(i)
End If
Next i
InitPaintStyle(item)
End Sub
Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
If bar5.ItemLinks.Count < 1 OrElse Not bar5.ItemLinks(0).Item Is iPaintStyle OrElse (Not barManager1.HighlightedLink Is Nothing AndAlso Not barManager1.HighlightedLink.Item Is iPaintStyle) Then
timer1.Stop()
Return
End If
If barManager1.HighlightedLink Is Nothing Then
barManager1.SelectLink(bar5.ItemLinks(0))
Else
barManager1.SelectLink(Nothing)
End If
tc += 10
If tc > 10 Then
timer1.Stop()
barManager1.SelectLink(Nothing)
End If
End Sub
Private Sub InitHomePage()
GoToItem("www.devexpress.com")
End Sub
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
BeginInvoke(New MethodInvoker(AddressOf InitHomePage))
End Sub
End Class
End Namespace