How to use windows forms in the webapplication.


generally we cant use some windowsforms functionalities in the webapplications like previously in this DNS we had a large discussion for how to select multiple files to upload the images to server for this i gave the alternate solutions for this but no direct way for selecting multiple files as we do in winforms application. Now i found a way to use that functionality by making the activex object.

How to create ActiveX control for using windows forms in web application.



points



  • Open VisualStudio and Select ClassLibrary Project.

  • Delete the .cs file and add a new user control to the project and design as you
    want.( here in my example iam designing as i required to select multiple files as
    we dont have an option to select multiple files for uploading using file upload
    so here we are preparing our own control for selecting multiple files)

  • Now code the functionality as required.

  • Now add the using System.Runtime.InteropServices; to the namespaces of cs
    file.

  • Now add this to cs file before the class or immediate after namespace or betweeen
    the namespace and class definition.[ProgId("multiUpload.ActiveControl")]

    • here multiUpload is the name of the namespace of the project working with.

    • here ActiveControl is the class name nothingbut the name of the usercontrol
      i added.



  • also add this [ClassInterface(ClassInterfaceType.AutoDual)] after the above
    statement.

  • also add this [Guid("415D09B9-3C9F-43F4-BB5C-C056263EF270")] this is the
    id for identification so just for generating this follow below steps.

    • goto the Tools menu in the project and then select Create GUID which
      opens a window and show you different statements with radio buttons.

    • now select the second last one you can see the sample id below and then select new
      id button on the right side and select copy button and exit the window.

    • now copy the id in the cs file as above.



  • Now one last one important one is make these visible to the COM so write this statement
    [ComVisible(true)]

  • you use this statement for the methods or functions which you are creating in the
    cs files, if you want to expose those methods to javascript.

  • Now goto the properties of the project and signin the assemebly for this follow
    below steps

    • right click on the project and select propwerties

    • goto signing option there select sign in the assmebly checkbox and then select
      the new in the dropdown.

    • now enter some name in the filename textbox and then if you want to protect it with
      pwd you can give password for it here iam not doing it, then press ok which creates
      the sn key file for project.


  • now build you project and open the debug folder of your project.

  • open the cmd of the visualstudio and then there type command like regasm /codebase
    d:\\testactive.dll


    • here regasm is exe for registering the assembly to interact with that.

    • /code base is for creatign the cab file contains code.

    • d:\\testactive.dll is the path of the dll here you can just drag and drop the dll
      file from debug folder to the cmd after typing regasm /codebase which automatically
      adds the path by continuing with /codebase

    • press enter you can see the successfull message




  • how to consume the activex created above


  • Now open other instance for visualstudio and create a web application or website.

  • Now inorder to use the activex just write the below statement.
     < object
    id="DemoActiveX" classid="clsid:415D09B9-3C9F-43F4-BB5C-C056263EF270" codebase="multiUpload.cab"
    height="200" width="200">
    </object >

  • now here give id of own and name attribute also after that classid= here you give
    the GUID given in the classlibrary project.

  • and code base is like your namespace of the class library followed with .cab and
    give properties height and width.

  • now you can able to see the usercontrol you created and its functionality.



here is my sample code for creating ActiveX means classlibrary project code



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace multiUpload
{
[ProgId("multiUpload.ActiveControl")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("415D09B9-3C9F-43F4-BB5C-C056263EF270")]
[ComVisible(true)]
public partial class ActiveControl : UserControl
{
string values=string.Empty;
public ActiveControl()
{
InitializeComponent();
}
[ComVisible(true)]
public string getfiles()
{
return values.TrimEnd(',');
}
[ComVisible(true)]
public void button1_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.ShowDialog();
if (openFileDialog1.FileNames!= null)
{
foreach (string names in openFileDialog1.FileNames)
{
listBox1.Items.Add(names);
values += names+",";
}
}
else { MessageBox.Show("no items selected"); } }
catch
(Exception ex) { MessageBox.Show(ex.Message); } } } }


my aspx code in the
webapplication for invoking the activex


<div >
<table >
<tr >
<td >
<object id="Object1" classid="clsid:415D09B9-3C9F-43F4-BB5C-C056263EF270" codebase="multiUpload.cab"
height="200" width="200" >
</object >
</td >
</tr >
<tr >
<td >
<input type="button" value="click" onclick="getitems();" / >
</td >
</tr >
</table >
</div >

my script code

<script type="text/javascript">
function getitems() {
var items = document.DemoActiveX;
if (items) {
alert(items.getfiles());
}
else
alert("no msg");
}
</script>


Attachments

  • Test ActiveX webapplication (44240-223555-Test-ActiveX-webapplication.rar)
  • ActiveX Project (44240-223627-ActiveX-Project.rar)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: