Change Text Value on Runtime

R

Radek Budaø

Hi all,
i have trouble with changing text value of textbox on runtime. I use this
control to display process information about sending e-mail per smtp. I use
external component

which can hadle events by sending mail.

I have function, which can calculate status of process in percent

Progress Function is called every time, when sending status is changed,
function calculates correctly 'percent' value, but on form i cannot change
default text. Can you anybody help me? Below i attached all source code for
this form.

I have no onLoad Function for this form defined. Is it problem? How can I
changed text value of text box on runtime?

Than you for your help
Radek Budar

Imports System.Text

Imports devBiz.Net.Mail

Public Class frmRegistrationRequest

Dim frmMailStatusForm as New frmMailSendingStatus

Private Sub sendRequest( ByVal sender As System.Object, ByVal e As
System.EventArgs) _

Handles btnSendRegReq.Click


Dim strPrijmeni as String

Dim strJmeno as String

Dim strMesto as String

Dim strEmail as String

Dim sql as String

Dim firstRun as Date

Dim regNrUser as Double


Dim mailMsg As MailMessage = New MailMessage

mailMsg.From.EMail = "(e-mail address removed)"

mailMsg.To.Add("XXX","(e-mail address removed)")

Dim now as Date

strPrijmeni = txtPrijmeni.Text

strJmeno = txtJmeno.Text

strMesto = txtMesto.Text

strEmail = txtEmail.Text

Dim sb As New StringBuilder()

sb.Append(strPrijmeni)

sb.Append(",")

sb.Append(strJmeno)

sb.Append(",")

sb.Append(strMesto)

sb.Append(",")

sb.Append(strEmail)

sb.Append(",")

sb.Append(regNrUser)


mailMsg.Subject = "test - " & now

mailMsg.Body = sb.ToString

Dim client As smtp = New smtp

client.Host = "smtp.seznam.cz"

client.Port = 25

' We will capture the progress event, so we attach an event handler

AddHandler client.OnProgress, AddressOf Progress


txtMailStatus.Visible = True

'E-mail sending

Try

client.SendMessage(mailMsg)

Catch ex As System.Net.Mail.SmtpException

txtMailStatus.ForeColor = Color.Red

txtMailStatus.Text = "Failure!"

MsgBox("Odeslání e-mailu se nezdaøilo, zkontrolujte pøipojení k internetu!"
& vbNewLine & vbNewLine + ex.Message, MsgBoxStyle.Exclamation, "Chyba pøi
odesílání registraèního e-mailu")

Exit Sub

Finally

End Try


txtMailStatus.ForeColor = Color.Green

txtMailStatus.text = "Done...success!"


stavRegistrace = 3

Me.Close

End Sub



Public Sub Progress(ByVal sender As Object, ByVal e As ProgressEventArgs)

Dim percent As Integer = _

CType((CType(e.Position / e.Length * 100, Double)), Integer)

txtMailStatus.Text = percent

End Sub

End Class
 
A

Armin Zingler

Radek Budaø said:
Progress Function is called every time, when sending status is
changed, function calculates correctly 'percent' value, but on form
i cannot change default text. Can you anybody help me? Below i
attached all source code for this form.

What do you mean with "default text"? I guess, "txtMailStatus.Text =
percent" is executed but the display isn't updated, right? If this is the
case: There can be two different reasons depending on how the smtp class
works (see it's documentation).

If the OnProgress event is raised in a different thread, you must call
txtMailStatus.Invoke to marshal updating the textbox to the right thread.
Only the thread that created a control is allowed to access it.

If it's not raised in a different thread, the display isn't updated because
your application is still busy in "client.SendMessage(mailMsg)" and there's
no time to update the display. In earlier Win versions, you could force
updating the display by calling txtMailStatus.Refresh but this can fail
under WinXP after the application is not "responsive" after few seconds. In
this case you can either create your own thread to send the mail - then you
do have to call txtMailStatus.Invoke to update the textbox - or, you can use
a simple workaround by calling the PeekMessage API function (faking the
responsiveness of your application) in addition to calling
txtMailStatus.Refresh.

Armin
 
R

Radek Budar

What do you mean with "default text"? I guess, "txtMailStatus.Text =
percent" is executed but the display isn't updated, right? If this is the

Yes, correct. Thank you for your advice, i will try it as soon as possible.
Can you say me still one...? How can I find, that onProgress event is raised
in a different thread...? Sorry, I am rookie :)

Thanks
 
A

Armin Zingler

Radek Budar said:
Yes, correct. Thank you for your advice, i will try it as soon as
possible. Can you say me still one...? How can I find, that
onProgress event is raised in a different thread...? Sorry, I am
rookie :)

In the documentation on the class. Or: In Sub Progress, examine the return
value of Me.InvokeRequired. If it is True, the event is raised from a
different thread.


Armin
 
R

Radek Budar

Hallo,
i tried it and...Me.InvokeRequired is 'false', also it is not running in
separate thread (i muss say FORTUNATELLY, because use of Invoke method in
now out of my capacity :). But I made any changes in my Progress procedure,
so text box is now updating...When, after every text box updating, show
message box, text in text box is updated. I dont know why but i know, that
is for me unacceptable. Here is core:

Public Sub Progress(ByVal sender As Object, ByVal e As ProgressEventArgs)
Dim percent As Integer = _

CType((CType(e.Position / e.Length * 100, Double)), Integer)

txtMailStatus.Text = percent

MsgBox("Status: " & percent)

End Sub

When I delete 'MsgBox("Status: " & percent)', no update of text box
'txtMailStatus' is displayed!

Any advice now, please...

Best regards

Radek Budar
 
R

Radek Budar

Hallo
i tried InvokeRequired and result is 'False'. So, case one, other thread,
is out. I made a little change in my procedure 'progress', after every text
box change, i display message box. Message box displays percent value. After
every display of message box, is text box also updated! I dont know why...?
After delete MessageBox, is no text changed!
Here is my changed procedure. Can you give me advice, what is incorrenc or
why after display message box is text box also updated?

Public Sub Progress(ByVal sender As Object, ByVal e As ProgressEventArgs)

Dim percent As Integer = _

CType((CType(e.Position / e.Length * 100, Double)), Integer)

txtMailStatus.Text = percent

MsgBox("Status: " & percent)

End Sub
 
A

Armin Zingler

Radek Budar said:
Hallo
i tried InvokeRequired and result is 'False'. So, case one, other
thread, is out. I made a little change in my procedure 'progress',
after every text box change, i display message box. Message box
displays percent value. After every display of message box, is text
box also updated! I dont know why...? After delete MessageBox, is no
text changed!
Here is my changed procedure. Can you give me advice, what is
incorrenc or why after display message box is text box also updated?

If you display the messagebox, the application has time to update the
display, including the textbox. To solve this, see my comment "If it's not
raised in a different thread..."

If you're interested in basic information:
http://msdn.microsoft.com/library/e...ssagequeues/aboutmessagesandmessagequeues.asp

http://msdn.microsoft.com/library/en-us/gdi/pantdraw_1xd1.asp


Armin
 
R

Radek Budar

Thanks for rich information and for links. I solved the problem follower:

Public Sub Progress(ByVal sender As Object, ByVal e As ProgressEventArgs)
Dim percent As Integer = _
CType((CType(e.Position / e.Length * 100, Double)), Integer)
txtMailStatus.Text = percent
Me.Refresh
End Sub

I tested it on WXP, and it looks right :)

Thank you still one and wish happy new year

Radek Budar
 
A

Armin Zingler

Radek Budar said:
Thanks for rich information and for links. I solved the problem
follower:

Public Sub Progress(ByVal sender As Object, ByVal e As
ProgressEventArgs) Dim percent As Integer = _
CType((CType(e.Position / e.Length * 100, Double)), Integer)
txtMailStatus.Text = percent
Me.Refresh
End Sub

I tested it on WXP, and it looks right :)


Good to hear, but if you cover and uncover your Form, or click onto it once,
the progress might be gone. This might take some seconds til the OS
considers your application to be hung (although it isn't).
Thank you still one and wish happy new year

Wish you the same.



Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top