I'm building a series of controls that override Windows default behavior, but this is going to get very long eventually. I'm a little new to C#, but I understand the convention is to separate code out by function into their own classes for readability and such. If I wanted to move the code that fixes (by removing entirely) the F1-help for Windows, what would I do?
For now, I made a folder and then a new class. I auto generated then customized the constructor as follows:
namespace MyWay.Fixers
{
class F1Fixer
{
// Used to know the long-ass number soup of the user that's logged in. Necessary because when we run as admin, it would normally change THEIR settings
private string loggedInSIDStr;
public F1Fixer(string SIDStr)
{
loggedInSIDStr = SIDStr;
}
private void CheckF1()
{
using (var hku = RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Registry64))
{
using (var F1key = hku.OpenSubKey(loggedInSIDStr @"\SOFTWARE\Classes\TypeLib\{8cec5860-07a1-11d9-b15e-000d56bfe6ee}\1.0\0\win32"))
{
// EGADS! It's active!
if (F1key == null)
{
fckF1RestoreBtn.Enabled = false;
fckF1KillBtn.Enabled = true;
fckF1Status.Text = "That creepy bugger is waiting and watching.";
fckF1Status.BackColor = Color.FromArgb(255, 232, 0);
}
else
{
fckF1RestoreBtn.Enabled = true;
fckF1KillBtn.Enabled = false;
fckF1Status.Text = "The F1-Help function had been put in it's place.";
fckF1Status.BackColor = Color.FromArgb(0, 200, 50);
}
}
}
}
private void fckF1KillBtn_Click(object sender, EventArgs e)
{
using (var hku = RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Registry64))
{
RegistryKey setter = hku.CreateSubKey(loggedInSIDStr @"\SOFTWARE\Classes\Typelib\{8cec5860-07a1-11d9-b15e-000d56bfe6ee}\1.0\0\win32", RegistryKeyPermissionCheck.ReadWriteSubTree);
setter.SetValue("", "");
setter.Close();
setter = hku.CreateSubKey(loggedInSIDStr @"\SOFTWARE\Classes\Typelib\{8cec5860-07a1-11d9-b15e-000d56bfe6ee}\1.0\0\win64", RegistryKeyPermissionCheck.ReadWriteSubTree);
setter.SetValue("", "");
setter.Close();
}
CheckF1();
}
private void fckF1RestoreBtn_Click(object sender, EventArgs e)
{
Registry.Users.DeleteSubKeyTree(loggedInSIDStr @"\SOFTWARE\Classes\Typelib\{8cec5860-07a1-11d9-b15e-000d56bfe6ee}\1.0\0");
CheckF1();
}
}
}
The problem is that the class in the folder can't access my form elements. All the existing (and working code) when it's in program.cs suddenly goes redline without any suggested fix. I haven't found a good tutorial or video explaining how to code this properly - maybe it's considered too basic, but I've never coded in Java or a similarly class-heavy language. I usually just put my code in a few files separated by general function/idea (with no classes).
If I'm going to do this, I want to do it properly, but how do I do that and make it work?
CodePudding user response:
You would normally keep the GUI-specific code in the Form1.cs
and call your business logic in F1Fixer.cs
from it (usually by creating a Fixer object or calling a static method).
Otherwise, you can pass objects/functions from Form1.cs
to F1Fixer.cs
through the Constructor or function parameter, but that is not a great idea.