VB.NET - 将 WinForm 应用程序导出/转换为 Web ASP.NET 应用程序的最简单方法
背景:我有一个用 VB.NET 编写的 winform 应用程序,它使用 WebService 根据用户选择进行不同采访的营销公司向用户发送不同的邀请。 winform 应用程序从各种文本框、列表框和下拉列表中提取字符串值以创建一些 XML 并将其推送到名为 AcompServiceClient 的 Web 服务
问题:
- 是否有向导或第 3 方应用程序可以将 winform 数据导出到 webform asp.net或者我应该从头开始构建一个 aspx 页面,并为所有控件使用与 winform 应用程序相同的命名空间?
- 除了 AcompServiceClient Web 服务和代码隐藏 vb 之外,我还需要传输或设置哪些文件才能完成这项工作? (查看项目文件的屏幕截图)
- 我是否必须复制 app.config 文件的任何部分并将其适应 web.config 文件?
我在想:
- 我可以首先将 Acomp_Invitation_Form.vb 复制到 AComp_Invitation_Web_App.aspx.vb 代码隐藏页面。
- 从网络服务器上添加现有的网络服务
- 使用相同的名称/id 在前端 aspx 页面上手动重新添加格式、文本框、列表框和下拉列表
以下是 WinForm 应用程序的屏幕截图:
这是项目文件的屏幕截图:
这是我在 Acomp_Invitation_Form.vb 上的代码:
Imports TestClient.aCompService
Imports System.Text
Public Class Form1
Private proxy As New AcompServiceClient
Private Sub stuff()
Dim splitContractingBundle() As String
splitContractingBundle = Split(cb2.SelectedItem, "|")
Dim splitMarketingCompany() As String
splitMarketingCompany = Split(cb3.SelectedItem, "|")
Dim strDate As String = System.DateTime.Now.ToString
Dim strOpData As String = String.Format("{0}~{1}~{2}~{3}~{4}~{5}~{6}~{7}~{8}~{9}~{10}",
Trim(splitMarketingCompany(0)), txtFirstName.Text, "", txtLastName.Text,
txtEmail.Text, txtEmail.Text, "1", strDate,
"Pending", "1/1/1900", Trim(splitContractingBundle(0)))
Dim int1 As Boolean = proxy.AddContractOpportunity(strOpData, "test", "test")
txtEmail.Text = ""
txtFirstName.Text = ""
txtLastName.Text = ""
lbCarriers.Items.Clear()
cb2.Items.Clear()
cb3.Items.Clear()
cb2.SelectedItem = ""
cb3.SelectedText = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'TODO Add code to validate that all selections that are reaquired are met.
'ccemail and the additional message are not required
Dim firstname As String = txtFirstName.Text
Dim lastname As String = txtLastName.Text
Dim ccEmail As String = txtccEmail.Text
Dim sb As New StringBuilder
sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
sb.AppendLine("<root>")
sb.AppendLine("<MarketingCompany>")
sb.AppendLine("<MarketingCompanyName>")
''Get Marketing Company Short Name
Dim splitMC As String() = Split(cb3.SelectedItem, "|")
Dim MCShort As String = Trim(splitMC(0))
sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", MCShort))
'sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", My.Settings.MarketingCompanyShortName))
sb.AppendLine(String.Format("<ccEmail>{0}</ccEmail>", txtccEmail.Text))
sb.AppendLine(String.Format("<emailMessage>{0}</emailMessage>", txtMessage.Text))
sb.AppendLine(String.Format("<MarketerName>{0}</MarketerName>", txtMarketerName.Text))
sb.AppendLine("<agent>")
sb.AppendLine(String.Format("<FirstName>{0}</FirstName>", txtFirstName.Text))
sb.AppendLine(String.Format("<LastName>{0}</LastName>", txtLastName.Text))
sb.AppendLine(String.Format("<Email>{0}</Email>", txtEmail.Text))
sb.AppendLine("<CRMGuid>123456</CRMGuid>")
Dim spltBundles() As String
For Each item In cb2.SelectedItems
If Trim(item) <> "" Then
spltBundles = Split(item, "|")
sb.AppendLine("<ContractingOpportunity>")
sb.AppendLine(String.Format("<Carrier>{0}</Carrier>", Trim(spltBundles(0))))
sb.AppendLine(String.Format("<ContractingOpportunityName>{0}</ContractingOpportunityName>", Trim(spltBundles(1))))
sb.AppendLine("</ContractingOpportunity>")
End If
Next
sb.AppendLine("</agent>")
sb.AppendLine("</MarketingCompanyName>")
sb.AppendLine(" </MarketingCompany>")
sb.AppendLine(" </root>")
Dim xmlStr = sb.ToString
Dim int1 As Boolean = proxy.AddContractOpportunity(xmlStr.ToString, "test", "test")
MsgBox("Made It")
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetCarriers()
GetMarketingCompanies()
End Sub
Private Sub GetCarriers()
Try
Dim ac1 As Array
ac1 = proxy.GetCarrierNames("test", "test")
For Each item In ac1
lbCarriers.Items.Add(String.Format("{0} | {1} | {2}", item.CarrierID, item.CarrierNameLong, item.CarrierNameShort))
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub GetMarketingCompanies()
Try
Dim ac1 As Array
ac1 = proxy.GetMarketingCompanyNames("test", "test")
For Each item In ac1
cb3.Items.Add(String.Format("{0} | {1}", item.MarketingCompanyShort, item.MarketingCompanyName))
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub lbCarriers_LostFocus(sender As Object, e As System.EventArgs) Handles lbCarriers.LostFocus
Dim splt() As String
Dim ac1 As Array
cb2.Items.Clear()
For Each item In lbCarriers.SelectedItems
splt = Split(item, "|")
ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
For Each Pitem In ac1
cb2.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
Next
Next
End Sub
End Class
Background: I have a winform application written in VB.NET that uses a WebService to send out different invitations to users based on the marketing company they select to take different interviews. The winform app is pulling string values from a variety of textboxes, listboxes, and dropdownlists to create some XML and push it to a web service called AcompServiceClient
Questions:
- Is there a wizard or 3rd party application that will export winform data to webform asp.net or should I build an aspx page from scratch w/ the same namespaces for all the controls as the winform app?
- Which files do I need to transport or setup to make this work besides the AcompServiceClient web service and the code-behind vb? (look at screenshot of the Project Files)
- Do i have to copy over any parts of the app.config file and adapt it to the web.config file?
I was thinking:
- I can start by copying the Acomp_Invitation_Form.vb to the AComp_Invitation_Web_App.aspx.vb code behind page.
- Add existing webservice off the webserver
- Manually re-add formatting, text boxes, list boxes, and drop down lists on the front end aspx page using the same names / id's
Here's a screenshot of the WinForm App:
Here's a screenshot of the Project Files:
Here's my code on Acomp_Invitation_Form.vb:
Imports TestClient.aCompService
Imports System.Text
Public Class Form1
Private proxy As New AcompServiceClient
Private Sub stuff()
Dim splitContractingBundle() As String
splitContractingBundle = Split(cb2.SelectedItem, "|")
Dim splitMarketingCompany() As String
splitMarketingCompany = Split(cb3.SelectedItem, "|")
Dim strDate As String = System.DateTime.Now.ToString
Dim strOpData As String = String.Format("{0}~{1}~{2}~{3}~{4}~{5}~{6}~{7}~{8}~{9}~{10}",
Trim(splitMarketingCompany(0)), txtFirstName.Text, "", txtLastName.Text,
txtEmail.Text, txtEmail.Text, "1", strDate,
"Pending", "1/1/1900", Trim(splitContractingBundle(0)))
Dim int1 As Boolean = proxy.AddContractOpportunity(strOpData, "test", "test")
txtEmail.Text = ""
txtFirstName.Text = ""
txtLastName.Text = ""
lbCarriers.Items.Clear()
cb2.Items.Clear()
cb3.Items.Clear()
cb2.SelectedItem = ""
cb3.SelectedText = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'TODO Add code to validate that all selections that are reaquired are met.
'ccemail and the additional message are not required
Dim firstname As String = txtFirstName.Text
Dim lastname As String = txtLastName.Text
Dim ccEmail As String = txtccEmail.Text
Dim sb As New StringBuilder
sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
sb.AppendLine("<root>")
sb.AppendLine("<MarketingCompany>")
sb.AppendLine("<MarketingCompanyName>")
''Get Marketing Company Short Name
Dim splitMC As String() = Split(cb3.SelectedItem, "|")
Dim MCShort As String = Trim(splitMC(0))
sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", MCShort))
'sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", My.Settings.MarketingCompanyShortName))
sb.AppendLine(String.Format("<ccEmail>{0}</ccEmail>", txtccEmail.Text))
sb.AppendLine(String.Format("<emailMessage>{0}</emailMessage>", txtMessage.Text))
sb.AppendLine(String.Format("<MarketerName>{0}</MarketerName>", txtMarketerName.Text))
sb.AppendLine("<agent>")
sb.AppendLine(String.Format("<FirstName>{0}</FirstName>", txtFirstName.Text))
sb.AppendLine(String.Format("<LastName>{0}</LastName>", txtLastName.Text))
sb.AppendLine(String.Format("<Email>{0}</Email>", txtEmail.Text))
sb.AppendLine("<CRMGuid>123456</CRMGuid>")
Dim spltBundles() As String
For Each item In cb2.SelectedItems
If Trim(item) <> "" Then
spltBundles = Split(item, "|")
sb.AppendLine("<ContractingOpportunity>")
sb.AppendLine(String.Format("<Carrier>{0}</Carrier>", Trim(spltBundles(0))))
sb.AppendLine(String.Format("<ContractingOpportunityName>{0}</ContractingOpportunityName>", Trim(spltBundles(1))))
sb.AppendLine("</ContractingOpportunity>")
End If
Next
sb.AppendLine("</agent>")
sb.AppendLine("</MarketingCompanyName>")
sb.AppendLine(" </MarketingCompany>")
sb.AppendLine(" </root>")
Dim xmlStr = sb.ToString
Dim int1 As Boolean = proxy.AddContractOpportunity(xmlStr.ToString, "test", "test")
MsgBox("Made It")
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetCarriers()
GetMarketingCompanies()
End Sub
Private Sub GetCarriers()
Try
Dim ac1 As Array
ac1 = proxy.GetCarrierNames("test", "test")
For Each item In ac1
lbCarriers.Items.Add(String.Format("{0} | {1} | {2}", item.CarrierID, item.CarrierNameLong, item.CarrierNameShort))
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub GetMarketingCompanies()
Try
Dim ac1 As Array
ac1 = proxy.GetMarketingCompanyNames("test", "test")
For Each item In ac1
cb3.Items.Add(String.Format("{0} | {1}", item.MarketingCompanyShort, item.MarketingCompanyName))
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub lbCarriers_LostFocus(sender As Object, e As System.EventArgs) Handles lbCarriers.LostFocus
Dim splt() As String
Dim ac1 As Array
cb2.Items.Clear()
For Each item In lbCarriers.SelectedItems
splt = Split(item, "|")
ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
For Each Pitem In ac1
cb2.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
Next
Next
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要非常小心简单的方法。虽然 ASP.NET Web 窗体可能看起来类似于 Windows 窗体(连接到事件的控件),但底层机制却非常不同。如果您还没有这样做,我建议您阅读 HTTP 的工作原理以及 ASP.NET 页面的生命周期。
Be very careful of the easy way. While ASP.NET Web Forms might look similar to Windows Forms (controls hooked up to events), the underlying mechanism is very very different. If you have not done so already I recommend you read up on how HTTP works and the life cycle of an ASP.NET page.
是的,你想要的方式就是我已经做过很多次的方式。
只需复制代码隐藏中的方法并将它们粘贴到 ASP.NET 页面的代码隐藏中即可。您的某些方法不兼容,因为它们在 asp.net 中不受支持,但是当您构建项目时您会发现我们的速度非常快。
使用与 winform 中的控件名称完全相同的控件创建网页。当你构建时,你所要做的就是修复你的错误,然后你就可以上路了。
看起来您已连接到某些服务,因此您当然需要引用该服务。
Yes, the way you want to do it is the way I have done this many times.
Just copy the methods from your code behind and paste them into the code behind of your asp.net page. Some of your methods are not compatible because they are not supported in asp.net but you will find that our real quick when you build the project.
Create your web page with the controls having exactly the same name as the ones in the winform. When you build, all you have to do is fix your errors and you are on your way.
It looks like you are hooked up to some service so of course you will need to reference that.
是的,这就是一般想法。我会特别注意与在无状态 Web 环境中使用
AcompServiceClient
相关的任何问题。在不了解它是什么、如何工作或如何使用的情况下,很难说你是否必须重新考虑如何使用它。看起来您没有做任何其他依赖于在有状态环境中运行的事情。您只需从各种文本框中提取字符串值来创建一些 XML 并将其推送到服务。所有这些都应该顺利移植。您可能想添加一些客户端验证规则,但除此之外它看起来很简单。
您需要更改填充 DropDownList 的方式。 win 和 web 表单之间的工作方式略有不同。它想要绑定到 Web 表单中的数据源。
Yeah, that's the general idea. I'd pay special attention to any concerns related to using
AcompServiceClient
in a stateless web environment. It's hard to say whether you have to rethink how you're using that or not without knowing anything about what it is, how it works or how it's consumed.It doesn't look like you're doing anything else that relies on running in a stateful environment. You're just pulling string values from a variety of textboxes to create some XML and push it to a service. All of that should port over smoothly. You might want to look at adding some client side validation rules, but other than that it looks straight forward.
You'll want to change how you're populating your DropDownList. Those work a little different between win and web forms. It wants to be bound to a datasource in webforms.