473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

image and notifyicon problem

JR
Hi,

I need to change the notifyicon in a countdown. after about 3000-5000 times
there is an error.

Here is all the code. what is wrong

please help

the code needs only an notifyicon and an imagelist the timer is for changing
the icon

Jan

'start code---------------------------------

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents ImageList1 As System.Windows.Forms.ImageList

Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon

Friend WithEvents Timer1 As System.Windows.Forms.Timer

<System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container

Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)

Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)

Me.Timer1 = New System.Windows.Forms.Timer(Me.components)

'

'ImageList1

'

Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)

Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent

'

'NotifyIcon1

'

Me.NotifyIcon1.Text = "NotifyIcon1"

Me.NotifyIcon1.Visible = True

'

'Timer1

'

Me.Timer1.Enabled = True

Me.Timer1.Interval = 5

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Name = "Form1"

Me.ShowInTaskbar = False

Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

sBuild_100_Icons()

End Sub

Sub sBuild_100_Icons()

Dim rect As Rectangle = New Rectangle(0, 0, 32, 32)

Dim displayGraphics As Graphics = Me.CreateGraphics()

Dim img As Image = New Bitmap(rect.Width, rect.Height, displayGraphics)

Dim imageGraphics As Graphics = Graphics.FromImage(img)

Dim bcolor As Brush = New
SolidBrush(Color.FromKnownColor(KnownColor.Control ))

Dim cColor As Color = Color.Red

Dim fnt As New Font(Me.Font.Name, 16, FontStyle.Bold)

For X As Byte = 0 To 99

imageGraphics.FillRectangle(bcolor, rect)

imageGraphics.DrawString(Format(X, "00"), fnt, New SolidBrush(cColor), 0, _

(rect.Height - imageGraphics.MeasureString(Format(X, "00"), fnt).Height) /
2)

Dim mybitmap As Bitmap = img

Dim hIcon As IntPtr = mybitmap.GetHicon

ImageList1.Images.Add(Icon.FromHandle(hIcon))

Next

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

Static Teller As Int64

Try

Teller += 1

Catch ex As Exception

Teller = 0

End Try

Dim mybitmap As Bitmap = ImageList1.Images(Teller Mod 100) 'I have tryed
many mod values

'te next line gives an error by the 3000-5000° times

Dim hIcon As IntPtr = mybitmap.GetHicon

NotifyIcon1.Icon = Icon.FromHandle(hIcon)

Dim sw As New System.IO.StreamWriter("teller.txt", True)

sw.WriteLine(Teller)

sw.Close()

End Sub

End Class
Mar 12 '07 #1
2 2115
JR wrote:
</backposted>

It would help if you provided the actual error message.

Anyway, it *seems* to me that you're wasting resources galore when you
recreate the icon at each timer tick. I suggest you forget the
ImageList and store your icons in an array, and just assign the
appropriate icon to the NotifyIcon directly from the array. Something
like this:

<aircode>
'At form level
Private Const MAX_ICON As Integer = 99
Private mIcons(MAX_ICON) As Icon

Sub sBuild_100_Icons()

'...
'... your initializations go here
'...

Dim Brush As Brush = New SolidBrush(cColor)
Dim Bmp As Bitmap = img
For X As Byte = 0 To MAX_ICON
Dim Text As String = Format(X, "00")
Dim Height As Single = imageGraphics. _
MeasureString(Text, fnt).Height
imageGraphics.FillRectangle(bcolor, rect)
imageGraphics.DrawString( _
Text, fnt, Brush, _
0, (rect.Height - Height) / 2)

Dim hIcon As IntPtr = Bmp.GetHicon
mIcons(X) = Icon.FromHandle(hIcon)
Next
End Sub

Private Sub Timer1_Tick( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Timer1.Tick

Static Teller As Long
Try
Teller += 1
Catch Ex As Exception
Teller = 0
End Try

NotifyIcon1.Icon = mIcons(Teller Mod (MAX_ICON + 1))

'...
End Sub
</aircode>

HTH.

Regards,

Branco.

Hi,

I need to change the notifyicon in a countdown. after about 3000-5000 times
there is an error.

Here is all the code. what is wrong

please help

the code needs only an notifyicon and an imagelist the timer is for changing
the icon

Jan

'start code---------------------------------

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents ImageList1 As System.Windows.Forms.ImageList

Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon

Friend WithEvents Timer1 As System.Windows.Forms.Timer

<System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container

Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)

Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)

Me.Timer1 = New System.Windows.Forms.Timer(Me.components)

'

'ImageList1

'

Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)

Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent

'

'NotifyIcon1

'

Me.NotifyIcon1.Text = "NotifyIcon1"

Me.NotifyIcon1.Visible = True

'

'Timer1

'

Me.Timer1.Enabled = True

Me.Timer1.Interval = 5

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Name = "Form1"

Me.ShowInTaskbar = False

Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

sBuild_100_Icons()

End Sub

Sub sBuild_100_Icons()

Dim rect As Rectangle = New Rectangle(0, 0, 32, 32)

Dim displayGraphics As Graphics = Me.CreateGraphics()

Dim img As Image = New Bitmap(rect.Width, rect.Height, displayGraphics)

Dim imageGraphics As Graphics = Graphics.FromImage(img)

Dim bcolor As Brush = New
SolidBrush(Color.FromKnownColor(KnownColor.Control ))

Dim cColor As Color = Color.Red

Dim fnt As New Font(Me.Font.Name, 16, FontStyle.Bold)

For X As Byte = 0 To 99

imageGraphics.FillRectangle(bcolor, rect)

imageGraphics.DrawString(Format(X, "00"), fnt, New SolidBrush(cColor), 0,_

(rect.Height - imageGraphics.MeasureString(Format(X, "00"), fnt).Height) /
2)

Dim mybitmap As Bitmap = img

Dim hIcon As IntPtr = mybitmap.GetHicon

ImageList1.Images.Add(Icon.FromHandle(hIcon))

Next

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

Static Teller As Int64

Try

Teller += 1

Catch ex As Exception

Teller = 0

End Try

Dim mybitmap As Bitmap = ImageList1.Images(Teller Mod 100) 'I have tryed
many mod values

'te next line gives an error by the 3000-5000° times

Dim hIcon As IntPtr = mybitmap.GetHicon

NotifyIcon1.Icon = Icon.FromHandle(hIcon)

Dim sw As New System.IO.StreamWriter("teller.txt", True)

sw.WriteLine(Teller)

sw.Close()

End Sub

End Class

Mar 12 '07 #2
JR
Hi,

I have build an imagelist (sBuild_100_Icons) than I pick almays one
calulated by a mod calculation
Also remember that the code that I have send is a test duplicate to recreate
the error and asking for help

Jan

"Branco Medeiros" <br*************@gmail.comschreef in bericht
news:11**********************@8g2000cwh.googlegrou ps.com...
JR wrote:
</backposted>

It would help if you provided the actual error message.

Anyway, it *seems* to me that you're wasting resources galore when you
recreate the icon at each timer tick. I suggest you forget the
ImageList and store your icons in an array, and just assign the
appropriate icon to the NotifyIcon directly from the array. Something
like this:

<aircode>
'At form level
Private Const MAX_ICON As Integer = 99
Private mIcons(MAX_ICON) As Icon

Sub sBuild_100_Icons()

'...
'... your initializations go here
'...

Dim Brush As Brush = New SolidBrush(cColor)
Dim Bmp As Bitmap = img
For X As Byte = 0 To MAX_ICON
Dim Text As String = Format(X, "00")
Dim Height As Single = imageGraphics. _
MeasureString(Text, fnt).Height
imageGraphics.FillRectangle(bcolor, rect)
imageGraphics.DrawString( _
Text, fnt, Brush, _
0, (rect.Height - Height) / 2)

Dim hIcon As IntPtr = Bmp.GetHicon
mIcons(X) = Icon.FromHandle(hIcon)
Next
End Sub

Private Sub Timer1_Tick( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Timer1.Tick

Static Teller As Long
Try
Teller += 1
Catch Ex As Exception
Teller = 0
End Try

NotifyIcon1.Icon = mIcons(Teller Mod (MAX_ICON + 1))

'...
End Sub
</aircode>

HTH.

Regards,

Branco.

Hi,

I need to change the notifyicon in a countdown. after about 3000-5000
times
there is an error.

Here is all the code. what is wrong

please help

the code needs only an notifyicon and an imagelist the timer is for
changing
the icon

Jan

'start code---------------------------------

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents ImageList1 As System.Windows.Forms.ImageList

Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon

Friend WithEvents Timer1 As System.Windows.Forms.Timer

<System.Diagnostics.DebuggerStepThrough()Private Sub
InitializeComponent()

Me.components = New System.ComponentModel.Container

Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)

Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)

Me.Timer1 = New System.Windows.Forms.Timer(Me.components)

'

'ImageList1

'

Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)

Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent

'

'NotifyIcon1

'

Me.NotifyIcon1.Text = "NotifyIcon1"

Me.NotifyIcon1.Visible = True

'

'Timer1

'

Me.Timer1.Enabled = True

Me.Timer1.Interval = 5

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Name = "Form1"

Me.ShowInTaskbar = False

Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

sBuild_100_Icons()

End Sub

Sub sBuild_100_Icons()

Dim rect As Rectangle = New Rectangle(0, 0, 32, 32)

Dim displayGraphics As Graphics = Me.CreateGraphics()

Dim img As Image = New Bitmap(rect.Width, rect.Height, displayGraphics)

Dim imageGraphics As Graphics = Graphics.FromImage(img)

Dim bcolor As Brush = New
SolidBrush(Color.FromKnownColor(KnownColor.Control ))

Dim cColor As Color = Color.Red

Dim fnt As New Font(Me.Font.Name, 16, FontStyle.Bold)

For X As Byte = 0 To 99

imageGraphics.FillRectangle(bcolor, rect)

imageGraphics.DrawString(Format(X, "00"), fnt, New SolidBrush(cColor), 0,
_

(rect.Height - imageGraphics.MeasureString(Format(X, "00"), fnt).Height) /
2)

Dim mybitmap As Bitmap = img

Dim hIcon As IntPtr = mybitmap.GetHicon

ImageList1.Images.Add(Icon.FromHandle(hIcon))

Next

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

Static Teller As Int64

Try

Teller += 1

Catch ex As Exception

Teller = 0

End Try

Dim mybitmap As Bitmap = ImageList1.Images(Teller Mod 100) 'I have tryed
many mod values

'te next line gives an error by the 3000-5000° times

Dim hIcon As IntPtr = mybitmap.GetHicon

NotifyIcon1.Icon = Icon.FromHandle(hIcon)

Dim sw As New System.IO.StreamWriter("teller.txt", True)

sw.WriteLine(Teller)

sw.Close()

End Sub

End Class


Mar 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: petterl | last post by:
I have tried to find the error in the code below but I always get " An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module. Additional information: Object...
2
by: Rob Mayo | last post by:
OK, maybe this is my opinion, maybe these are bugs. Given the folowing: I have a NotifyIcon on my Form, a Context menu associated with the NotifyIcon, and a MenuItem on the ContextMenu set as...
0
by: Mike Allen | last post by:
Hi Everyone, My problem concerns ContextMenus. It's easy enough to create one and apply it to a NotifyIcon. When it's compiled and run, it works beautifully. Right click and there you are. But I...
3
by: Glen | last post by:
Can anyone tell me if there is a workable method to get the mouse cursor position on the screen or the NotifyIcon position? I need to display a context menu for the NotifyIcon when clicked and I'd...
12
by: mdb | last post by:
My app has a notify icon in the systray. I want the left mouse click to initiate a menu, and a right-mouse click to do something else. Normally, for a button, I would listen to MouseDown and if I...
2
by: Amjad | last post by:
Hi, I'm using the NotifyIcon to process some text files in the background periodically. I want to display one icon in the task bar when the program is in stand by mode, and I want to display...
2
by: stillcoding | last post by:
Hi I have a problem with memory usage increasing when I right click to display the context menu on a notifyIcon OS: Windows XP Professional SP2 Software: VS2005 Professional edition SP1 The...
1
by: \Ji Zhou [MSFT]\ | last post by:
Hello Jason, Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou and I will be working on this issue with you. I have tried to but cannot reproduce your issue on my side....
8
by: starrysky | last post by:
I have a program which puts an icon in the notification area and has a menu associated with it available by right clicking on the icon. I want the menu items to be selected by single left clicks but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.