Windows form doesn't show Form_Load in visual studio - c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
I am watching tutorial where Form1_Load() is included
I am watching tutorial where Form1_Load() is included, is there another way to include TextBox items by coding?

CSharp and VB.NET are a bit different on how to write event handlers. In your case, you need to tell the forms designer you want to handle the .Load event.
Find the properties window in the designer for the form, and click on the lightning bolt.
Now the properties window displays all the events that the form can trigger. Find Load and double-click on the space next to it.
The designer will now generate the Form1.Load() method you are missing
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
An alternative way to do this same thing.
In the form constructor type this.Load += and then press the [Tab] button.
Visual studio will fill in the code needed to handle the event wit the option to rename the event handler
A third way of handling the Load event
My preferred way is to override the OnLoad() method in the form.
Type override OnLoad inside the class, and IntelliSense is going to prompt you the method to override. Select OnLoad and press [Tab].
Now you can write the same code you would have before, but inside the OnLoad() method which gets called automatically every time the .Load event triggers.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Write code here
}
}
Yes in the 20+ years of WinForms programming and VisualStudio development some redundancies have developed. It is not a bad thing, as VS needs to accommodate different coding styles.
There is a 4th way, that applies to VB.NET and involves the Handles keyword which makes more logical sense IMHO than the way C# does things, but that is just me.

You can "wire up" events via code.
Here's an example with the Load() event of the Form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
This can be done with any control and event, but it's easier to do via the IDE as described by Jeroen van Langen. If you're creating dynamic controls (created at run-time) then this is the way to do it.

Go to your form designer and make sure the form control self is selected. press F4 for the properties control. Press on the lightning icon for events and search for the Load event.

Related

WinForms AutoSave To Database (vs. Save Button)

I'm writing a WinForms (C#) using Entity Framework. application
Currently, I have a big fancy Save Button on my form that works just as you'd hope and expect it to.
I have been asked to change the application to save automatically as the the user changes each field.
I know I can do this by adding code to the Value_Changed event in each of my controls. But is there an easier way? There are a large number of controls on this form. Some way to overload the OnChange event for multiple control types (Text, DropDown, CheckBox, RadioButton)?
In the form's constructor, loop through all of its child controls (this.Controls) recursively and add the (same) callback method to the ValueChanged event for each of them. That will associate it with each control on the form regardless of any changes to the form in the future.
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
foreach (Control c in this.Controls)
{
c.Validated += C_Validated;
}
}
private void C_Validated(object sender, EventArgs e)
{
Debug.Print($"{sender.GetType().FullName} had Validated event called");
}
}
}

RoutedEventArgs cannot be founded in the System.Windows

I have a program on the Windows Forms (VS13). In the code:
using System.Windows;
namespace WF
{
public partial class Form1 : Form
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//...
}
}
}
RoutedEventArgs cannot be found, but it exists in the namespace System.Windows. What can be reason? I have a reference to the WindowsBase.
RoutedEventArgs is a WPF-specific thing. In Winforms, nothing like that exists. You should remove your reference to WindowsBase ans stick to Winforms specific things.
For instance, the Loaded event doesn't exists. You must use the Load event from the Form object instead. The designer really helps here in adding all the bits of code you need and attaching the event handlers.
I did add Windows.UI.Xaml namespace and problem solved.

New to C#-would like to add WndProc

Everyone, I am just so new to C#, please help me...
I would like to add WndProc to process messages, I have look into properties but I don't see the thunderbolt to display function name so I can add one I like. I search the internet and see WndProc as
protected override void WndProc(ref Message msg)
{
//do something
}
I would like it to be generated for me, not to type it down ?
WndProc is not a .NET event handler; it's a window procedure, part of the native Win32 API. You won't get any code generation for it as an event handler in Visual Studio.
In Windows Forms all you have to do is override a form's existing WndProc() method and start coding. As it's found in the Form class, there is an autocomplete option for it if you type the following:
override WndProc
which then generates:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
}
just to make this perfectly clear: it's rather improbable that you ever have to do something with WndProc inside winforms/wpf/whatever in the .net world.
All this nasty stuff is abstracted and hidden away from you and I don't know a single case where I really needed/missed it.
In Winforms you just wire up events with
Eventname += EventHandlerMethod;
(or you can do such more advanced stuff with annonymous methods and lambdas but don't concern yourself to much with it at the moment).
The easiest way is to just use the Designer and hook your events there:
After subscribed to a event with this tool the editor will show you the handler it created and you can start coding away.
Here is a quick example:
I just started a new project and added a single button "button1" onto the form:
then I hook up the OnClick-Event of the button (select the button and goto the event-tab):
and finaly I added code to change the buttons text to "clicked" into the codebehind:
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StackOverflowHelp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// the following line is from InitializeComponent - here you can see how the eventhandler is hook
// this.button1.Click += new System.EventHandler(this.OnButton1Clicked);
}
private void OnButton1Clicked(object sender, EventArgs e)
{
var button = sender as Button; // <- same as button1
if (button == null) return; // <- should never happen, but who is to know?
button.Text = "clicked";
}
}
}
that's all. The nasty dispatching of events is done by the framework.

Can't fire MouseWheel event in C# Windows Forms

First off, the mousewheel event is not listed in Visual Studio 2008's events pane which is very annoying.
I found the correct format online though, and wrote this into my code:
private void Form1_MouseWheel(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
...from which I'm getting no response when the mousewheel is rotated.
I'm doing this in my code's main class area, and the designer contains only the one form/window/whatever so the mouse isn't losing focus.
namespace BlahBlah
{
public partial class Form1 : Form
{
And by contrast, I have this method right above the mousewheel one and it works fine:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
If I had to guess, I'm thinking I'm not correctly linking the code to the form (aka: all the stuff that visual studio would do for me if I added this event through the designer's events panel). But I could be wrong or just be making some silly error.
Can you help me get ANY kind of response when the mouse wheel is rotated? Thanks!
The mousewheel event needs to be set up.
Add this to Form1's constructor (After InitalizeComponents();)
this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);
I don't have enough reputation to respond with a comment, but the answer to your side question is that the delegates do need to be setup. However when you create one by double clicking it in the properties pane the code is automatically generated and placed in the *.designer.cs file in the InitializeComponent method.
It's best in this case to override the OnMouseWheel member function rather than register to the event. For example:
public class MyFrm : Form
{
protected override void OnMouseWheel(MouseEventArgs e)
{
/*Handle the mouse wheel here*/
base.OnMouseWheel(e);
}
}
I don't know how to subscribe to that particular event but you can override the appropriate method on the form, instead.

How can I disable hittests on a Windows Form?

Is it possible to disable the hittest on a Windows Forms window, and if so, how do I do it? I want to have a opaque window that cannot be clicked.
Thanks in advance,
Christoph
If you're talking to a different process, you need to send and retrieve Windows messages.
http://www.c-sharpcorner.com/UploadFile/thmok/SendingWindowsMessageinCSharp11262005042819AM/SendingWindowsMessageinCSharp.aspx
Have a look at this link:
Using Window Messages to Implement Global System Hooks in C#
http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
Global system hooks allow an application to intercept Windows messages intended for other applications. This has always been difficult (impossible, according to MSDN) to implement in C#. This article attempts to implement global system hooks by creating a DLL wrapper in C++ that posts messages to the hooking application's message queue.
Do you want a window that cannot be moved? Set FormBorderStyle to none.
Well, I still don't know much about your use case, but I'll take a stab anyway, and provide a simple example.
I assume that you want to control something on the main form from your floating form.
To do this, you need a reference to your main form from your floating form. You do this by creating a constructor overload in your floating form that accepts an instance of your main form, like this:
public FloatingForm(MainForm mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
The floating form contains a textbox named floatingFormTextBox, and a button named Button1. The partial class for the floating form looks like this:
public partial class FloatingForm : Form
{
MainForm _mainForm;
public FloatingForm()
{
InitializeComponent();
}
public FloatingForm(MainForm mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
private void button1_Click(object sender, EventArgs e)
{
_mainForm.DoSomething(floatingFormTextBox.Text);
}
}
The main form just contains a textbox named mainFormTextBox. When the main form loads, it creates an instance of the floating form, passing a reference to itself to the floating form's new constructor overload. The partial class for the main form looks like this:
public partial class MainForm : Form
{
FloatingForm _floatingForm;
public MainForm()
{
InitializeComponent();
}
public void DoSomething(string text)
{
mainFormTextBox.Text = text;
this.Refresh();
}
private void MainForm_Load(object sender, EventArgs e)
{
_floatingForm = new FloatingForm(this);
_floatingForm.Show();
}
}
Now, when I put some text into the textbox of the floating form and click the button, the text shows up in the textbox of the main form.

Categories

Resources