Unable to access methods in another winform - c#

I have two projects in my solution called ePad demo and PLHardwareHelper. I have added a reference to the PLHardwareHelper for the epad demo and I can successfully access methods in epad from hardwarehelper.
The problem is though that I cannot access anything in hardware helper from epad. I can't added a reference because it says it will result in a circular reference. I can't even add the namespace as a reference in the form.
How can access methods in hardwarehelper from within epad without adding a reference?
I have added a third reference called ApplicationJoin so I can add it's reference to both the other projects but it still complains that it can't find it. I have added an image below to demonstrate:
The code from ApplicationJoin:
namespace ApplicationJoin
{
public partial class Form1 : Form
{
public static string _imagevalues = "";
public Form1()
{
InitializeComponent();
}
public static void ImageValues(string value)
{
_imagevalues = value;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
The issue:
The error on the blue squiggly line is:
The name 'ApplicationJoin' does not exist in the current context
I have tried rebuilding and cleaning the solution but it still doesn't work.

Related

Populate list in form from a table (Entity Framework) then use in other places in the form code

New C# and Entity Framework user. I want to put the results of a table into a C# list. I want to reference this list in multiple form events. I will cycle through the list from beginning to end throughout the life of the form.
Currently, I have this code (snippet):
public partial class FrmMain : Form
{
private readonly admEntities _admEntities = new admEntities();
public FrmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
var exhibitor = _admEntities.Exhibitors.ToList();
// Put the first value in a text box - this works
txtCurrentSaleOrder.Text = exhibitor.First().SaleOrder.ToString();
}
}
I was hoping to use the exhibitor variable elsewhere on other events. However, I am unable to reference it.
I have not been successful in my searching and coding attempts to resolve this problem so far. Any pointer in the right direction would be appreciated.
One thing I tried was to put this code in a class above FrmMain(). That also failed. Probably due to not fully understanding classes yet.
You should make exhibitor a field, which is a fancy way of saying declare it outside of a method, inside of your class. Here is an example where exhibitor can be accessed from anywhere else in the class.
public partial class FrmMain : Form
{
private readonly admEntities _admEntities = new admEntities();
//It's unclear what exhibitor is in your question, so I'm using generic code that will allow it to work regardless.
private Type exhibitorType = typeof(admEntities.Exhibitors);
private List<exhibitorType> exhibitor = new List<exhibitorType>();
public FrmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
//Can reference here
exhibitor = _admEntities.Exhibitors.ToList();
txtCurrentSaleOrder.Text = exhibitor.First().SaleOrder.ToString();
}
private void AnotherMethod()
{
//Can reference exhibitor here too.
}
}

Visual Studio does not support code parsing or generation because it is not contained within a project that supports code

I am building a Windows Form Application, I have a Form1.cs file and Form1.Designer.cs file. After I added a private method inside a Form1 class in Form1.cs it was working, but after I closed VS code and opened it again it was saying
"does not support code parsing or generation because it is not contained within a project that supports code". Call Stack
I have deleted the private method I've added in Form1 class and re-opened Project again, but it says the same thing
namespace Project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void MyFunc()
{
//some code here
}
private void Button1_Click(object sender, EventArgs e)
{
MyFunc();
}
}
}
I expected Button1_Click event handler to call my own method

Passing Variable from another class into a form

I am struggling with passing a Variable (a string) in C# for a special problem:
Overview:
I am writing a plugin for a purchased program at my company. The program (or better: the programs support) gives the user basic C#-Code which basically just opens a form, and connects the program with whatever I write down in the forms code.
As it is a Visual-Studio-Solution I get some files: "MyUserInterface.cs" and "MyUserInterface.Designer.cs".
"MyUserInterface.Designer.cs" defines the look of my form, i thing the most importand parts for my problem are:
partial class MyUserInterface
{
[...]
private void InitializeComponent()
{
[...]
this.f_status = new System.Windows.Forms.Label();
this.SuspendLayout();
[...]
//
// status
//
this.f_status.Name = "status";
this.f_status.Text = "WELCOME TO MYPLUGIN v2";
[...]
this.Controls.Add(this.f_status);
this.ResumeLayout(false);
this.PerformLayout();
}
[...]
private System.Windows.Forms.Label f_status;
[...]
}
The most important code from "MyUserInterface.cs" is:
partial class MyUserInterface
{
[...]
public MyUserInterface()
{
InitializeComponent();
}
[...]
private void click_compute(object sender, EventArgs e)
{
//Basically everythings runs here!
//The code is opend in other classes and other files
}
}
Now as i marked in the code section, my whole code runs in the "click-compute" Function and is "outsourced" into other classes.
One important part of my code is found in "statushandler.cs":
class statushandler
{
[...]
public static void status_msg(string c_msg)
{
[...]
f_status.Text = c_msg; // And here is my problem!!
[...]
}
}
Problem:
In my special case, i try to change the text of the "f_status"-Lable while running my code by using the "status_msg" Function!
While I pass variables between classes a few times in my code. A cannot figure out, why this explicit one cant be found inside "statushandler". (It is no problem as long as I stay inside the original "click_compute", without going into a different class).
What I already tried:
1.) I tried to change basically everything in "MyUserInterface" into "public",
2.) Also I tried to call f_status in status_msg like MyUserInterface.f_status.Text,
3.) Write a Getter/Setter-Function in "MyUserInterface.(Designer.)cs" (both), which was catastrophic because i couldn't define the Label in the InitializeComponent anymore.
4.)
a.)Read a lot of Stackoverflow-Threads about passing variables between classes, which all didn't helped, all solutions I found, are working between classes, but not in this special case.
b.)Watched a lot of youTube tutorials, same result.
c.)Read some stackoverflow-Threds about passing variables between different Forms, but they all had in common, that the "displaying-form" was opend AFTER the variable was known. In my special case the form is opened all the time, and can't be closed, nor reopened...
And now I am out of ideas!
I wouldn't be surprised, if I do not see some details, but I can't found them... I would be very happy, when somebody could help me!
My question:
How can I change the text of my lable from another class?
Your method is static while your form has instance. So your static method does not know anything about your form. You can add MyUserInterface parameter to static method
public static void status_msg(MyUserInterface form, string c_msg)
{
[...]
form.f_status.Text = c_msg; // And here is my problem!!
[...]
}
If you have single instance form (only one instance is created at a time) you can have static property with it's reference:
partial class MyUserInterface
{
public static MyUserInterface Instance { get; private set; }
[...]
public MyUserInterface()
{
InitializeComponent();
Instance = this;
}
}
With this solution you can use your old method:
class statushandler
{
[...]
public static void status_msg(string c_msg)
{
[...]
MyUserInterface.Instance.f_status.Text = c_msg; // You have instance of yout form here
[...]
}
}
Of course you should protect against null/ Disposed form etc.
Create a public property on the specific class in your 1st Form that gets the label's value like this:
public string Name {get {return Label1.Text}; set {Label1.Text = value}; }
Then in your 2nd Form:
public Form2(Form1 form)
{
string name;
name = form.Name;
}

Static class object reference

I have one main form and a static class when i access static class member it gives me nullreference error . Previously it was working fine donot know what happend . Any one can suggest what is wrong.
code snap:
namespace MyNamespace
{
public partial class myForm : Form
{
public myForm()
{
InitializeComponent();
}
private void myForm_Load(object sender, EventArgs e)
{
My_Static_Data_Class.player_name="Demo Player"
}
}
public static class My_Static_Data_Class
{
public static string player_name = "";
}
}
please help?
You're either accessing the static class member before it was set to "Demo Player". For example, you are trying accessing My_Static_Data_Class.player_name in Program.cs code before it calls the main form from the Main[] method. Alternatively you're might be setting My_Static_Data_Class.player_name to null elsewhere in code and then accessing it.
Check all the references in code editor and follow it up. To do this, right click on My_Static_Data_Class.player_name in Visual Studio editor, and choose Find All References menu item.

Visual studio cannot detect newly created class for asp.net "website"

I am trying to modify an existing .net "website" project by adding a new class to it. I added my class inside the App_Code folder (let's call this class "ClassA"), and tried to access it from the outside in an aspx.cs file. For some reason, right after I created the class, I can create an instance of it in my aspx.cs file without any warnings from Visual Studio (e.g., ClassA a = new ClassA()). But every time I rebuild the project, I get the following Error from visual studio (The type or namespace name 'ClassA' could not be found (are you missing a using directive or an assembly reference?)). What am I missing here?
Code for Class A -> App_Code/ClassA.cs
public class ClassA
{
public string test;
}
Code for Test.aspx.cs
namespace A{
public class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClassA a = new ClassA(); // line with error
}
}
}
If I put everything in Test.aspx.cs I get no errors:
namespace A{
public class ClassA
{
public string test;
}
public class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClassA a = new ClassA(); // no error!
}
}
}
By default the classes inside App_Code folder has property "Build Action" set as "Content".
If you want to use it you need to set its "Build Action" property to "Compile".
This should work.
I am really sorry, for I could not totally understand the problem.
In my opinion you should not use namespaces when you create a class in app_code and let it be accessible to the whole website project.
In this instance you have added a namespace, try adding:
using b;
where b is your namespace you used in your new class. This should solve your problem.

Categories