Form1_Load无引用且不执行问题求助
问题描述
- Form1_Load方法显示0个引用,运行时完全不执行
- 已在项目属性中设置启动对象为
GraceMail.Form1,手动修改项目文件的StartupObject行为此值后,无编译报错但方法仍不触发
相关代码
项目文件内容
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <StartupObject>Sub Main</StartupObject> <UseWindowsForms>true</UseWindowsForms> <MyType>WindowsForms</MyType> <TargetFrameworks>net6.0-windows</TargetFrameworks> <ApplicationIcon>gracemail.ico</ApplicationIcon> <Authors>******* *********</Authors> <Company>SG***</Company> <RunAnalyzersDuringBuild>True</RunAnalyzersDuringBuild> </PropertyGroup> </Project>
Form1代码片段
Public Class Form1 Private searchIndex As Integer = -1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim iniFile As New IniFile(Path.GetDirectoryName(Application.ExecutablePath) & "\GM.ini") Dim ThePath = Path.GetDirectoryName(Application.ExecutablePath) iniFile.WriteString("Settings", "path", ThePath) Dim TitleBartxt As String = iniFile.ReadString("Settings", "TitleBarTxt") Dim version = TitleBartxt.Substring(TitleBartxt.LastIndexOf(" ")) Me.Text = TitleBartxt Label3.Font = New Drawing.Font("Microsoft Sans Serif", 65.25, FontStyle.Bold Or FontStyle.Italic) Label3.Text = "Grace Mail" & version lblUpdate.Text = "Last database update: " & iniFile.ReadString("Settings", "dbUpdate") If Not File.Exists(ThePath & "\db\gmdb.csv") Then MsgBox("This is a new installation or the database file is missing." & vbCrLf & "In order to proceed using GraceMail you must:" & vbCrLf & "1. Download the database Excel file" & vbCrLf & "2. Select Database Operations, then choose 'Update Database'", vbInformation) Button1.Enabled = False Else Call loadTable() Call colorExceptions() End If End Sub End Class
问题截图

解决方案
- 修正项目启动对象配置:当前项目文件中
<StartupObject>值为Sub Main,这会优先执行自定义Main方法而非自动启动Form1。将其修改为GraceMail.Form1,确保和项目属性设置一致:<StartupObject>GraceMail.Form1</StartupObject> - 检查自定义Sub Main:如果项目中存在单独的
Module定义了Sub Main,即使设置启动对象为Form1,程序仍会优先执行该方法。此时要么删除这个Sub Main,要么在其中手动实例化并启动Form1:Sub Main() Application.Run(New Form1()) End Sub - 手动绑定Load事件:若上述操作后仍不生效,可在Form1构造函数中手动添加事件绑定,确保Load事件正确关联到Form1_Load方法:
Public Sub New() InitializeComponent() AddHandler Me.Load, AddressOf Form1_Load End Sub - 清理重建项目:缓存可能导致配置不生效,执行
Build -> Clean Solution清理后,再执行Build -> Rebuild Solution重新生成项目。
内容的提问来源于stack exchange,提问作者Jeffy
相关产品推荐
相关产品推荐

