2021-07-21 VB窗体求范围质数(Excel复制工作簿)

vbnet 复制代码
Imports System.IO.Ports

'Imports System.IO
'Imports ADOX

Public Class Form1
    Dim 列表 As New DataGridView
    Dim wb1 As New TextBox
    Dim wb2 As New TextBox
    Dim aj As New Button
    Dim xs As New TextBox
    Dim 串行端口 = New SerialPort
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "VB解答专用窗体"

        
        
        wb1.Parent = Me
        wb1.Text = "3"
        wb2.Parent = Me
        wb2.Text = "47"
        xs.Parent = Me
        xs.Text = "质数:"
        aj.Parent = Me
        aj.Text = "查找"
        wb2.Location = New Point(wb1.Width + 7)
        aj.Location = New Point(wb1.Width + wb2.Width + 7 * 2)
        xs.Location = New Point(wb1.Width + wb2.Width + aj.Width + 7 * 3)
        xs.Width = Me.Width - (wb1.Width + wb2.Width + aj.Width + 7 * 3)
        xs.WordWrap = True
        xs.Height = Me.Height
        xs.Multiline = True

        AddHandler aj.Click, AddressOf 按键单击

    End Sub
    Private Sub 按键单击(sender As Object, e As EventArgs)
        '缘由https://ask.csdn.net/questions/7477534?spm=1005.2025.3001.5141
        xs.Text = "质数:"
        Dim a = Integer.Parse(wb1.Text.Trim())
        Dim b = Integer.Parse(wb2.Text.Trim())
        Dim c As Integer = 3
        While a <= b And a > 1
            If a = 2 Or a = 3 Then
                xs.AppendText(a.ToString("0 "))
                a = IIf(a = 2, a + 1, a + 2)
            ElseIf c <= a / c Then
                If (a Mod c = 0) Then
                    a = a + 2
                    c = 3
                Else
                    c = c + 2
                End If
            Else
                xs.AppendText(a.ToString("0 "))
                a = a + 2
                c = 3
            End If
        End While
    End Sub
    
    Private Sub Excel复制工作簿()
        Dim myExcelApp As New Microsoft.Office.Interop.Excel.Application

        myExcelApp.Workbooks.Open(System.Environment.CurrentDirectory + "\\测试用例.xlsx", Type.Missing)
        myExcelApp.Workbooks.Open(System.Environment.CurrentDirectory + "\\复制用例.xlsx", Type.Missing)

        Dim worksheet1 As Microsoft.Office.Interop.Excel.Worksheet = CType(myExcelApp.Workbooks(1).Worksheets(1),  _
                                                                            Microsoft.Office.Interop.Excel.Worksheet)
        Dim worksheet2 As Microsoft.Office.Interop.Excel.Worksheet = CType(myExcelApp.Workbooks(2).Worksheets(1),  _
                                                                            Microsoft.Office.Interop.Excel.Worksheet)
        worksheet1.Copy(After:=worksheet2)

        myExcelApp.Workbooks(2).Save()
        myExcelApp.Quit()
        myExcelApp = Nothing

    End Sub

End Class

缘由VB求素数问题,提示:须使用双分支选择结构。_软件工程-CSDN问答

vbnet 复制代码
    Private Sub 按键单击(sender As Object, e As EventArgs)
        '缘由https://ask.csdn.net/questions/7477534?spm=1005.2025.3001.5141
        xs.Text = "质数:"
        Dim a = Integer.Parse(wb1.Text.Trim())
        If a < 30 Then
            MessageBox.Show("数据无效可重新输入!", "友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            wb1.Focus()
        End If
        Dim b = Integer.Parse(wb2.Text.Trim())
        Dim c As Integer = 3, h As Integer = 0, q = 2
        While q <= a And q > 1
            If q = 2 Or q = 3 Then
                xs.AppendText(q.ToString("0 "))
                h = h + q
                q = IIf(q = 2, q + 1, q + 2)
            ElseIf c <= q / c Then
                If (q Mod c = 0) Then
                    q = q + 2
                    c = 3
                Else
                    c = c + 2
                End If
            Else
                xs.AppendText(q.ToString("0 "))
                h = h + q
                q = q + 2
                c = 3
            End If
        End While
        wb2.Text = h
    End Sub
相关推荐
_dindong18 小时前
动规:回文串问题
笔记·学习·算法·leetcode·动态规划·力扣
Zwb29979218 小时前
Day 30 - 错误、异常与 JSON 数据 - Python学习笔记
笔记·python·学习·json
wangwangmoon_light19 小时前
0.0 编码基础模板
java·数据结构·算法
共享家952719 小时前
Leetcode刷题
算法·leetcode·职场和发展
simon_skywalker19 小时前
第7章 n步时序差分 n步时序差分预测
人工智能·算法·强化学习
山,离天三尺三19 小时前
基于LINUX平台使用C语言实现MQTT协议连接华为云平台(IOT)(网络编程)
linux·c语言·开发语言·网络·物联网·算法·华为云
不太可爱的叶某人19 小时前
【学习笔记】kafka权威指南——第7章 构建数据管道(7-10章只做了解)
笔记·学习·kafka
flashlight_hi20 小时前
LeetCode 分类刷题:74. 搜索二维矩阵
python·算法·leetcode·矩阵
小年糕是糕手20 小时前
【数据结构】算法复杂度
c语言·开发语言·数据结构·学习·算法·leetcode·排序算法
程序员-King.20 小时前
day86——有效的字母异位词(LeetCode-242)
算法·字符串