-
Jul 30th, 2024, 01:51 PM
#1
Thread Starter
New Member
Problem enabling Graphic commands
Good day everyone.
When I clicked "start", the program ran successfully. When I added the lines starting with "myBrush' and "myFont" in the btnAdd_Click Sub Procedure, two things happened:
1. In the line "Private Sub btnAdd_Click . . ." , e got associated with "EventArgs", whereas in the main form, the line "Private Sub Form1_Paint. . .", e got associated with "PaintEventArgs".
2. The statement "Imports System.Net.Mime.MediaTypeNames" got added above the line "Public Class Form1".
The net effect of the above observations was that the graphics commands (myBrush, myFontand Drawstring) are underlined in red and don't work.
How do I regain the ability to use the graphic commands to paint text on the screen in the btnAdd sub procedure?
On a related matter (?) when I clicked on the Add button, the lbx31 listbox became visible, but when I released the mouse button the listbox reverted back to "not visible" status and disappeared from the screen. What textbox property do I modify which will allow the textbox to remain visible after I release the mouse button?
Thanks again in advance.
Note that The first line in the code "Imports System . . ." only appeared in the code after I added the "myBrush and myFont commands to the btnAdd_Click sub procedure.
Code:
Code:
Imports System.Net.Mime.MediaTypeNames
Public Class Form1
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim myFont As Font = New Font("Arial", 14, FontStyle.Regular)
Dim myBrush As Brush = New SolidBrush(Color.Yellow)
Dim x As Integer
Dim y As Integer
Dim Mnth As String
lbxMonth.Enabled = False
lbxMonth.Visible = False
lbxMonth.Location = New Point(500, 152)
lbx31.Enabled = False
lbx31.Visible = False
lbx30.Enabled = False
lbx30.Visible = False
lbx29.Enabled = False
lbx29.Visible = False
lbx28.Enabled = False
lbx28.Visible = False
lblEvent.Visible = False
lblMonth.Visible = False
lblDay.Visible = False
tbxEvent.Enabled = False
tbxEvent.Visible = False
lblCurrentMonth.Text = GetMonth()
lblCurrentYear.Text = GetYear()
If Dir("C:\Users\nicho\Documents\VisualBasic\Planner\Data\Jan_Events.txt") <> "" Then
Else
Intlz() 'Creates null files for each month
End If
Mnth = lblCurrentMonth.Text
LoadArrays(Mnth)
myBrush = New SolidBrush(Color.White)
myFont = New Font("Arial", 11, FontStyle.Regular)
y = 50
x = 20
For i = 1 To 31
myBrush = New SolidBrush(Color.White)
e.Graphics.DrawString(i & Chr(46), myFont, myBrush, x, y)
If StatusRay(i) = "1" Then
myBrush = New SolidBrush(Color.Lime)
Else
myBrush = New SolidBrush(Color.Red)
End If
e.Graphics.DrawString(EventRay(i), myFont, myBrush, x + 25, y)
y = y + 20
Next i
End Sub
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
lbx31.Enabled = True
lbx31.Visible = True
myBrush = New SolidBrush(Color.White)
myFont = New Font("Arial", 11, FontStyle.Regular)
End Sub
End Class
Last edited by jmcilhinney; Jul 30th, 2024 at 09:16 PM.
-
Jul 30th, 2024, 09:17 PM
#2
Re: Problem enabling Graphic commands
I have formatted your code for you. Please don't post unformatted code snippets as they are too hard to read.
-
Jul 30th, 2024, 09:38 PM
#3
Re: Problem enabling Graphic commands
You need to do some reading about variable scope. When you declare a variable, it exists only within the scope of the block in which it is declared.
1. In the line "Private Sub btnAdd_Click . . ." , e got associated with "EventArgs", whereas in the main form, the line "Private Sub Form1_Paint. . .", e got associated with "PaintEventArgs".
There's no "association" going on. Those are two different methods so they are two different scopes. Variables that exist within one have zero connection to variables that exist in the other. In both cases, 'e' is a method parameter and, like all method parameters or any other variable, they are declared as the type they need to be to serve their intended purpose. By convention, all event handlers will have two parameters: 'sender As Object' and 'e As EventArgs'. The first one is always a reference to the object that raised the event and the second is a reference to an object that contains the data for the event. Many event have no data, so the 'e' parameter is declared as type EventArgs, which is basically a placeholder. If an event does have data then 'e' will be declared as a type that inherits EventArgs and adds members to access and use that data.
2. The statement "Imports System.Net.Mime.MediaTypeNames" got added above the line "Public Class Form1".
It would not have been added for that reason. The only reason that that would have been added would be if you used a name that appeared to be a nested type within that type. Also, I'm pretty sure that you would have been prompted to add it, rather than it just being added automatically. It doesn't appear to be needed now anyway, so you can almost certainly remove it.
The net effect of the above observations was that the graphics commands (myBrush, myFontand Drawstring) are underlined in red and don't work.
Nope. They don't work because your code doesn't make sense, because you don't understand scope. Those variables are declared in the Form1_Paint method so they only exist within that scope. You can't refer to them in a different method because they don't exist in a different method. You could declare variables with those names in that method but they would be different variables. It would be just like someone else using the name JPSR at a different web site. If you want to be able to refer to the same variables in multiple different methods then they have to be declared outside any of those methods, i.e. at the class level.
Your code is very broken even apart from that though. You are doing waaaaayyyyyyy too much work in that Paint event handler. There should be nothing but drawing code in there. Checking files and folders and changing the states of controls should be done elsewhere. The Paint event can be raised multiple times in quick succession so doing all that work over and over is just crazy. Also, you should not be creating multiple identical objects over and over. How many times in that code do you execute 'myBrush = New SolidBrush(Color.White)' and not once do you dispose one of them. If you were going to do it that way, you should be creating one object for the event and disposing it when you're done with it, e.g.
Code:
Using brush As New SolidBrush(Color.White)
'Use brush here.
End Using
You shouldn't do it that way though. Not for that brush, at least. The system already provides what you need via Brushes.White, so you should be using that and letting the system manage its lifetime. For brushes with custom colours that the system doesn't provide or other disposable objects, e.g. that Font, you should be doing as I showed above.
-
Jul 31st, 2024, 01:12 PM
#4
Thread Starter
New Member
Re: Problem enabling Graphic commands
First, thank you for responding to my post. I downloaded VB 2022 this month so I am obviously very inexperienced with the language as you can tell.
i have implemented some of your suggestions to the best of my ability and the program still runs successfully. By moving the "Dim" statements for myFont and myBrush up to the Class level, I can utilize those features in the btnAdd method without any issues.
However when I added the line "Graphics.Drawstring . . " the IDE flagged it stating that "Reference to a non-shared member requires an object reference." How do I remedy this?
I also have the following issue with the listbox:
When I click on the "Add" button the listbox becomes visible. When I release the mouse button the listbox is still visible providing the cursor is still on the "Add" button. As soon as the cursor moves way from the "Add" button the listbox is no longer visible.
Thanks for your patience.
Code:
Code:
Public Class Form1
Dim myFont As Font = New Font("Arial", 14, FontStyle.Regular)
Dim myBrush As Brush = New SolidBrush(Color.Yellow)
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim x As Integer
Dim y As Integer
Dim Mnth As String
ScreenSetup()
Mnth = lblCurrentMonth.Text
LoadArrays(Mnth)
myBrush = New SolidBrush(Color.White)
myFont = New Font("Arial", 11, FontStyle.Regular)
y = 50
x = 20
For i = 1 To 31
Using myBrush = New SolidBrush(Color.White)
e.Graphics.DrawString(i & Chr(46), myFont, myBrush, x, y)
End Using
If StatusRay(i) = "1" Then
Using myBrush = New SolidBrush(Color.Lime)
e.Graphics.DrawString(EventRay(i), myFont, myBrush, x + 25, y)
End Using
Else
Using myBrush = New SolidBrush(Color.Red)
e.Graphics.DrawString(EventRay(i), myFont, myBrush, x + 25, y)
End Using
End If
y = y + 20
Next i
End Sub
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
lbx31.Enabled = True
lbx31.Visible = True
lblMonth.Visible = True
Dim x As Single, y As Single, i As Integer
myBrush = New SolidBrush(Color.White)
myFont = New Font("Arial", 11, FontStyle.Regular)
Graphics.DrawString("", myFont, myBrush, x + 25, y)
End Sub
Last edited by jmcilhinney; Jul 31st, 2024 at 09:47 PM.
-
Jul 31st, 2024, 09:48 PM
#5
Re: Problem enabling Graphic commands
For the second time, I have formatted your code. Please it yourself in future. If you make your questions hard to read through lack of effort, you're likely to see a lack of effort to help you too.
-
Jul 31st, 2024, 09:54 PM
#6
Re: Problem enabling Graphic commands
Put ALL your drawing code and ONLY your drawing code in the Paint event handler. Any time you want to force a repaint, you call Invalidate on the appropriate control; the form in your case. Invalidate tells the control what part of its area needs to be repainted and to raise a Paint event. If you call Invalidate without any argument then the whole control will be repainted. That's OK if it's not happening a lot but, even then, it's good practice to calculate the smallest area that has or might have changed and invalidate only that area. Your entire drawing code will be executed every time but only the invalidated area will be repainted on screen. It is the painting of pixels that is the slow part so that's why you want to minimise that area. Slow painting leads to flickering if it's done often.
I suggest that you follow the CodeBank link in my signature below and check out some of my threads on drawing to get some more information and ideas.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|