vbdotnet.it

Tricks & tips, workaround, forum and ideas with .Net

About the author

Enrico Rossini è il gestore di questo blog.
E-mail me Send mail

Recent posts

Recent comments

Contributi

Best 6 ~ 6 users ~ 6 comments

Info legali

Le opinioni espresse in questo blog sono strettamente personali e ogni persona è responsabile dei commenti che inserisce. I marchi citati sono delle rispettive aziende.

© Copyright 2010

Advertising


FindControl ricorsivo

Imports System.Collections.Generic
 
Private Shared Function FindControlIterative(ByVal root As Control, _
        ByVal id As String) As Control
 
        Dim ctl As Control = root
        Dim ctls As LinkedList(Of Control) = New LinkedList(Of Control)
 
        Do While (ctl IsNot Nothing)
            If ctl.ID = id Then
                Return ctl
            End If
            For Each child As Control In ctl.Controls
                If child.ID = id Then
                    Return child
                End If
                If child.HasControls() Then
                    ctls.AddLast(child)
                End If
            Next
            ctl = ctls.First.Value
            ctls.Remove(ctl)
        Loop
 
        Return Nothing
 
End Function

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Wednesday, October 28, 2009 4:54 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Conversione progetto da .NET 1.x: errore allowDefinition = 'MachineToApplication'

Nel convertire un progetto .NET 1.x alla versione 2.0 o superiore, ci si imbatte quasi sempre in questo criptico errore. "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level This error can be caused by a virtual directory not being configured as an application in IIS."  Il problema è che la conversione crea una cartella Backup con all'interno un file web.config che va in conflitto con quello presente nella cartella principale.

Cancellando la cartella Backup il problema viene risolto.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Friday, October 23, 2009 3:35 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Windows Mobile: path di esecuzione dell'applicazione

Come trovare il path dell'applicazione corrente su Windows Mobile?

Public Function AppPATH() As String
   Return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules(0).FullyQualifiedName)
End Function

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Tuesday, October 13, 2009 12:53 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Windows Mobile: rilevare la versione

Come riuscire a rilevare la versione del sistema operativo installato sul proprio palmare?

If System.Environment.OSVersion.Version.Major < 5 Then

MessageBox.Show("Versione 2003")

Else

MessageBox.Show("Versione 5.0")

End If

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Tuesday, October 13, 2009 12:34 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Creare un combobox con ricerca

Per quanto hai cercato un combobox con la possibilità di ricerca? Ecco una semplice implementazione:

Private Sub CombBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
        
Dim findString As String = String.Empty
        
If (Not e.KeyChar.IsLetterOrDigit(e.KeyChar, 0)) Then Exit Sub
        findString = e.KeyChar
        
With ComboBox1
            .SelectedIndex = ComboBox1.FindString(findString)
        
End With
        If ComboBox1.SelectedIndex > -1 Then e.Handled = True
End
Sub

Ciao a tutti!

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Tuesday, September 15, 2009 12:14 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Cercare un elemento in un combobox

Chissà a quanti è capitato di dover cercare all'interno di un combobox un elemento presente (non un valore). Per risolvere il problema il comando da scrivere è piuttosto semplice:

Me.cb.SelectedIndex = Me.cb.FindString("valore")

Il comando FindString presente tra le proprietà del combobox restituisce un intero che è la posizione del valore cercato all'interno degli elementi presenti nel combobox. In questo caso si è scelto di selezionare il valore trovato.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Tuesday, September 15, 2009 12:03 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Focus ad un TextBox

Ciao a tutti e buon Ferragosto! (a chi nn lavora). Oggi un veloce tricks: a me è capitato di dover dare il focus ad un textbox ma non compare il cursore all'interno dello stesso.

La funzione più scontata sarebbe TextBox.Focus(). Invece quella che funziona meglio è TextBox.Select(). Provate e verificate voi stessi!

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Saturday, August 15, 2009 12:47 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Formattazione con String.Format

formatting strings

exampleoutput
String.Format("--{0,10}--", "test"); --      test--
String.Format("--{0,-10}--", "test"); --test      --

formatting numbers

specifiertypeformatoutput
(double 1.2345)
output
(int -12345)
c currency {0:c} £1.23 -£12,345.00
d decimal
(whole number)
{0:d} System.FormatException -12345
e exponent / scientific {0:e} 1.234500e+000 -1.234500e+004
f fixed point {0:f} 1.23 -12345.00
g general {0:g} 1.2345 -12345
n number {0:n} 1.23 -12,345.00
r round trippable {0:r} 1.23 System.FormatException
x hexadecimal {0:x4} System.FormatException ffffcfc7

custom number formatting

specifiertypeformatoutput
(double 1234.56)
0 zero placeholder {0:00.000} 1234.560
# digit placeholder {0:#.##} 1234.56
. decimal point placeholder {0:0.0} 1234.6
, thousand separator {0:0,0} 1,235
% percentage {0:0%} 123456%

date formatting

specifiertypeoutput
(June 8, 1970 12:30:59)
d Short Date 08/06/1970
D Long Date 08 June 1970
t Short Time 12:30
T Long Time 12:30:59
f Full date and time 08 June 1970 12:30
F Full date and time (long) 08 June 1970 12:30:59
g Default date and time 08/06/1970 12:30
G Default date and time (long) 08/06/1970 12:30:59
M Day / Month 8 June
r RFC1123 date string Mon, 08 Jun 1970 12:30:59 GMT
s Sortable date/time 1970-06-08T12:30:59
u Universal time, local timezone 1970-06-08 12:30:59Z
Y Month / Year June 1970

custom date formatting

specifiertypeoutput
(June 8, 1970 12:30:59)
dd Day 08
ddd Short Day Name Mon
dddd Full Day Name Monday
hh 2 digit hour 12
HH 2 digit hour (24 hour) 12
mm 2 digit minute 30
MM Month 06
MMM Short Month name Jun
MMMM Month name June
ss seconds 59
tt AM/PM PM
yy 2 digit year 70
yyyy 4 digit year 1970
: seperator, e.g. {0:hh:mm:ss} 12:30:59
/ seperator, e.g. {0:dd/MM/yyyy} 08/06/1970

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: asp.net | Generalità | vb.net
Posted by enrico on Friday, August 07, 2009 11:15 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Directory del programma

Ciao, questo è un semplice tricks. Molto spesso accade che si vuole sapere in quale directory gira il nostro programma. COn questa semplice riga lo si sa.

Private myFileName As String = System.Reflection.Assembly.GetExecutingAssembly().Location
myFileName = myFileName.Substring(0, myFileName.LastIndexOf("\"))

Attenzione. La funzione System.Reflection.Assembly.GetExecutingAssembly().Location restituisce anche il nome del file eseguibile. Per questo è stato necessario aggiungere la riga sottostante. Buon lavoro.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Tuesday, July 28, 2009 12:46 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Invio email con framework 3.5

Con il .NET framework 3.5 è stato introdotto un nuovo namespace Mail per l’invio delle email: questo namespace può essere aggiunto ai nostri programmi con la seguenti riga di codice:

Imports System.Net.Mail

L’invio dell’email può presentare qualche problema poiché non si trovano molti esempi su come inviare ad esempio degli allegati o ricevere una conferma di lettura. Nell’esempio che segue cercherò di rispondere a questi problemi.

Si supponga di avere un form con i seguenti textbox: txtTo per inserire l’email a cui spedire, txtSubject per inserire l’oggetto del messaggio, txtMessage per il corpo del messaggio e un listbox di nome lstAttachment per visualizzare l’elenco degli allegati. Un bottone per l’invio ed un checkbox per voler spedire il messaggio come HTML.

Dim objSmtpMail As System.Net.Mail.SmtpClient       ' variabile generale per l'invio dell'email
Dim Attachment As System.Net.Mail.Attachment        ' variabile per salvare gli allegati
Dim Mailmsg As New System.Net.Mail.MailMessage()    ' variabile per creare il corpo del messaggio
 
Private _displayname As String = ""                 ' variabile che contiene il nome dell'email da visualizzare
Private _from As String = ""                        ' variabile con l'indirizzo email da cui spedire
Private _smtpserver As String = ""                  ' variabile contenente l'indirizzo del server mail
 
Public Sub SendEmail()
   ' imposta le proprietà del server
   objSmtpMail = New System.Net.Mail.SmtpClient(_smtpserver)
 
   ' definisce il mittente 
   ' (primo parametro l'indirizzo email, secondo il nome che deve apparire all'utente che riceve l'email)
   Mailmsg.From = New System.Net.Mail.MailAddress(_from, _displayname)
 
   ' per specificare indirizzi multipli separare uno dall'altro con il ;
   Mailmsg.To.Add(New System.Net.Mail.MailAddress(Me.txtTo.Text.Trim))
 
   ' specifica formato
   If Me.tsbHTML.Checked = True Then
      ' spedisce l'email in formato HTML
      Mailmsg.IsBodyHtml = True
   Else
      Mailmsg.IsBodyHtml = False
   End If
 
   ' aggiunge degli header all'email che consentono di ricevere la conferma di lettura
   Mailmsg.Headers.Add("Reply-To", Mailmsg.From.Address)
   Mailmsg.Headers.Add("Disposition-Notification-To", Mailmsg.From.Address)
   Mailmsg.Headers.Add("Return-Receipt-To", Mailmsg.From.Address)
 
   ' aggiunge il testo
   Mailmsg.Subject = txtSubject.Text
   Mailmsg.Body = txtMessage.Text
 
   ' aggiunte gli allegati
   For Counter = 0 To lstAttachment.Items.Count - 1
      Attachment = New System.Net.Mail.Attachment(lstAttachment.Items(Counter))
      Mailmsg.Attachments.Add(Attachment)
   Next
 
   ' spedisce effettivamente l'email
   objSmtpMail.Send(Mailmsg)
End Sub

Spero vi sia utile. Ciao!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Tuesday, July 21, 2009 12:03 PM
Permalink | Comments (0) | Post RSSRSS comment feed