Load event not firing - c#

I am rebuilding a project that I originally coded in SharpDevelop in Visual Studio. The relevant code worked in SharpDevelop.
The following steps should illustrate the problem:
Create a new project
Add a reference to System.Data.SQLLite (I am using the DLL for .NET 4.5)
In the main form, add the using statement:
using System.Data.SQLite;
In the form's Load event, attach the event handler
private void PriceType2_Load(object sender, EventArgs e)
{
MessageBox.Show("Load event hNDLER");
SQLiteConnection con;
//con = new SQLiteConnection();
}
That works, but if you uncomment the next line, it doesn't.
private void PriceType2_Load(object sender, EventArgs e)
{
//SetDisableColors();
//ToggleEnableOff();
MessageBox.Show("Load event");
SQLiteConnection con;
con = new SQLiteConnection();
}
The form opens, but the message box is not displayed. As I said, it works fine in SharpDevelop. And in case you're wondering, I am migrating the project because I need a grown up Report Viewer.
Any ideas?

I just changed the reference to the assembly for .NET 4.0 (although my dev machine and all my others have 4.5) and it works. I remembered reading somewhere that there was an issue with the 4.5 DLL, but I don't know what it was.
Thanks for all the pointers.

Related

.Net 6.0 Windows App Cant Reference System

So the weirdness has hit. I created a new Visual Studio 2022 - fully patched, updated, and current.net 6.0 Windows Desktop app. It has 1 form that has a menu bar dragged from the toolbox on it and that is it. I have done nothing else to it! System, Form, void, object, EventArgs, and Application are all red and not available (see below)
using System;
namespace Developmeny_Test_Application
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
}
private void e7xitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
This is as basic as it gets and can I get Visual Studio to let it in?
Under Dependencies/COM it is showing an orange triangle (which I always thought meant depreciated but as this was created with the wizard templates that's not likely)
So my question is what am I missing? All the web searching I have done has revealed nothing of any use.
Any help is gratefully appreciated.
Added as an edit for more information
The section that has the triangle has this section in the project file :
<ItemGroup>
<COMReference Include="{bee4bfec-6683-3e67-9167-3c0cbc68f40a}">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>4</VersionMinor>
<VersionMajor>2</VersionMajor>
<Guid>bee4bfec-6683-3e67-9167-3c0cbc68f40a</Guid>
</COMReference>
</ItemGroup>
That COM-Reference is fishy. You should not need that. When I do the same (create a new Winforms project and add a "MenuStrip" to the form) I'm not getting that reference.
I'm assuming you accidentally added a component from a third-party library to your form, which caused this strange reference. Just delete that section from the project file and see what happens. If the error persists, please quote the exact error message you get.

Microsoft Visual Linking Forms/Projects

So I have one Solution with four Projects in it. In Project_Owner_Add I want a Next button so that when it is clicked one of the other Projects is displayed.
This is my code.
private void buttonNext_Click(object sender, EventArgs e)
{
Project_Owner_Add.Form1 next = Project_Owner_Add_Product_Owner.Form1();
next.Show();
}
The error message is as follows:
Project_Owner_Add_Product_Owner does not exist in the current context
I'm assuming I'm going about calling information from a separate Project all wrong but I had sort of thought it was a matter of simply linking the forms together (this also doesn't work).
Any suggestions on how to get around this?
Add reference to your other project as already suggested, also you can use using directive to set some alias for your Form1 class in case when it exists in both projects.
The keyword new is essential when it comes to instantiating classes in C#. You can do something like this :
Add reference to other project if needed - > then :
using MyForm = Project_Owner_Add.Form1;
private void buttonNext_Click(object sender, EventArgs e)
{
MyForm next = new MyForm();
next.Show();
}
You need to add a reference to the second project. Do the following steps:
Right click Project_Owner_Add and select Add Reference.
Then Solution --> Projects and select second project from the list.
Then try this:
secondprojectNamespace.Form1 next = new secondprojectNamespace.Form1();
next.Show();

NullReferenceException during .exe run, but not during debug

During the construction of my Window LoginSystem, a NullReferenceException is being thrown only when running the application through the .exe.
While debugging, everything works perfectly fine however.
Code which calls the LoginSystem window:
LoginSystem ls = new LoginSystem();
ls.Show();
Where I've found the problem to be in my LoginSystem class:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Login.con = new SqlConnection(ConfigurationManager.ConnectionStrings["thuisDB"].ConnectionString);
...
}
Just in case you're wondering:
public class Login
{
public static SqlConnection con = null;
...
}
Link to stack trace:
HERE
PS: This line (Login.con = new SqlConnection(....) is the first time Login.con gets called, as the only code using that static var are in a class which LoginSystem is supposed to make.
EDIT: This question is NOT about me asking what a NullRef is or how to fix it, it was merely a single event where I didn't know why it was being thrown & didn't know how to debug it.
ConnectionStrings["thuisDB"] will be null if you are running the .exe in a folder where it can't find the configuration file.
Look for something like MyProgram.exe.config and make sure that is in the same folder as your executable file.

Debugging very slow after adding persistent settings

I have a semi-small visual C# solution I am working on in Visual Studio 2010. I recently added in Persistent settings using the settings.Settings file and Properties.Settings.Default. On the Form Load event, I am checking the value using Properties.Settings.Default and am assigning them to a checkbox.Checked variable.
//Load Form1
private void Form1_Load(object sender, System.EventArgs e)
{
cbxShowPass.Checked = Properties.Settings.Default.showFullPassword;
checkBox2.Checked = Properties.Settings.Default.showBookmarkFiles;
}
When I start my program in Debug mode, it is extremely slow to start, however, when I remove that line of code, it will start quickly.
What can I do to make my program start up quickly in Debug mode without removing the Settings?
Here are my CheckBox_Changed events, where I assign the value to the Settings.
private void ShowPass_CheckChanged(object sender, EventArgs e)
{
Properties.Settings.Default.showFullPassword = !Properties.Settings.Default.showFullPassword;
Properties.Settings.Default.Save();
DisplayPassword();
}

MessagePrompt crashing at first launch of application

I have a problem that I am using coding4fun dll in my WP7 application for showing the popup messages.
I am using:
Micrsoft.Phone.Controls.Toolkit
Coding4fun.Phone.Controls
At first launch of deployment on device its crashing saying that value cannot be null(parameter name element) while on emulator its running fine. I have tried the latest version of this dll but the result was same.
While adding Micrsoft.Phone.Controls.Toolkit of latest version 1.4.8 is giving warning that adding a silverlight library may result in unexpected consequences.
while I tried other version of this dll still no success.
I am getting exception in stacktrace
Clarity.Phone.Extensions.DialogService.InitializePopUp
Clarity.Phone.Extensions.DilaogService.Show
Basically i am using that popup inside constuctor of mainpage.xaml(first page) after InitializeComponent() and it is throwing null reference type at first launch while deploying but app is getting installed. again if i run application on device then it is appearing correctly.
My code is:
notificationPrompt = new MessagePrompt();
notificationPrompt.Title = "Notification"
notificationPrompt.Body = "";
notificationPrompt.ActionPopUpButtons.Clear();
Button btnDisclaimer = new Button() { Content = "Yes" };
btnDisclaimerContinue.Click += new RoutedEventHandler(btnNotificationPromptYes_Click);
Button btnDisclaimerCancel = new Button() { Content = "No" };
btnDisclaimerCancel.Click += new RoutedEventHandler(btnNotificationPromptNo_Click);
notificationPrompt.ActionPopUpButtons.Add(btnDisclaimerContinue);
notificationPrompt.ActionPopUpButtons.Add(btnDisclaimerCancel);
notificationPrompt.Show();
I think it's the better to move all this code outside the constructor, and put it inside the Loaded event (occurs when a FrameworkElement has been constructed and added to the object tree: http://msdn.microsoft.com/en-us/library/ms596558(vs.95)) of the PhoneApplicationPage class, or just override the OnNavigatedTo method:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// What you want here...
...
}
Often when you have exceptions in the constructor of a PhoneApplicationPage, they will not show, making the debug more difficult and annoying...

Categories