|
First please use the 'code block' or 'inline code' for posting code. Makes it easier to read the code.
As to your problem, you don't need the paint event. Using this event is why you have the endless loop.
Paint event is triggered every time the form repaints (logic isn't it ) and since you call a dialog in your paint event every time that dialog closes your form is repainted so your dialog is opened again and so on...
Suggestion:
Remove your paint event completely.
Draw your image in your button click event and go from there (might I suggest a picture box to display your image.)
|
|
|
|
|
Oh sorry, i will post in code block next time.
The reason i cant do that, is because im using this application as a sort of paint...and once i loaded the photo into a picture and started painting on the form, i couldnt paint over top of the picturebox...but if i load the picture directly onto the form, i am able to pain over it, and thats where my problem is.
If there is a way to paint on top of a picturebox, i would have done that, but i couldn't see a way to do that. I can load a picture onto the form and paint on it just fine if i explicitly tell it where the file loacation is, but using the openfiledialog into the mix seems to really mess it up and I just can't figure out how to fix it..
Thanks for your help!
|
|
|
|
|
Ah so your not only painting the image in that case your right you can't use the picture box.
you might want to check out Christian Graus his articles here[^]
I think it was him who had a couple on this sort of thing.
As to your immediate problem I would do the following:
In your button click (and only there) open your file dialog, and save the selected image into a global variable for that form.
In the paint event draw that image and do nothing more than that, so don't open the file dialog in there.
Pseudo code:
private img as image
private sub button_click ...
if openfiledialog.showdialog = ok then
img = image.fromfile(openfiledialog.getpath)
end if
end sub
private sub paint_event....
'draw the img object
end sub
|
|
|
|
|
I figured it out with the help of several different forums thanks alot guys! Heres the code i ended up with in case your curious.
Imports System.IO
Imports System.Windows.Forms.MouseEventArgs
Imports System.Windows.Forms.Form
Imports System.Windows.Forms.PaintEventArgs
Public Class Edtor
Private gfx As Graphics
Public user As String
Private pathset As Boolean
Dim button2 As Global.System.Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.Visible = True
Me.Visible = False
End Sub
Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
End Sub
Private Sub Edtor_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
gfx.Dispose()
End Sub
Private Sub Edtor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gfx = Me.CreateGraphics()
End Sub
Public Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
If (Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Me.SaveFileDialog1.FileName
End If
End Sub
Public Sub Edtor_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If RadioButton1.Checked = True Then
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
Dim rect As Rectangle
With rect
.X = e.X - 1
.Y = e.Y - 1
.Height = 2
.Width = .Height / .Height
gfx.DrawRectangle(System.Drawing.Pens.Red, rect)
End With
End If
If RadioButton2.Checked = True Then
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
Dim rect As Rectangle
With rect
.X = e.X - 1
.Y = e.Y - 1
.Height = 2
.Width = .Height / .Height
gfx.DrawRectangle(System.Drawing.Pens.Blue, rect)
End With
End If
If RadioButton3.Checked = True Then
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
Dim rect As Rectangle
With rect
.X = e.X - 1
.Y = e.Y - 1
.Height = 2
.Width = .Height / .Height
gfx.DrawRectangle(System.Drawing.Pens.Black, rect)
End With
End If
End Sub
Private Sub drawonform(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If pathset = True Then
If button2 = True Then
If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Form1.openDiag.FileName
End If
End If
' Create image.
Dim Newimage As Image = Image.FromFile(user)
' Create Point for upper-left corner of image.
Dim ulCorner As New Point(100, 100)
' Draw image to screen.
e.Graphics.DrawImage(Newimage, ulCorner)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonpic.Click
If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Form1.openDiag.FileName
pathset = True
End If
End Sub
End Class
|
|
|
|
|
treashunter wrote: I figured it out
Nice thread to read. 5ved.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Still not quite right I'm afraid, though good work.
You still have the dialog popping up in the Paint event, and that is a no-no.
All you need is to handle the Shown event, or override the OnShown method. In that method invoke Me.Button2.PerformClick() . The Paint event then just checks if the path has been set yet.
|
|
|
|
|
treashunter wrote: i will post in code block next time
Yes, please do. It makes it easier to read. Since you are new, I'll be a nice guy about it
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
treashunter wrote: Public Sub drawonform(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Call Button2_Click(sender, e)
Okay, this is the main problem. The paint event (especially of the form) can happen many times per second even, and is an uncontrolled (by you) event. That means it will happen whether you want to or not.
So in your code, as is, the code in your button click event will be executed just as many times (whether you want to or not).
In other words, do not use the Paint event, but rather the Form's Load event, or if absolutely necessary, VisibleChanged , in which case you need to check whether the form is being hidden or made visible.
And a good opportunity to practice good coding techniques:
As a rule of thumb, do not call an event from within other events just to run the code inside that event. Rather create a separate sub and move the code in the event to it. So in your case:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonpic.Click
OpenPictureDialog()
End Sub
Public Sub drawonform(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
OpenPictureDialog()
End Sub
Private Sub OpenPictureDialog()
Try
If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Form1.openDiag.FileName
End If
Catch ex as Exception
MsgBox(ex.ToString)
End Try
End Sub
My advice is free, and you may get what you paid for.
|
|
|
|
|
As others have mentioned, do not have this type of operation inside a procedure that handles the Paint event as Paint event does happen often and you cannot control when it is triggered or not. From what I read of your description of what you are trying to achieve, you should be putting the code to open your open dialog in a button click event procedure for the button in question.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
In vb6.0, how can i check Internet connection. that is. Is net connected or not.
Send source code please
|
|
|
|
|
Tufail Ahmad wrote: Send source code please
Private Declare Function InternetGetConnectedState Lib "wininet.dll" _
(ByRef dwflags As Long, ByVal dwReserved As Long) As Long
Public Function IsOnline() As Boolean
Dim LFlags As Long
IsOnline = InternetGetConnectedState(LFlags, 0&)
End Function
I are Troll
|
|
|
|
|
IsOnline = InternetGetConnectedState(LFlags, 0&)
In the above line, LFlags returns some values, what does it mean.
Note: I am connected to Internet through LAN
|
|
|
|
|
|
Private Const FLAG_ICC_FORCE_CONNECTION = &H1
Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
Function Check_Internet_Connection() As Boolean
If InternetCheckConnection("http://www.google.com", FLAG_ICC_FORCE_CONNECTION, 0&) = 0 Then
Check_Internet_Connection = False
Else
Check_Internet_Connection = True
End If
End Function
Best Regards,
SOFTDEV
If you have knowledge, let others light their candles at it
|
|
|
|
|
Tufail Ahmad wrote: Send source code please
Sorry, it is not how this site works. People will offer help and hints as to what to do, but to just throw out code, no.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Hi eveyone,
I,m working on a program where I want to save numbers to a file and then be able to later have my program be able to tell me which numbers occur most often with in this file. I have managed to write to a text file and i even figured out how to read how many lines the file has. I,m stuck on retreiving the data. I,m using vsual basic 2008 by the way. Would it be better to use a database?? Thanks for any help you all can be.
Sincerely,
A-dogg
|
|
|
|
|
A-dogg2009 wrote: Would it be better to use a database
Depends on the volume of data and how much you want to manipulate it.
A-dogg2009 wrote: stuck on retreiving the data
Take a look at the SYSTEM.IO name space, filestreams
A-dogg2009 wrote: which numbers occur most often with in this file
Read up on LINQ, its perfect for this
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
|
Hi every one
I 'm using SQL DMO to connect database in my computer
But i can't connect to my database
Here my code:
Dim sever As New SQLDMO.SQLServer
sever.LoginSecure = True
sever.Connect("local")
how to connect to my local sever
I'm using SQLEXPRESS
thanks
|
|
|
|
|
this[^] contains the magic line you need to connect.
|
|
|
|
|
There's a walkthrough here; http://www.sqldbatips.com/showarticle.asp?ID=34.
Your server-instance is probably named "(local)\SQLEXPRESS"
I are Troll
|
|
|
|
|
Please help me to answer the following question! Thanks!
1.In VB.NET, when datasource of datagridview = datatable(Datatable contains data).On the datagridview display a grid of data ,the end row is empty.
What type of value of the cells on this row in VB.NET?How to check ?
2.Can I make the end row disappear?
|
|
|
|
|
2. Set:
DataGridView1.AllowUSerToAddRows = False
and
DataGridView1.AllowUserToDeleteRows = False
I don't speak Idiot - please talk slowly and clearly
'This space for rent'
Driven to the arms of Heineken by the wife
|
|
|
|
|
Hi,
I'm currently having a big problem with freehand drawing.
I want to do the following thing: I want to draw 2 freehand lines both with different colors and I need the final line to be on the top of the other lines created before. To store the lines I use 2 GraphicsPath objects, one GraphicsPath per color.
I tried to solve this problem by using two arrays like this:
Public MousePath0() As System.Drawing.Drawing2D.GraphicsPath
Public MousePath1() As System.Drawing.Drawing2D.GraphicsPath
My idea was to make a system which will remember the order of all lines and draw them correctly, but when I add an graphics path to the array and when I try to add line to this graphics path VB 2008 gives me an error "Object reference not set to an instance of an object."
Please help, I don't want to use exactly a GraphicsPath for the lines, it can be something different.
Thank you!
|
|
|
|
|
Hello All,
I am hoping someone could steer me in the right direction with a problem I am having.
I created a small app that sucessfully consumes a Web Service and traps the response. I am able to read individual pieces of the document but when it comes to arrays, I am having the most difficult time.
Below I have posted the XML response and what I am looking to store is the values in ref1 and ref2. But I don't know how to get at them. I see a reference to these 2 elements in the SearchServiceNames and ServiceLastModificationDates tags but I am totally lost on how to read and hook the two up.
Any help would be most appreciated. I thank you for your time and comments.
<?xml version="1.0"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<ns1:GetSearchServicesResponse SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://www.dwdev.com/Webservices/Freezerworks/">
<SearchServicesCount>3</SearchServicesCount>
<SearchServiceNames href="#ref-1"/>
<SearchServiceLastModificationDates href="#ref-2"/>
</ns1:GetSearchServicesResponse>
<SOAP-ENC:Array SOAP-ENC:arrayType="xsd:string[3]" id="ref-1">
<item>GetIDs</item>
<item>russtest</item>
<item>russtestservice</item>
</SOAP-ENC:Array>
<SOAP-ENC:Array SOAP-ENC:arrayType="xsd:date[3]" id="ref-2">
<item>2009-11-17</item>
<item>2009-11-16</item>
<item>2009-11-16</item>
</SOAP-ENC:Array></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
|
|
|
|