Question Access objects of FormX on FormY

Legjendat

New member
Joined
Nov 29, 2012
Messages
2
Programming Experience
Beginner
ok, this is driving me mad, I don't have any C# experience, and I'm trying to convert a project of mine from VB.NET to C#, I have converted online most of my code, and fixed any conversion problems except the ones where I access objects of a form on another form.
This is very simple to do in VB.NET, as I just add FormX before MyObject, and it is fixed, but in C# it seems to work differently.

My Code is below, and when I try to call the NotifyIcon1 that belongs to Form1 on the code on my Form3 I would get the error "An object reference is required for the non-static field, method, or property" for which I googled about an hour before I gave up since I couldn't understand how to make it work, even though there were plenty of articles on this.

the errors are the same I guess, and I got some more on form1, but if anyone here could fix me one of them I think I shouldn't have problems fixing the rest.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
using System.IO;

namespace Converted_App
{
    public partial class Form3
    {
          internal Form3()
              {
            InitializeComponent();
        }
        private void Form3_Load(object sender, System.EventArgs e)
        {
            string ProgramFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
            string Company = ProgramFiles + "\\Test Company";
            string Product = Company + "\\Test Product";
          
            StartupCheckBox.Checked = Properties.Settings.Default.AddToStartup;
            TrayIconBox.Checked = Properties.Settings.Default.ShowIconInTray;
            AlwaysOnTopCheckBox.Checked = Properties.Settings.Default.AlwaysOnTop;
        }

        private void StartupCheckBox_CheckedChanged(object sender, System.EventArgs e)
        {

  /*This code is just fine          
        if (StartupCheckBox.Checked == true) 
            {
                Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",              true).SetValue(Application.ProductName, Application.ExecutablePath);
                Properties.Settings.Default.AddToStartup = true;
                Properties.Settings.Default.Save();
            }
            else
            {
                Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true).DeleteValue(Application.ProductName);
                Properties.Settings.Default.AddToStartup = false;
                Properties.Settings.Default.Save();
            }
        } */

        private void TrayIconBox_CheckedChanged(object sender, System.EventArgs e)
        {
          if (TrayIconBox.Checked == true)
            {
                Properties.Settings.Default.ShowIconInTray = true;
                Properties.Settings.Default.Save();
                Form1.NotifyIcon1.Visible = true;  // THIS BRINGS UP THE ERROR
            }
            else
            {
                Properties.Settings.Default.ShowIconInTray = false;
                Properties.Settings.Default.Save();
                Form1.NotifyIcon1.Visible = false; // THIS BRINGS UP THE ERROR
            }
        }

        private void AlwaysOnTopCheckBox_CheckedChanged(object sender, System.EventArgs e)
        {
            if (AlwaysOnTopCheckBox.Checked == true)
            {
                Form1.TopMost = true; // THIS BRINGS UP THE ERROR
                Properties.Settings.Default.AlwaysOnTop = true;
                Properties.Settings.Default.Save();
            }
            else
            {
                Form1.TopMost = false; // THIS BRINGS UP THE ERROR
                Properties.Settings.Default.AlwaysOnTop = false;
                Properties.Settings.Default.Save();
            }



        }
    }
} //end of root namespace
 

Legjendat

New member
Joined
Nov 29, 2012
Messages
2
Programming Experience
Beginner
I saw that in Google, and I saw many tutorials, and I tried, but I couldn't fix it, which means I wasn't really understanding it. Something so simple in VB becomes such a hassle in C#.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,136
Location
Sydney, Australia
Programming Experience
10+
I saw that in Google, and I saw many tutorials, and I tried, but I couldn't fix it, which means I wasn't really understanding it. Something so simple in VB becomes such a hassle in C#.
It's actually not a hassle. It just means that you have to treat forms in the same way as other objects. Default instances make forms appear to have some special powers that other types don't, which is not the case.

That said, here's a class that allows you to work with forms in a very similar way to default instances in VB:

[2005] Generic Singleton Form (Maintaining Only One Instance of a Form)

That's a C# translation of a class I created in VB 2005.
 
Top Bottom