I have this problem in this Code:
If RichTextBox1.Font.Bold = True Then
RichTextBox1.Font.Bold = False
Else
RichTextBox1.Font.Bold = True
End If
Here My problem:"RichTextBox1.Font.Bold = False" (This property is ReadOnly),
I need some Help.
Here's a simple example:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If RichTextBox1.Font.Bold = True Then
RichTextBox1.Font = New Font(RichTextBox1.Font, FontStyle.Regular)
Else
RichTextBox1.Font = New Font(RichTextBox1.Font, FontStyle.Bold)
End If
End Sub
Related
As a new programmer in VB, I struggled to get my print routine working. Searching through a number of sources I developed the hybrid below which is working to print a simple .txt file. My question sounds like a simple one but I've learned nothing is simple in printing. How do I get the PrintPreviewDialog to close once the printing is complete?
Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click
Try
PrintPreviewDialog1.Document = PrintDocument1
PageSetupDialog1.PageSettings =
PrintDocument1.DefaultPageSettings
If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.DefaultPageSettings =
PageSetupDialog1.PageSettings
PrintPreviewDialog1.Size = New System.Drawing.Size(500, 600)
PrintPreviewDialog1.ShowDialog()
End If
Catch ex As Exception
MessageBox.Show("Printing Operation Failed" & vbCrLf &
ex.Message)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
Static MyNewAcctFile As String = IO.File.ReadAllText(strErrorFile)
Dim printFont As New Font("Arial", 14, FontStyle.Regular)
Dim charsFitted As Integer
Dim linesFilled As Integer
e.Graphics.MeasureString(MyNewAcctFile, printFont, New SizeF(e.MarginBounds.Width, e.MarginBounds.Height), Drawing.StringFormat.GenericTypographic, charsFitted, linesFilled)
e.Graphics.DrawString(MyNewAcctFile, printFont, Brushes.Black, e.MarginBounds, Drawing.StringFormat.GenericTypographic)
MyNewAcctFile = MyNewAcctFile.Substring(charsFitted)
If MyNewAcctFile <> "" Then
e.HasMorePages = True
Else
e.HasMorePages = False
MyNewAcctFile = IO.File.ReadAllText(strErrorFile)
End If
End Sub
I would expect to see something more like:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Private FileName As String
Private txtFileContents As String
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
FileName = "C:\Users\mikes\Documents\SomeFile.txt"
txtFileContents = IO.File.ReadAllText(FileName)
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
If txtFileContents.Length > 0 Then
Using printFont As New Font("Arial", 14, FontStyle.Regular)
Dim charsFitted As Integer
Dim linesFilled As Integer
e.Graphics.MeasureString(txtFileContents, printFont, New SizeF(e.MarginBounds.Width, e.MarginBounds.Height), Drawing.StringFormat.GenericTypographic, charsFitted, linesFilled)
e.Graphics.DrawString(txtFileContents, printFont, Brushes.Black, e.MarginBounds, Drawing.StringFormat.GenericTypographic)
txtFileContents = txtFileContents.Substring(charsFitted)
e.HasMorePages = (txtFileContents.Length > 0)
End Using
End If
End Sub
Private Sub PrintDocument1_EndPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.EndPrint
If Not PrintDocument1.PrintController.IsPreview Then
PrintPreviewDialog1.Close()
End If
End Sub
End Class
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
txtRate.Text = "0"
lblPromotional.ForeColor = Color.Empty
lblPromotional.BackColor = Color.Empty
lblPromotional.Font = originalfontname()
It's not working for font?
If you don't want to use originalFont, then you can just manually change the label font by
lblPromotional.Font = New Drawing.Font("Times New Roman", 16, FontStyle.Bold)
or whatever font you used. Here is the documentation: https://msdn.microsoft.com/en-us/library/system.drawing.font(v=vs.110).aspx
You need to cache the entire Font object before you change it - not just the font name.
Class SomeForm Inherits Form
Private originalFont As Font
Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.originalFont = Me.lblPromotional.Font
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
txtRate.Text = "0"
lblPromotional.ForeColor = Color.Empty
lblPromotional.BackColor = Color.Empty
lblPromotional.Font = Me.originalFont
End Sub
End Class
With the help of the code below (by LarsTech - drag and detach tabpages
) I was able to detach a tabpage and place it into a new form. But when I close that form the dragged tabpage doesn't return to its original position.
If anyone out there can help me program this, it'll be great!
Private Sub TabControl1_MouseMove(sender As Object, e As MouseEventArgs) Handles TabControl1.MouseMove
If (e.Button = MouseButtons.Left) Then
TabControl1.DoDragDrop(TabControl1.SelectedTab, DragDropEffects.Move)
End If
End Sub
Private Sub TabControl1_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles TabControl1.GiveFeedback
e.UseDefaultCursors = False
End Sub
Private Sub TabControl1_QueryContinueDrag(sender As Object, e As QueryContinueDragEventArgs) Handles TabControl1.QueryContinueDrag
If Control.MouseButtons <> MouseButtons.Left Then
e.Action = DragAction.Cancel
Dim f As New Form
f.Size = New Size(400, 300)
f.StartPosition = FormStartPosition.Manual
f.Location = MousePosition
Dim tc As New TabControl
tc.Dock = DockStyle.Fill
tc.TabPages.Add(TabControl1.SelectedTab)
f.Controls.Add(tc)
f.Show()
Me.Cursor = Cursors.Default
Else
e.Action = DragAction.Continue
Me.Cursor = Cursors.Help
End If
End Sub
I've been able to produce the code which returns the tabpage to its original place when the form closes, however the page doesn't return to the original index position. Below is the updated code:
Public f As Form
Private Sub TabControl1_MouseMove(sender As Object, e As MouseEventArgs) Handles TabControl1.MouseMove
If (e.Button = MouseButtons.Left) Then
TabControl1.DoDragDrop(TabControl1.SelectedTab, DragDropEffects.Move)
End If
End Sub
Private Sub TabControl1_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles TabControl1.GiveFeedback
e.UseDefaultCursors = False
End Sub
Private Sub TabControl1_QueryContinueDrag(sender As Object, e As QueryContinueDragEventArgs) Handles TabControl1.QueryContinueDrag
f = New Form
If Control.MouseButtons <> MouseButtons.Left Then
e.Action = DragAction.Cancel
AddHandler f.FormClosing, AddressOf ClosingDraggableWindow_EventHandler
f.Size = New Size(400, 300)
f.Name = TabControl1.SelectedTab.Text
f.TabIndex = TabControl1.SelectedTab.TabIndex
f.StartPosition = FormStartPosition.Manual
f.Location = MousePosition
Dim tc As New TabControl()
tc.Dock = DockStyle.Fill
tc.TabPages.Add(TabControl1.SelectedTab)
f.Controls.Add(tc)
f.Show()
End If
End Sub
Sub ClosingDraggableWindow_EventHandler()
Dim tbp As New TabPage()
tbp.Text = f.Name
Dim tbc As TabControl = f.Controls(0)
Dim tbp2 As TabPage = tbc.TabPages(0)
TabControl1.TabPages.Insert(f.TabIndex + 1, tbp2)
End Sub
Changing this sentence
f.TabIndex = TabControl1.SelectedTab.TabIndex
to
f.TabIndex = TabControl1.SelectedIndex
it works fine.
TabIndex represents the order into the form but not the index into the TabControl1.
i just start create a project with this(To The Topic)
an error is start in here(maybe)
First Button Code
Private Sub DeimosButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeimosButton2.Click
State(i)
DeimosButton3.Visible = False
DeimosButton4.Visible = False
End Sub
Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
DeimosButton2.Top = DeimosButton2.Top - 1
If DeimosButton2.Location = New Point(22, 54) Then
Timer4.Stop()
DeimosButton2.Enabled = True
End If
End Sub
Private Sub State(ByVal Ref As Integer)
If Ref = 0 Then
Timer4.Start()
DeimosButton2.Enabled = False
i = 1
Else
Timer5.Start()
DeimosButton2.Enabled = False
i = 0
End If
End Sub
Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
DeimosButton2.Top = DeimosButton2.Top + 1
If DeimosButton2.Location = New Point(22, 85) Then
Timer5.Stop()
DeimosButton2.Enabled = True
DeimosButton3.Visible = True
DeimosButton4.Visible = True
End If
End Sub
This Second Button Code
Private Sub DeimosButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeimosButton3.Click
states(o)
DeimosButton2.Visible = False
DeimosButton4.Visible = False
End Sub
Private Sub Timer6_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer6.Tick
DeimosButton3.Top = DeimosButton3.Top - 1
If DeimosButton3.Location = New Point(22, 54) Then
Timer6.Stop()
DeimosButton3.Enabled = True
End If
End Sub
Private Sub states(ByVal Def As Integer)
If Def = 0 Then
Timer6.Start()
DeimosButton3.Enabled = False
o = 1
Else
Timer7.Start()
DeimosButton3.Enabled = False
o = 0
End If
End Sub
Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick
DeimosButton3.Top = DeimosButton3.Top + 1
If DeimosButton3.Location = New Point(22, 116) Then
Timer7.Stop()
DeimosButton3.Enabled = True
DeimosButton2.Visible = True
DeimosButton4.Visible = True
End If
End Sub
The problem is when i click the second button
but when i click the first button, is no matter.
Problem : The button is still moving to top, although it is has reach the position.
there is a solution for this ?
You must change
DeimosButton3.Location = New Point(22, 116)
to
DeimosButton3.Location.y = 116
This is valid to all
for all your code
I have a web browser that I am trying to keep to the minimal to keep it running fast (I have the firefox gecko browser engine even though that doesn't matter for this question I think.) but there is one thing I want to add and that is bookmarks. Right now I have a ton of messy code but I wasn't able to create a new tool strip button for each time I hit the bookmark button. So what I did is I added the appropriate settings and 6 toolstrip buttons. Now this limits me to 6 bookmarks. Which really sucks. My code is here:
Imports System.IO
Public Class tabForm
Dim ico As Image = Nothing
Private Sub goBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goBack.Click
webBrowser.GoBack()
End Sub
Private Sub goForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goForward.Click
webBrowser.GoForward()
End Sub
Private Sub Navigate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Navigate.Click
If urlBox.Text = "yt" Then
webBrowser.Navigate("http://www.youtube.com")
ElseIf urlBox.Text = "fb" Then
webBrowser.Navigate("http://www.facebook.com")
ElseIf urlBox.Text = "gm" Then
webBrowser.Navigate("http://www.gmail.com")
ElseIf urlBox.Text = "go" Then
webBrowser.Navigate("http://www.google.com")
Else
webBrowser.Navigate(urlBox.Text)
End If
End Sub
Private Sub webBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.EventArgs) Handles webBrowser.DocumentCompleted
Me.Text = webBrowser.DocumentTitle
geticon()
End Sub
Private Sub webBrowser_Navigated(ByVal sender As System.Object, ByVal e As Skybound.Gecko.GeckoNavigatedEventArgs) Handles webBrowser.Navigated
Try
urlBox.Text = webBrowser.Url.ToString
Catch ex As Exception
End Try
End Sub
Private Sub urlBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles urlBox.KeyDown
Try
If e.KeyCode = Keys.Enter Then
If urlBox.Text = "yt" Then
webBrowser.Navigate("http://www.youtube.com")
ElseIf urlBox.Text = "fb" Then
webBrowser.Navigate("http://www.facebook.com")
ElseIf urlBox.Text = "gm" Then
webBrowser.Navigate("http://www.gmail.com")
ElseIf urlBox.Text = "go" Then
webBrowser.Navigate("http://www.google.com")
Else
webBrowser.Navigate(urlBox.Text)
End If
e.SuppressKeyPress = True
End If
Catch ex As Exception
End Try
End Sub
Private Sub geticon()
Try
Dim url As Uri = New Uri(webBrowser.Url.ToString)
If url.HostNameType = UriHostNameType.Dns Then
' Get the URL of the favicon
' url.Host will return such string as www.google.com
Dim iconURL = "http://" & url.Host & "/favicon.ico"
' Download the favicon
Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL)
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim stream As System.IO.Stream = response.GetResponseStream()
Dim favicon = Image.FromStream(stream)
' Display the favicon on ToolStripLabel1
Me.favicon.Image = favicon
End If
Catch ex As Exception
Me.favicon.Image = Nothing
End Try
End Sub
Private Sub favicon_timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles favicon_timer.Tick
Try
Catch ex As Exception
End Try
End Sub
Private Sub Reload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reload.Click
webBrowser.Reload()
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
webBrowser.Navigate(My.Settings.mark1)
End Sub
Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
webBrowser.Navigate(My.Settings.mark6)
End Sub
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
webBrowser.Navigate(My.Settings.mark5)
End Sub
Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
webBrowser.Navigate(My.Settings.mark4)
End Sub
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
webBrowser.Navigate(My.Settings.mark3)
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
webBrowser.Navigate(My.Settings.mark2)
End Sub
Private Sub Fav_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
If ToolStripButton1.Text = webBrowser.DocumentTitle Then
ToolStripButton1.Text = ""
My.Settings.mark1 = ""
My.Settings.mark11 = ""
ToolStripButton1.Visible = False
End If
If ToolStripButton2.Text = webBrowser.DocumentTitle Then
ToolStripButton2.Text = ""
My.Settings.mark2 = ""
My.Settings.mark22 = ""
ToolStripButton2.Visible = False
End If
If ToolStripButton3.Text = webBrowser.DocumentTitle Then
ToolStripButton3.Text = ""
My.Settings.mark3 = ""
My.Settings.mark33 = ""
ToolStripButton3.Visible = False
End If
If ToolStripButton4.Text = webBrowser.DocumentTitle Then
ToolStripButton4.Text = ""
My.Settings.mark4 = ""
My.Settings.mark44 = ""
ToolStripButton4.Visible = False
End If
If ToolStripButton5.Text = webBrowser.DocumentTitle Then
ToolStripButton5.Text = ""
My.Settings.mark5 = ""
My.Settings.mark55 = ""
ToolStripButton5.Visible = False
End If
If ToolStripButton6.Text = webBrowser.DocumentTitle Then
ToolStripButton6.Text = ""
My.Settings.mark6 = ""
My.Settings.mark66 = ""
ToolStripButton6.Visible = False
End If
End Sub
Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
If ToolStripButton1.Text = "" Then
ToolStripButton1.Text = webBrowser.DocumentTitle
My.Settings.mark1 = webBrowser.Url.ToString
My.Settings.mark11 = webBrowser.DocumentTitle
ToolStripButton1.Visible = True
ElseIf ToolStripButton2.Text = "" Then
ToolStripButton2.Text = webBrowser.DocumentTitle
My.Settings.mark2 = webBrowser.Url.ToString
My.Settings.mark22 = webBrowser.DocumentTitle
ToolStripButton2.Visible = True
ElseIf ToolStripButton3.Text = "" Then
ToolStripButton3.Text = webBrowser.DocumentTitle
My.Settings.mark3 = webBrowser.Url.ToString
My.Settings.mark33 = webBrowser.DocumentTitle
ToolStripButton3.Visible = True
ElseIf ToolStripButton4.Text = "" Then
ToolStripButton4.Text = webBrowser.DocumentTitle
My.Settings.mark4 = webBrowser.Url.ToString
My.Settings.mark44 = webBrowser.DocumentTitle
ToolStripButton4.Visible = True
ElseIf ToolStripButton5.Text = "" Then
ToolStripButton5.Text = webBrowser.DocumentTitle
My.Settings.mark5 = webBrowser.Url.ToString
My.Settings.mark55 = webBrowser.DocumentTitle
ToolStripButton5.Visible = True
ElseIf ToolStripButton6.Text = "" Then
ToolStripButton6.Text = webBrowser.DocumentTitle
My.Settings.mark6 = webBrowser.Url.ToString
My.Settings.mark66 = webBrowser.DocumentTitle
ToolStripButton6.Visible = True
Else
End If
End Sub
Private Sub ToolStripButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton9.Click
Dim newFav As New ToolStripButton
newFav.Text = webBrowser.Url.ToString
newFav.PerformClick()
End Sub
Private Sub favClick()
End Sub
Private Sub tabForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Settings.mark1 <> "" And My.Settings.mark11 <> "" Then
ToolStripButton1.Text = My.Settings.mark11
ToolStripButton1.Visible = True
End If
If My.Settings.mark2 <> "" And My.Settings.mark22 <> "" Then
ToolStripButton2.Text = My.Settings.mark22
ToolStripButton2.Visible = True
End If
If My.Settings.mark3 <> "" And My.Settings.mark33 <> "" Then
ToolStripButton3.Text = My.Settings.mark33
ToolStripButton3.Visible = True
End If
If My.Settings.mark4 <> "" And My.Settings.mark44 <> "" Then
ToolStripButton4.Text = My.Settings.mark44
ToolStripButton4.Visible = True
End If
If My.Settings.mark5 <> "" And My.Settings.mark55 <> "" Then
ToolStripButton5.Text = My.Settings.mark55
ToolStripButton5.Visible = True
End If
If My.Settings.mark6 <> "" And My.Settings.mark66 <> "" Then
ToolStripButton6.Text = My.Settings.mark66
ToolStripButton6.Visible = True
End If
End Sub
End Class
That's all of it. But as I said earlier I could only add 6 bookmarks. Is there any way I can add an unlimited number of bookmarks. I tried something like this:
Private Sub Bookmark()
Dim mark As New ToolStripButton
mark.DisplayStyle = Text
mark.Parent = ToolStrip1 'This didn't work
End Sub
But if I did get that to work what would I put to make it navigate to that page? Please help.
I finally found the answer to this. If anybody wondering where here is the link:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/eebcf40a-dec9-41ae-8e8b-3d446cf93322/web-browser-bookmarks-bar
Have a nice day.
create a class called favorite and a setting called favorites of type system.collections.specialized.stringcollection
public class favorite
inherits toolstripsplitbutton
public mytitle as string
public myurl as string
public myimg as image
public mybrowser as webbrowser
public sub new(title as string, url as string, browser as webbrowser)
mybase.new()
mytitle = title
myurl = url
'create a getfavicon function - Google :)
myimg = GetFavicon(myurl)
mybrowser = browser
me.text = mytitle
me.tooltiptext = mytitle + " : " + myurl
me.image = myimg
end sub
private sub Favorite_Click(sender as object, e as eventargs) handles me.click
browser.navigate(myurl)
end sub
then in your form code add a sub
public sub AddFav() handles '>>the add favorite button<<.click
my.settings.favorites.add(webbrowser.documenttitle + "|" + webbrowser.url.tostring)
my.settings.save()
end sub
public sub RefreshFavs()
for each fav as string in my.settings.favorites
dim favarray as array = fav.split(new char() {"|"c})
dim x as new favorite(favarray(0), favarray(favarray.count - 1, webbrowser)
mytoolstrip.items.add(x)
next
end sub
simple explanation:
AddFav() - save favorites with page title and url
RefreshFavs() - splits each setting by the "|" which the title and url are seperated
Class Favorite() - allows you to add a splitbutton that navigates the linked browser to it's myurl variable.
hope this helps. sorry if its a little crude, I had to recall it from my memory.