Thursday, 24 October 2013

A SIMPLE DRAG AND DROP HOW TO EXAMPLE

A Simple Drag And Drop How To Example

Introduction

Believe it or not, I've never written an application that's needed to use drag & drop. I'm working on something now where I'd like to use that feature, and so I went searching for a simple example of how d&d works. Amazingly, I couldn't find one--all the examples I found seemed to deal with tree controls or didn't deal specifically with how to use d&d. While simple, there are some things that I discovered when writing a little demonstration program, so I decided to write a beginner's tutorial on d&d.

Creating A Simple Image Viewer

I've decided to illustrate d&d with a simple, single image viewer capable of viewing JPG, BMP, and PNG files.

The STAThread Attribute

The very first step is to ensure that your Main method is designated with the [STAThread] attribute. You won't be able to do drag and drop operations if this is missing (in fact, the program will display an error message if you try to run it--not by the program's doing, but by .NET's).
[STAThread]
static void Main() 
{
  Application.Run(new Form1());
}

The AllowDrop Property

The first step is to identify the control for which you want to enable d&d and set the AllowDrop property to true. This is usually the application's Form control. For a Form, this is a simple matter of setting the AllowDrop property in the designer to true:
But what if you want to enable d&d for a particular control, say a PictureBox? Oddly enough, this property, defined in the base Control class, isn't available in the properties of the PictureBox:
Nor, if you have:
PictureBox pb=new PictureBox();
and you start typing:
pb.Allow
will Intellisense auto-complete. Yet, this doesn't mean that you can't manually assign this property! It seems like thePictureBox is the only control that doesn't expose this property, for no reason that I found.
In any case, since the demonstration application doesn't do anything else besides display an image, it makes sense in this case to enable d&d for the form rather than just the PictureBox.

The Drag And Drop Events

The next step is to wire-up the d&d events (note that the PictureBox control doesn't show these events either in the designer, but yes, they are there and you can use them).
Since we're adding d&d capability to the form, we can use the designer to add our methods for the four events we're almost always interested in:
  • DragEnter
  • DragOver
  • DragDrop
  • DragLeave

The DragEnter Event

This is simple enough--this event fires when the mouse enters a control in which d&d is enabled. It gives you the opportunity to:
  • inspect the DragEventArgs to see the DataObject being dragged onto your control is something you can accept.
  • perform any initialization you may need to handle the data.
  • set the Effect property to indicate whether the item will be copied or moved to the control.
DragEventArgs
The DragEventArgs contains several properties that we are interested in:
  • AllowedEffect - The operations that are allowed by the source of the drag event. What we're primarily interested in is whether the source allows copying or moving.
  • Data - The IDataObject that actually contains the data in which we are interested.
  • Effect - the target effect that we want to set for this object.
  • XY - the mouse position. This is in screen coordinates.
  • KeyState - can be used to inspect the state of the SHIFT, CTRL, and ALT keys as well as mouse buttons.
In my example, I'm interested in two things:
  • can the object be copied into my application?
  • is the data in a format that my application accepts?
and in particular, the application accepts images dragged from Explorer, so I'm interested in the filename. As a result, the OnDragEnter method starts like this:
private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
  Debug.WriteLine("OnDragEnter");
  string filename;
  validData=GetFilename(out filename, e);
where all the real work is done in the GetFilename method:
protected bool GetFilename(out string filename, DragEventArgs e)
{
  bool ret=false;
  filename=String.Empty;

  if ( (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
  {
    Array data=((IDataObject)e.Data).GetData("FileName") as Array;
    if (data != null)
    {
      if ( (data.Length ==1) && (data.GetValue(0) is String) )
      {
        filename=((string[])data)[0];
        string ext=Path.GetExtension(filename).ToLower();
        if ( (ext==".jpg") || (ext==".png") || (ext==".bmp") )
        {
          ret=true;
        }
      }
    }
  }
  return ret;
}
First, I test whether the source allows copying of the data. Second, I get the data based on the filename. When dropping an image, there are several formats that the data supports, which can be inspected in the debugger:
We see that "FileName" is one of the acceptable formats, so I'm going to request that data in the FileName format. However, we're not done yet. The object returned is actually an Array, as evident in the watch window:
So, what I want to do is:
verify that there is only one datum being dropped (rather than a collection of filenames) and, while probably overkill, I want to verify that the data type of this one and only one entry is a string. Once I've done all this verification, I can get the filename, extract the extension, and confirm that it's either ".jpg", ".bmp", or ".png". If all is well, the method returns true, indicating success, along with the filename.
Managing The Data
Now that we have the filename, what do we do with it? In my application, I'd like to display a small image to give the user some feedback, showing the picture about to be dropped onto the application. This is a good example that illustrates that sometimes you'll have to manage the data intelligently. I wanted to deal with two problems:
  • what if the image being dragged is already the same image the application is displaying?
  • loading the image into the application is a time consuming process, and I don't want to bog the application's main thread down by loading it.
The rest of the OnDragEnter method handles this logic, in addition to determining whether we set the drag effect toCopy. Here's the complete method:
private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
  Debug.WriteLine("OnDragEnter");
  string filename;
  validData=GetFilename(out filename, e);
  if (validData)
  {
    if (lastFilename != filename)
    {
      thumbnail.Image=null;
      thumbnail.Visible=false;
      lastFilename=filename;
      getImageThread=new Thread(new ThreadStart(LoadImage));
      getImageThread.Start();
    }
    else
    {
      thumbnail.Visible=true;
    }
    e.Effect=DragDropEffects.Copy;
  }
  else
  {
  e.Effect=DragDropEffects.None;
  }
}
Notice that I first check whether image being displayed is the one again being dragged onto the application. If so, we have the image and we're all set to display the thumbnail (there's a potentially small bug in this code--can you figure it out?)
If it's a new image, I'm going to start up a thread that actually loads the image and puts it into the thumbnail when it's ready. Let's look at that thread and related functions:
public delegate void AssignImageDlgt();

protected void LoadImage()
{
  nextImage=new Bitmap(lastFilename);
  this.Invoke(new AssignImageDlgt(AssignImage));
}

protected void AssignImage()
{
  thumbnail.Width=100;
  // 100 iWidth
  // ---- = ------
  // tHeight iHeight
  thumbnail.Height=nextImage.Height * 100 / nextImage.Width;
  SetThumbnailLocation(this.PointToClient(new Point(lastX, lastY)));
  thumbnail.Image=nextImage;
}
The image is loaded into nextImage. Because we're manipulating a control that is being managed in the application thread, I'm using the Invoke method to execute a delegate so that the actual manipulation of the thumbnail control is done in the application thread rather than our worker thread. This is the "safe" way to manipulate UI objects.

The DragOver Event

Something I didn't realize about the DragOver event is that it fires repeatedly, even if I don't move the mouse. Depending on what our application does, there may be considerable time taking up in updating the visual cues when the user moves the mouse. We certainly don't want to waste CPU time when the user hasn't moved the mouse. Therefore, I'm testing if the position of the mouse has changed:
private void OnDragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
  Debug.WriteLine("OnDragOver");
  if (validData)
  {
    if ( (e.X != lastX) || (e.Y != lastY) )
    {
      SetThumbnailLocation(this.PointToClient(new Point(e.X, e.Y)));
    }
  }
}
Again, we're calling a separate method to actually set the thumbnail position:
protected void SetThumbnailLocation(Point p)
{
  if (thumbnail.Image==null)
  {
    thumbnail.Visible=false;
  }
  else
  {
    p.X-=thumbnail.Width/2;
    p.Y-=thumbnail.Height/2;
    thumbnail.Location=p;
    thumbnail.Visible=true;
  }
}
This method determines the thumbnail position and its visibility, based on whether the nextImage exists (it won't exist until the worker thread has finished loading it).

The DragLeave Event

When the mouse leaves our application, we want to hide any visual cues that are currently being displayed:
private void OnDragLeave(object sender, System.EventArgs e)
{
  Debug.WriteLine("OnDragLeave");
  thumbnail.Visible=false;
}

The DragDrop Event

In this event handler, we want to verify that the data being dropped is valid. This is easy, using the validData flag that was set with the OnDragEnter event handler. But if the worker thread hasn't finished loading the image, we need to wait. And because the worker thread uses the Invoke method, we can't just use the Join method, putting the application in a wait state until the worker thread is complete. Doing so prevents the Invoke method from executing. Therefore, we have to test whether the thread is still alive and tell the application to continue processing events:
private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
  Debug.WriteLine("OnDragDrop");
  if (validData)
  {
    while (getImageThread.IsAlive)
    {
      Application.DoEvents();
      Thread.Sleep(0);
    }
    thumbnail.Visible=false;
    image=nextImage;
    AdjustView();
    if ( (pb.Image != null) && (pb.Image != nextImage) )
    {
      pb.Image.Dispose();
    }
    pb.Image=image;
  }
}
After we are ensured that the worker thread has terminated, I'm adjusting the application's PictureBox so that it displays the image in the correct proportions. I also manually dispose of any previous image to free up the image resources now, rather than waiting for garbage collection to do so. But I have to make sure that if the user drags the same image as being viewed into the application, I don't actually dispose of that image, as it's the current image!

Conclusion

While drag and drop is itself simple, some planning has to go into:
  • the visual cues that you provide the user.
  • how to work with those visual cues if they are initially time consuming to set up.
  • how you manage those visual resources during the drag and drop process.
  • how to optimize for repetitive actions.
  • how to clean up after the drop is made.
These are things that I think are useful to bring to a beginner's attention. It's not so simple after all!

Thursday, 17 October 2013

MEDICAL STORE MANAGEMENT SYSTEM

Medical store management system ( VB )

In   proposed   system, the  management  needs  not  to  keep  any  type  of  registers,  which  they  use  to  keep  in  old  one.  They have work only with one computer.  All the details are stored in computer files.  The  dual  entries  are  done  very  quickly  as  entry  in  one  file  only ,  affects  the  other  file  where  it  has  to  record.  In  the  way  there  is  no  need  to  record  the  computer  operator.


Source Code

Login

Imports System.Data.OleDb
Imports System.IO

Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As Integer = 0
        Try
            cmd = New OleDbCommand("select * from login where id='" & Trim(LCase(TextBox1.Text)) & "' and pword='" & Trim(LCase(TextBox2.Text)) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows() Then
                While rs.Read
                    x = 1
                End While
            End If
            rs.Close()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
        If x = 1 Then
            Timer1.Enabled = True
            '  Me.Close()
        Else
            MsgBox("       Invalid user details      ", MsgBoxStyle.Information, "Error")
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBar1.Value = ProgressBar1.Value + 5
        If ProgressBar1.Value = 100 Then
            home.Show()
            Timer1.Enabled = False
            Me.Hide()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

Expenses and income


Imports System.Data.OleDb
Imports System.IO
Public Class expen

    Private Sub expen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim re As OleDbDataAdapter
        Dim ds As New DataSet
        Try
            ds.Reset()
            ds.Clear()
            re = New OleDbDataAdapter("SELECT [mid] as Medicine_Id, [mname] as Medicine_Name, [cname] as Company_Name,  [quan] as Quantity,[taxa] as Tax_Amount,[tot] as Total FROM med", con)
            con.Open()
            re.Fill(ds)
            con.Close()
            DataGridView2.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.Message(), MsgBoxStyle.Critical, "Error(s) Found")
        End Try

        Dim re1 As OleDbDataAdapter
        Dim ds1 As New DataSet
        Try
            ds1.Reset()
            ds1.Clear()
            re1 = New OleDbDataAdapter("SELECT [billno] as Bill_Number, [Patient] as Patient_Name, [netamount] as Net_Amount FROM savebill", con)
            con.Open()
            re1.Fill(ds1)
            con.Close()
            DataGridView1.DataSource = ds1.Tables(0)
        Catch ex As Exception
            MsgBox(ex.Message(), MsgBoxStyle.Critical, "Error(s) Found")
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

End Class    

Home

Imports System.Data.OleDb
Imports System.IO

Public Class home

    Private Sub LogoutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogoutToolStripMenuItem.Click
        Form1.Show()
        Form1.TextBox1.Text = ""
        Form1.TextBox2.Text = ""
        Form1.ProgressBar1.Value = 0
        Me.Hide()
    End Sub

    Private Sub EndToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndToolStripMenuItem.Click
        End
    End Sub

    Private Sub PasswordChangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasswordChangeToolStripMenuItem.Click
        pwordchange.Show()
    End Sub

    Private Sub home_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GroupBox1.Visible = False
        '  Form1.Close()
    End Sub

    Private Sub MedicineEntryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MedicineEntryToolStripMenuItem.Click
        medentry.Show()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GroupBox1.Visible = True
        Try
            cmd = New OleDbCommand("select * from billno", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox1.Text = rs(0)
                End While
            End If
            rs.Close()
            con.Close()
            TextBox1.Text += 1
            cmd = New OleDbCommand("update billno set numb ='" & Trim(TextBox1.Text) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            cmd = New OleDbCommand("select * from billno", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox1.Text = rs(0)
                End While
            End If
            rs.Close()
            con.Close()
            TextBox1.Text += 1
            cmd = New OleDbCommand("update billno set numb ='" & Trim(TextBox1.Text) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
        ComboBox1.Text = ""
        ComboBox2.Text = ""
        ComboBox3.Text = ""
        ComboBox4.Text = ""
        ComboBox5.Text = ""
        ComboBox6.Text = ""
        TextBox10.Text = ""
        TextBox11.Text = ""
        TextBox12.Text = ""
        TextBox13.Text = ""
        TextBox14.Text = ""
        TextBox15.Text = ""
        TextBox16.Text = ""
        TextBox17.Text = ""
        TextBox18.Text = ""
        TextBox19.Text = ""
        TextBox2.Text = ""
        TextBox20.Text = ""
        TextBox21.Text = ""
        TextBox3.Text = ""
        TextBox5.Text = ""
        TextBox4.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        TextBox9.Text = ""
    End Sub

    Private Sub TextBox14_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox14.TextChanged
        TextBox20.Text = Val(TextBox3.Text) * Val(TextBox14.Text)
    End Sub

    Private Sub TextBox13_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox13.TextChanged
        TextBox19.Text = Val(TextBox4.Text) * Val(TextBox13.Text)
    End Sub

    Private Sub TextBox12_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.TextChanged
        TextBox18.Text = Val(TextBox5.Text) * Val(TextBox12.Text)
    End Sub

    Private Sub TextBox11_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox11.TextChanged
        TextBox17.Text = Val(TextBox6.Text) * Val(TextBox11.Text)
    End Sub

    Private Sub TextBox10_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged
        TextBox16.Text = Val(TextBox7.Text) * Val(TextBox10.Text)
    End Sub

    Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
        TextBox15.Text = Val(TextBox8.Text) * Val(TextBox9.Text)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        GroupBox1.Visible = False
    End Sub

  
    Private Sub TextBox20_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox20.TextChanged
        TextBox21.Text = Val(TextBox15.Text) + Val(TextBox16.Text) + Val(TextBox17.Text) + Val(TextBox18.Text) + Val(TextBox19.Text) + Val(TextBox20.Text)
    End Sub

    Private Sub TextBox19_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox19.TextChanged
        TextBox21.Text = Val(TextBox15.Text) + Val(TextBox16.Text) + Val(TextBox17.Text) + Val(TextBox18.Text) + Val(TextBox19.Text) + Val(TextBox20.Text)

    End Sub

    Private Sub TextBox18_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox18.TextChanged
        TextBox21.Text = Val(TextBox15.Text) + Val(TextBox16.Text) + Val(TextBox17.Text) + Val(TextBox18.Text) + Val(TextBox19.Text) + Val(TextBox20.Text)

    End Sub

    Private Sub TextBox17_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox17.TextChanged
        TextBox21.Text = Val(TextBox15.Text) + Val(TextBox16.Text) + Val(TextBox17.Text) + Val(TextBox18.Text) + Val(TextBox19.Text) + Val(TextBox20.Text)

    End Sub

    Private Sub TextBox16_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox16.TextChanged
        TextBox21.Text = Val(TextBox15.Text) + Val(TextBox16.Text) + Val(TextBox17.Text) + Val(TextBox18.Text) + Val(TextBox19.Text) + Val(TextBox20.Text)

    End Sub

    Private Sub TextBox15_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox15.TextChanged
        TextBox21.Text = Val(TextBox15.Text) + Val(TextBox16.Text) + Val(TextBox17.Text) + Val(TextBox18.Text) + Val(TextBox19.Text) + Val(TextBox20.Text)

    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
        Try
            cmd = New OleDbCommand("select * from med", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    ComboBox1.Items.Add(rs(1))
                    ComboBox2.Items.Add(rs(1))
                    ComboBox3.Items.Add(rs(1))
                    ComboBox4.Items.Add(rs(1))
                    ComboBox5.Items.Add(rs(1))
                    ComboBox6.Items.Add(rs(1))
                End While
            End If
            rs.Close()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox1.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox14.Text = Trim(rs(5))
                End While
            End If
            rs.Close()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox2.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox13.Text = Trim(rs(5))
                End While
            End If
            rs.Close()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox3.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox12.Text = Trim(rs(5))
                End While
            End If
            rs.Close()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox4.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox11.Text = Trim(rs(5))
                End While
            End If
            rs.Close()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub ComboBox5_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox5.SelectedIndexChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox5.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox10.Text = Trim(rs(5))
                End While
            End If
            rs.Close()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub ComboBox6_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox6.SelectedIndexChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox6.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox9.Text = Trim(rs(5))
                End While
            End If
            rs.Close()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox1.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox22.Text = Trim(rs(4))
                End While
            End If
            TextBox23.Text = Val(TextBox22.Text) - Val(TextBox3.Text)
            rs.Close()
            con.Close()
            cmd = New OleDbCommand("update med set quan='" & Trim(TextBox23.Text) & "' where mname='" & Trim(ComboBox1.Text) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
        TextBox20.Text = Val(TextBox3.Text) * Val(TextBox14.Text)
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox2.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox25.Text = Trim(rs(4))
                End While
            End If
            TextBox24.Text = Val(TextBox25.Text) - Val(TextBox4.Text)
            rs.Close()
            con.Close()
            cmd = New OleDbCommand("update med set quan='" & Trim(TextBox24.Text) & "' where mname='" & Trim(ComboBox2.Text) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
        TextBox19.Text = Val(TextBox4.Text) * Val(TextBox13.Text)
    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox3.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox27.Text = Trim(rs(4))
                End While
            End If
            TextBox26.Text = Val(TextBox27.Text) - Val(TextBox5.Text)
            rs.Close()
            con.Close()
            cmd = New OleDbCommand("update med set quan='" & Trim(TextBox26.Text) & "' where mname='" & Trim(ComboBox3.Text) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
        TextBox18.Text = Val(TextBox5.Text) * Val(TextBox12.Text)
    End Sub

    Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox4.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox29.Text = Trim(rs(4))
                End While
            End If
            TextBox28.Text = Val(TextBox29.Text) - Val(TextBox6.Text)
            rs.Close()
            con.Close()
            cmd = New OleDbCommand("update med set quan='" & Trim(TextBox28.Text) & "' where mname='" & Trim(ComboBox4.Text) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
        TextBox17.Text = Val(TextBox6.Text) * Val(TextBox11.Text)
    End Sub

    Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox5.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox31.Text = Trim(rs(4))
                End While
            End If
            TextBox30.Text = Val(TextBox31.Text) - Val(TextBox7.Text)
            rs.Close()
            con.Close()
            cmd = New OleDbCommand("update med set quan='" & Trim(TextBox30.Text) & "' where mname='" & Trim(ComboBox5.Text) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
        TextBox16.Text = Val(TextBox7.Text) * Val(TextBox10.Text)
    End Sub

    Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
        Try
            cmd = New OleDbCommand("select * from med where mname='" & Trim(ComboBox6.Text) & "'", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    TextBox33.Text = Trim(rs(4))
                End While
            End If
            TextBox32.Text = Val(TextBox33.Text) - Val(TextBox8.Text)
            rs.Close()
            con.Close()
            cmd = New OleDbCommand("update med set quan='" & Trim(TextBox32.Text) & "' where mname='" & Trim(ComboBox6.Text) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
        TextBox15.Text = Val(TextBox8.Text) * Val(TextBox9.Text)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            cmd = New OleDbCommand("insert into savebill values('" & Trim(TextBox1.Text) & "','" & Trim(TextBox2.Text) & "','" & Trim(TextBox21.Text) & "')", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub StockDetailsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StockDetailsToolStripMenuItem.Click
        stock.Show()
    End Sub

    Private Sub ExpensesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExpensesToolStripMenuItem.Click
        expen.Show()
        expen.TabControl1.SelectedIndex = 2
    End Sub

    Private Sub IncomeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IncomeToolStripMenuItem.Click
        expen.Show()
        expen.TabControl1.SelectedIndex = 1
    End Sub
End Class



Medicine entry

Imports System.Data.OleDb
Imports System.IO

Public Class medentry
    Dim id As Integer

    Private Sub medentry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            cmd = New OleDbCommand("select * from mid", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    id = rs(0)
                End While
            End If
            rs.Close()
            con.Close()
            id += 1
            TextBox1.Text = "MID" & id
            cmd = New OleDbCommand("update mid set id ='" & Trim(id) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            cmd = New OleDbCommand("insert into med values('" & Trim(TextBox1.Text) & "','" & Trim(TextBox2.Text) & "','" & Trim(TextBox3.Text) & "','" & Trim(TextBox4.Text) & "','" & Trim(TextBox5.Text) & "','" & Trim(TextBox6.Text) & "','" & Trim(TextBox7.Text) & "','" & Trim(TextBox8.Text) & "')", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""
            GroupBox1.Visible = False
            GroupBox2.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        GroupBox1.Visible = True
        GroupBox2.Visible = True
        Try
            cmd = New OleDbCommand("select * from mid", con)
            con.Open()
            rs = cmd.ExecuteReader()
            If rs.HasRows Then
                While rs.Read
                    id = rs(0)
                End While
            End If
            rs.Close()
            con.Close()
            id += 1
            TextBox1.Text = "MID" & id
            cmd = New OleDbCommand("update mid set id ='" & Trim(id) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
        TextBox8.Text = Val(TextBox5.Text) * Val(TextBox6.Text)
    End Sub
End Class

Password change

Imports System.Data.OleDb
Imports System.IO
Public Class pwordchange

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            cmd = New OleDbCommand("update login set pword='" & Trim(TextBox3.Text) & "' where id='" & Trim(TextBox1.Text) & "'", con)
            con.Open()
            cmd.ExecuteNonQuery()
            Me.Close()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub pwordchange_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
Stock maintance source
Imports System.Data.OleDb
Imports System.IO

Public Class stock

    Private Sub stock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim re As OleDbDataAdapter
        Dim ds As New DataSet
        Try
            ds.Reset()
            ds.Clear()
            re = New OleDbDataAdapter("SELECT [mid] as Medicine_Id, [mname] as Medicine_Name, [cname] as Company_Name, [expdate] as Expire_Date, [quan] as Quantity, [unitp] as Unit_Price,[taxa] as Tax_Amount,[tot] as Total FROM med", con)
            con.Open()
            re.Fill(ds)
            con.Close()
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.Message(), MsgBoxStyle.Critical, "Error(s) Found")
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class