Promote Your Blog
Google

Friday, March 23, 2012

How To Continue Numbering in Enumerate From Previous List?

To pause and resume, enumerate using following (taken from a website and works wonderfully)

\newcounter{tempcounter}

\begin{document}

\begin{enumerate}
\item first;

\item second;
\setcounter{tempcounter}{\value{enumi}}
\end{enumerate}

This is a paragraph.

\begin{enumerate}
\setcounter{enumi}{\value{tempcounter}}
\item third;

\item and so on...
\end{enumerate}
\end{document}

Remember, \newcounter{tempcounter} goes before \begin{document}

How To Change Page Orientation From Portrait To Landscape

Default page style is Portrait in LaTex. You can change it to Landscape using:

\usepackage{lscape}

\newpage
\begin{landscape}
.....
.....
.....
\end{landscape}
\newpage

After \end{landscape}, page orientation will again be default one i.e. Portrait

How To Change Default Style For Enumerate and Itemize


Default style can for enumerate can be changed using
\renewcommand{label}{style} command

where 'label' may be any of the following:

  1. \labelenumi: first level
  2. \labelenumii: second level
  3. \labelenumiii: third level
  4. \labelenumiv: fourth level
style may be any combination of characters and numbers. The item number for each list may be printed using by using any of the following (from first depth to fourth depth):

  1. enumi
  2. enumii
  3. enumiii
  4. enumiv
These numbers may be styled with the following macros:

  1. \alph{number}: lowercase letters
  2. \Alph{number}: uppercase letters
  3. \arabic{number}: numbers
  4. \roman{number}: lowercase roman numerals
  5. \Roman{number}: uppercase roman numerals

Try following:

\renewcommand{\labelenumi}{\arabic{enumi}. }
\renewcommand{\labelenumii}{\labelenumi\alph{enumii}) }
\renewcommand{\labelenumiii}{\labelenumii\roman{enumiii}: }

This would be added to the top of the document, before \begin{document}.

From TexBlog

\usepackage{enumerate}



\begin{enumerate}[I]%for capital roman numbers.

\item

\end{enumerate}

\begin{enumerate}[(a)]%for small alpha-characters within brackets.

\item

\end{enumerate}

For changing style for itemize, try these:
  • \renewcommand{\labelitemi}{$\bullet$}
  • \renewcommand{\labelitemii}{$\cdot$}
  • \renewcommand{\labelitemiii}{$\diamond$}

\renewcommand{\labelitemiv}{$\ast$}

Friday, March 2, 2012

How To Send Email using vb.net

Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential("your gmail id", "your email password")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("your gmail id same as above")
mail.To.Add("recipient email id")
mail.Subject = subject
mail.Body = "Dear User, " & Environment.NewLine & Environment.NewLine & eb & Environment.NewLine & Environment.NewLine & "Regards," & Environment.NewLine & "HowToCodeIt"
SmtpServer.Send(mail)
MsgBox("Notification emailed successfully")
Catch ex As Exception
MsgBox(ex.ToString)
End Try

Note that SMTP details are for GMAIL. If you want to send email from some other provider say Yahoo or Rediff then google internet for their SMTP Port and Host details and do approp. replacement in the above code...

Also, as you can see that body is read from windows controls..same can be done for subject and email address of recipient etc.

Database Operations (CRUD) Using OleDB in vb.net

In this post, you will learn how to use OleDb to:

  • Connect to data source
  • Add records to database
  • Modify an existing record
  • Read the desired record
  • Read all records
  • Delete a particular record
  • Delete all records from database

Imports System.Data.OleDb

Dim conString As String

Dim con As OleDbConnection

Dim dr As OleDbDataReader



//CONNECTION

conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=w:\newdata.mdb"

con = New OleDbConnection(conString)

con.Open()

//ADD

Dim strSql As String

Try

strSql = "insert into books values('" & tbBook.Text & "','" & tbAuthor.Text & "')"

Dim cmd As OleDbCommand = New OleDbCommand(strSql, con)

Dim icount As Integer = cmd.ExecuteNonQuery

If icount > 0 Then

MsgBox(tbBook.Text & " book added to your library")

DB2List(lstBooks)

Else

MsgBox(tbBook.Text & " cannot be added to your library")

End If

Catch ex1 As Exception

MsgBox(ex1.Message)

End Try

//MODIFY

Dim strSql As String

Try

strSql = "update books set author = '" & tbAuthor.Text & "' where book = '" & tbBook.Text & "'"

Dim cmd As OleDbCommand = New OleDbCommand(strSql, con)

Dim icount As Integer = cmd.ExecuteNonQuery

If icount > 0 Then

MsgBox(tbBook.Text & " author updated")

DB2List(lstBooks)

Else

MsgBox(tbBook.Text & " author cannot be updated")

End If

Catch ex1 As Exception

MsgBox(ex1.Message)

End Try

//DELETE

Dim selItem As String

Dim strSql As String

For i = 0 To lstBooks.Items.Count - 1

If lstBooks.GetSelected(i) Then

selItem = lstBooks.GetItemText(lstBooks.Items(i))

End If

Next

strSql = "delete from books where book='" & selItem & "'"

Dim cmd As OleDbCommand = New OleDbCommand(strSql, con)

Dim icount As Integer = cmd.ExecuteNonQuery

If icount > 0 Then

MsgBox(selItem & " deleted sucessfully")

DB2List(lstBooks)

Else

MsgBox(selItem & " cannot be deleted")

End If

//DELETE ALL

Dim strSql As String

strSql = "delete * from books"

Dim cmd As OleDbCommand = New OleDbCommand(strSql, con)

Dim icount As Integer = cmd.ExecuteNonQuery

If icount > 0 Then

MsgBox("All books deleted successfully")

DB2List(lstBooks)

Else

MsgBox("All books cannot be deleted")

End If

//READ ALL

Private Sub DB2List(ByRef lb As ListBox)

Dim strSql As String = "select * from books"

Dim dadapter As OleDbDataAdapter

dadapter = New OleDbDataAdapter()

dadapter.SelectCommand = New OleDbCommand(strSql, con)

Dim dset As DataSet = New DataSet()

dadapter.Fill(dset, "Books")

lb.DataSource = dset

lb.DisplayMember = "Books.Book"

End Sub

//READ SELECTED

Private Sub RetrievConditional(ByVal bk As String)

Dim strSql As String = "select * from books where book = '" & bk & "'"

Dim cmd As OleDbCommand = New OleDbCommand(strSql, con)

dr = cmd.ExecuteReader

While dr.Read()

tbBook.Text = dr(0)

tbAuthor.Text = dr(1)

End While

End Sub

Code is self explanatory...

How To Display Selected Picture in vb.net

In this post, you will learn, how to:

  • Use OpenFileDialog to choose Jpeg files only
  • How to display selected jpg image

OpenFileDialog2.InitialDirectory = Directory.GetCurrentDirectory

OpenFileDialog2.Title = "Select a File"

OpenFileDialog2.FileName = ""

OpenFileDialog2.Filter = "Jpeg Files (*.jpg)|*.jpg"

OpenFileDialog2.FilterIndex = 1

If OpenFileDialog2.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then

PictureBox1.Image = Image.FromFile(OpenFileDialog2.FileName)

Else

MsgBox(“You Approp. Msg”)

End If

How To Use Environment Variables In vb.net

To set environment variable, right click my computer > Properties > Advanced > Environment Variables

Dim envVar As String = Environment.GetEnvironmentVariable("One of set environment variable")

How To Use .NET Charting Control

.NET provides a wonderful control for charting plots. Here are basic steps:

  • Drag and drop CHART control from toolbox to your application form/dialog etc.
  • Position it appropriately
  • There can be multiple plots (CHART AREAS) on a Chart
  • Add SERIES to Collection and assign each series to appropriate CHART AREAS
  • Choose type of plot - Line, Bar,....
  • Set Legends
  • Set Titles
  • Label Axis
  • IsValueIndexed must be set to TRUE for X-Axis

Don't worry!!! A detailed tutorial is on the way that will teach you how to do all these steps..illustrating through actual snapshots.

Then, there are some operations on chart that you do programmatically. Some of these operations are:

Imports System.Drawing.Printing

Imports System.IO

Imports System.Windows.Forms.DataVisualization.Charting

Show/Hide

Chart1.Show()

Chart1.Hide()


Clearing Points in a Chart Series

Chart1.Series("Name given to series at design time").Points.Clear()


Setting width from Series line

Chart1.Series("Name given to series at design time ").BorderWidth =


Adding Points to a Series

Chart1.Series("Name given to series at design time ").Points.AddXY(x, y)

Saving Output As Image

Chart1.SaveImage(“meaningful filename”, ChartImageFormat.Jpeg)

How To Count Number of Records In A .csv File

You are right!!! Just put a counter inside the while loop reading .csv file row by row i.e.

Dim ir As Integer = 0

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(fn)

MyReader.TextFieldType = FileIO.FieldType.Delimited

MyReader.SetDelimiters(",")

Dim currentRow As String()

While Not MyReader.EndOfData

Try

currentRow = MyReader.ReadFields()

ir = ir + 1

Catch ex As Microsoft.VisualBasic.

FileIO.MalformedLineException

MsgBox("Line " & ex.Message &

"is not valid and will be skipped.")

End Try

End While

End Using

How To Read A .csv File

Suppose I have .csv (Comma Separated values) file and I need to read certain columns of this file and plot them (related post: using .NET Chart Control). So, here is the code to read .csv files:

Imports System.IO.FileInfo

Imports System.IO

Dim x As Double, y1 As Double, y2 As Double

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(fn) 'fn =.csv File name

MyReader.TextFieldType = FileIO.FieldType.Delimited

MyReader.SetDelimiters(",")

Dim currentRow As String()

Dim i As Integer

Dim j As Integer

j = 0

x = 0.0

While Not MyReader.EndOfData

Try

currentRow = MyReader.ReadFields()

Dim currentField As String

If j <> 0 Then ‘Skipping Header Row

i = 0

For Each currentField In currentRow

If i = 0 Then ‘First column

x = currentField

ElseIf i = 4 Then ‘Fifth column

y1 = currentField

ElseIf i = 5 Then

y2 = currentField

End If

i = i + 1

Next

‘Do the operation on retrieved values. E.g. plotting them using .Chart Control (see relevant post)

x = x + 1

End If

j = j + 1

Catch ex As Microsoft.VisualBasic.

FileIO.MalformedLineException

MsgBox("Line " & ex.Message &

"is not valid and will be skipped.")

End Try

End While

End Using

How To Add Selected Items In A ListBox To A ListViewBox

In ListViewBox, each item can have different color and also ListViewBox can display columnar data.

Let us see code to:

  • Access selected items in ListBox
  • Set content & color of item to be added in ListViewBox
  • Clear items, Add items and set display properties of a ListViewBox

Dim sc() As Color = {Color.Blue, Color.Green, Color.Orange, Color.Black, Color.Red, Color.Chocolate, Color.DarkMagenta, Color.DarkKhaki, Color.Crimson, Color.HotPink, Color.Firebrick, Color.Goldenrod, Color.ForestGreen, Color.IndianRed, Color.DarkSalmon}

Form1.ListView1.View = View.Details

Dim ic As Integer = 0

Form1.ListView1.Items.Clear()

For Each it In lstDirList.SelectedItems

Dim x As New ListViewItem

x.Text = it.ToString

x.ForeColor = sc(ic)

Form1.ListView1.Items.Add(x)

ic = ic + 1

Next


How To Read Files With Given Extension In All Directories In Given Path

To read all the files with ".csv" extension in all the directories under a folder selected through FileBrowseDialog, the following code will work:

Imports System.IO.FileInfo

Imports System.IO

Dim Dir As String

Dim fni As FileInfo

lstDirList.Items.Clear()

For Each Dir In System.IO.Directory.GetDirectories(folderDlg.SelectedPath)

For Each fi In System.IO.Directory.GetFiles(Dir)

fni = My.Computer.FileSystem.GetFileInfo(fi)

If fni.Extension = ".csv" Then

stDirList.Items.Add(fi)

End If

Next

Next

You should also note how to:

  • Get names of all directories in a path
  • Names of all files in these directories iteratively
  • Information about a file
  • Comparison of file extension with desired one
  • How to clear data items from a list box and how to add items to listbox

How To Use FolderBrowseDialog in vb.net

Sometimes, your VB app need to get a selected folder for further operations. Say, a folder where to install your app. In this case and many alike situations, you can make use of FolderBrowseDialog.
Following code snippet shows how to use FolderBrowseDialog:

Imports System.Windows.Forms

Imports System.IO

Dim folderDlg As New FolderBrowserDialog

folderDlg.ShowNewFolderButton = False

folderDlg.SelectedPath = "C:\My Path"

If (folderDlg.ShowDialog() = DialogResult.OK) Then

MessageBox(folderDlg.SelectedPath)

End If