24 August 2012

UserControl in C#.net

Now, I create a user control for view PDF,JPEG,BMP and Tiff file.6) User could use the Menus of Document when user views the Document in the Easycon .but user can't view the PCL File .User could do zoom In and Zoom Out a Image file to Press Mouse Right Click Button and Mouse Left Click Button.
Controls  in the User Control:
Controls                  No
Panel                       2
Menu strip               1
Picture Box             1
axAcroPDF              1
Step by Step to Create a User Control :
Step:1
  Create a New Project using class file and Right click your project   in the Solution explore.
  Create a User Control (name :ViewFiles).
Step:2
Create a controls One by One.
  1)Panel
           Name of the Panel: pnlview (it is a Parent of the Usercontrol)
The following Controls are the Child of the Pnlview.
  2)Menustrip
           Name of the Menustrip: mnuviewfiles
   In that Menustrip , create a 2 toolstripMenuItem
       1)  View  : Name of the toolstripMenuItem: ttpview
        2) Close : Name of the toolstripMenuItem: ttpclose
  In this two Menus are the Parent Menus
Now, we create a Child of the Parent.
       create a 3 toolstripMenuItem of ttpview
1) ttpFit
2) ttpZoomIn
3) ttpZoomOut
 Panel
           Name of the Panel: pnlImageViewer
PictureBox:
   Name of the PictureBox: pictviewfile
axAcroPDF:
   Name of the axAcroPDF: axAcroPDF
         Now , we create a Controls in the User Control.   
3) Step:3
  Right Click your user Controls and Click the view code
Now Coding Side,
Declaration
public  string m_FileExt;
      public  string m_FileName;
      public int norWidth;
      public int norHeight;
private void ttpclose_Click(object sender, EventArgs e)
      {
          this.Visible = false;
         

      }

      private void ViewFiles_Load(object sender, EventArgs e)
      {
          try
          {
              File_view(m_FileExt, m_FileName);
             
          }
          catch (Exception ex)
          {
              WriteLog(ex.Message, ex.StackTrace);
          }
      }
             public void  File_view (string FileExtention,string FileName)
      {
          try
          {
              if (FileExtention == "pdf" || FileExtention == "PDF")
              {
                  pictviewfile.SendToBack();
                  pictviewfile.Visible = false;
                  this.Visible = true;
                  pnlview.Dock = DockStyle.Fill;
                  ttpview.Visible = false;
                  axAcroPDF.Visible = true;
                 axAcroPDF.BringToFront();
                  axAcroPDF.LoadFile(FileName);
                  axAcroPDF.Show();
              }
              else if (FileExtention == "JPEG" || FileExtention == "jpeg" || FileExtention == "TIFF" || FileExtention == "tiff" || FileExtention == "bmp" || FileExtention == "BMP")
              {
                  axAcroPDF.Visible = false;
                  axAcroPDF.SendToBack();
                  this.Visible = true;
                  pnlview.Visible = true;
                  pnlview.Dock = DockStyle.Fill;
                  pictviewfile.Visible = true;
                  pictviewfile.BringToFront();
                  pnlview.Refresh();
                  pictviewfile.Load(FileName);
                  pictviewfile.Height = pnlview.Height - 28;
                  pictviewfile.Width = pnlview.Width;
              }
             else if (FileExtention == "txt" || FileExtention == "TXT")
              {
                  this.Visible = false;
                  System.Diagnostics.Process.Start(FileName);
              }
          }
          catch (Exception ex)
          {
              WriteLog(ex.Message,ex.StackTrace);
          }
         
         
      }
      public string VFIles_Ext
      {
          get { return m_FileExt; }
          set { m_FileExt = value; }
      }
      public string VFile_name
      {
          get { return m_FileName; }
          set { m_FileName = value; }
      }

      private void picexit_Click(object sender, EventArgs e)
      {
          this.Visible = false;
      }
      public static void WriteLog(string message, string stkTrace)
      {
          #region step by Step Processing
          try
          {
              #region Declaration and Log File Name
              string logFile = String.Empty;
              System.IO.StreamWriter logWriter;
              logFile = "View_Test.txt";
              #endregion Declaration and Log File Name
              #region Check Log File exists or not
              if (System.IO.File.Exists(logFile))
              {
                  logWriter = System.IO.File.AppendText(logFile);
              }
              else
              {
                  logWriter = System.IO.File.CreateText(logFile);
              }
              #endregion Check Log File exists or not
              #region Add a Message and StackTrace into logFile and close the Logfile
              logWriter.WriteLine("Message    : " + message);
              logWriter.WriteLine("Stack Trace: " + stkTrace);
              logWriter.WriteLine("Date & Time: " + DateTime.Now.ToString());
              logWriter.WriteLine("------------------------------------------------------------------------------------------");
              logWriter.Close();
              #endregion Add a Message and StackTrace into logFile and close the Logfile
          }
          catch (Exception e)
          {
          }
          #endregion step by Step Processing

      }

      private void toolStripMenuItem4_Click(object sender, EventArgs e)
      {
          this.Visible = false;
      }

      private void ttpfit_Click(object sender, EventArgs e)
      {
          Zoom("Normal");
      }

      private void ttpzoomIn_Click(object sender, EventArgs e)
      {
          Zoom("ZoomIn");
      }

      private void ttpzoomOut_Click(object sender, EventArgs e)
      {
          Zoom("ZoomOut");
      }
      void Zoom(string Mode)
      {
          try
          {
              int picWidth = pictviewfile.Width;
              int picHeight = pictviewfile.Height;
              if (Mode == "Normal")
              {
                  pictviewfile.Width = norWidth;
                  pictviewfile.Height = norHeight;
                  pnlImageViewer.AutoScroll = false;
              }
              else if (Mode == "ZoomIn")
              {
                  pictviewfile.Width = picWidth + ((norWidth / 100) * 20);
                  pictviewfile.Height = picHeight + ((norHeight / 100) * 20);
                  pnlImageViewer.AutoScroll = true;
              }
              else
              {
                  if (picWidth > norWidth && picHeight > norHeight)
                  {
                      pictviewfile.Width = picWidth - ((norWidth / 100) * 20);
                      pictviewfile.Height = picHeight - ((norHeight / 100) * 20);
                      if (pictviewfile.Height == norHeight)
                      { pnlImageViewer.AutoScroll = false; }
                      else
                      { pnlImageViewer.AutoScroll = true; }
                  }
              }
          }
          catch (Exception ex)
          {
              WriteLog(ex.Message, ex.StackTrace);
          }
      }

      private void pnlview_Resize(object sender, EventArgs e)
      {
          pictviewfile.Width = pnlview.Width;
          pictviewfile.Height = pnlview.Height;
          norWidth = pictviewfile.Width;
          norHeight = pictviewfile.Height;
          axAcroPDF.Width = pnlview.Width;
          axAcroPDF.Height = pnlview.Height - 25;
      }

      private void ViewFiles_KeyDown(object sender, KeyEventArgs e)
      {
  

      }

      private void pictviewfile_MouseClick(object sender, MouseEventArgs e)
      {
          if (e.Button == MouseButtons.Right)
          {
              Zoom("ZoomIn");
          }
          else if (e.Button == MouseButtons.Left)
          {
              Zoom("ZoomOut");
          }
         
      }

      private void pictviewfile_Click(object sender, EventArgs e)
      {
     
      }

      private void pictviewfile_MouseEnter(object sender, EventArgs e)
      {
          Zoom("Fit");
      }
Step: 4

 Build and Run .Go to Project bin folder see the Dll File

Step:5

  Create a another project and refer that dll file in your new Project Toolbar and use them.

No comments: