Determining labels from another partial class/windows form - c#

I'm bit new at this so excuse me if my question is a bit novice. I'm designing a Windows form application to basically replace an old excel spreadsheet then email system(not that its much of a system). After the forms are filled out the answers are saved in terms of public variables, which is fine since it's a small program. Im having trouble referencing my variable from another windows form. Basically, I would like to have the filled out form close and a new "review" window pop up. I'm just using labels that will be show what the value of the variable is. If it was in the same class it wouldn't be a problem but im using two different forms that are partial classes of the same namespace. A bit of code:
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OPS_PictureLoader
{
public partial class Points_Screen : Form
{
public int DevJobStandardsTotal = 0;
And the label I would like to show a "0"(or whatever the program has added to it)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OPS_PictureLoader
{
public partial class Review : Form
{
public Review()
{
InitializeComponent();
label14.Text = DevJobStandardsTotal;
Again, thanks and feel free to tell me Im totally wrong :D

You need to pass the instance of the first form to the second form's constructor:
public Review(Points_Screen owner)
{
InitializeComponent();
label14.Text = owner.DevJobStandardsTotal;

Related

Syntax Error 'Helper' does not exist in the current context

I am building a form where i can drag and drop button controls.
I followed the tutorial from this link but the code generates an error difficult for me to debug.
Syntax Error
Error code CS1030, "The name 'Helper' does not exist in the current
context
I have tried adding and removing namespaces, using System and using System.Drawing but the same error code is still present.
In a much more extensive code like the actual example, Helper.ControlMover does work, and it is very easy to use. What exactly am i missing in the code below?
Thank you for your time and help.
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 MovingControls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Helper.ControlMover.Init(this.button1);
}
}
}

How do I get a Controls.ItemsControl in an Form

I have a Controls.ItemsControl (actually a HelixViewport3D) and would like to add it inside a Forms.UserControl.
I already tried this.Controls.Add(new HelixViewport3D());
Where this is a derivative from UserControl
however this resulted in an error HelixToolkit.Wpf.HelixViewport3D' to 'System.Windows.Forms.Control. Which makes sense given that they don't inherit anything from one another. Is there a sort of wrapper class in these situations?
using HelixToolkit.Wpf;
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;
public partial class HelixForm : Form
{
public HelixForm()
{
InitializeComponent();
var _3Dcntr = new HelixViewport3D();
this.Controls.Add(_3Dcntr);//doesn't work
//what we worked out so far
var elemHost = new ElementHost();
}
}
I have also noticed that it's supposed to be possible to host a WPF in a winform using ElementHost.Child but adding this variable/function cannot be found in my example code(missing something?), System.Windows.Forms.Integration.ElementHost tells me that System.Windows.Forms.Integration is missing.
Hosting WPF Composite Control in Winform
Mixing WPF in a Winforms app is not recommended but Microsoft provides a ElementHost in WindowsFormsIntegration namespace.
Here is a more indepth demo of them working
Mixing WPF and Winforms
(sorry for the simple answer I have not mixed them before.)

Form showing nothing. using CefSharp with Windows form in C#

I am trying to show a webpage in a form using C#. I am using CefSharp to show the webpage (as I would like to test & learn how it works). But since I have worked only on inbuilt webbrowser, I have no idea how to get started with CefSharp(Finding it difficult to get any tutorials). I tried to write this code which executes but the form shows nothing in it. Where am I going wrong ?
Here is my Code :
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;
using CefSharp;
using CefSharp.WinForms;
namespace chrometest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Cef.Initialize(new CefSettings());
var test = new CefSharp.WinForms.ChromiumWebBrowser("http://www.google.com")
{
Dock = DockStyle.Fill,
};
this.Controls.Add(test);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Edit :
I tried to run the example from https://github.com/cefsharp/CefSharp/tree/cefsharp/41/CefSharp.WinForms.Example
When I try to build the solution, it shows a dialog box saying "Restoring Nuget Package cef.redist.x64 3.2454.1317" with a progresss bar which takes hell lot of time to complete but never completes and after sometime it hangs.
Please help what should I do to get the example running.
The CefSharp project has a few different examples as part of the main project.
Basic Example using Nuget
https://github.com/cefsharp/CefSharp.MinimalExample
More Advanced Examples
https://github.com/cefsharp/CefSharp/tree/cefsharp/41/CefSharp.WinForms.Example
When you installed the project using Nuget it should have opened a Readme.txt file, it contains a lot of useful information.
https://github.com/cefsharp/CefSharp/blob/cefsharp/41/NuGet/Readme.txt
In the context of WinForms there's a few tutorials
http://www.codeguru.com/columns/dotnet/if-you-like-it-put-an-html5-ui-on-it.html
http://thechriskent.com/2014/08/18/embedded-chromium-in-winforms/
For those reading this looking for WPF, there's
http://www.codeproject.com/Articles/881315/Display-HTML-in-WPF-and-CefSharp-Tutorial-Part
http://www.codeproject.com/Articles/887148/Display-HTML-in-WPF-and-CefSharp-Tutorial-Part
More Links
https://github.com/cefsharp/CefSharp.Tutorial
http://thechriskent.com/category/net/cefsharp/

How do i get the exe file name of the second added project? EDITED

In Form1 i did this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace test
{
public partial class Form1 : Form
{
WindowsFormsApplication1.Form1 f1;
public Form1()
{
InitializeComponent();
MessageBox.Show("Oops something went wrong sorry");
f1 = new WindowsFormsApplication1.Form1();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Where f1 is the second project i just added.
Now i added the seocnd project as a reference.
In the second project i did:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string[] hardDrivedInfo;
string applicationFileName;
public Form1()
{
InitializeComponent();
applicationFileName = Path.GetDirectoryName(Application.ExecutablePath);
But the applicatioFileName show me the path of the exe file of the first project while i need to get the directory + file name of the second project wich is in directory: D:\C-Sharp\test\test\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe
The directory of the first project is: D:\C-Sharp\test\test\test\bin\Debug\test.exe
But i need to make that applicationFileName will show: D:\C-Sharp\test\test\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe
EDIT **
What i want to do is to run the first main project then after the messagebox is popup and i close it it will run the second project will copy the second project exe file to another location like D: and will run the exe file of the second project. So if i delete the first project exe file the second one on D: will keep running.
You could try using
string file = typeof(Form1).Assembly.Location;
See Assembly.Location for more information:
The location of the loaded file that contains the manifest. If the loaded file was shadow-copied, the location is that of the file after being shadow-copied. If the assembly is loaded from a byte array, such as when using the Load(Byte[]) method overload, the value returned is an empty string ("").
Your text is a bit unclear, but I'm guessing you're trying to get the name of the referenced assembly, rather than the one that started the process, right?
Try using Assembly.GetExecutingAssembly(). Then you can get the full path from the Location property.

C# Blocking IP by modifying Windows 7 Firewall

I'm starting to build an anti-ddos application in C#, it's going to block IP's by checking how many connections there are, in an amount of time, but I can't find whats needed for NetFwMgrType.
Here's code, I just started:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NETCONLib;
using NATUPNPLib;
using NetFwTypeLib;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
bool Firewallenabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
public Form1()
{
InitializeComponent();
}
}
}
The errors I get is:
Error 1 A field initializer cannot reference the non-static field, method, or property 'WindowsFormsApplication1.Form1.NetFwMgrType'
Error 2 A field initializer cannot reference the non-static field, method, or property 'WindowsFormsApplication1.Form1.mgr'
(I added references: \Windows\System32\hnetcfg.dll and \Windows\System32\FireWallAPI.dll)
Thanks for answer.
Oh and if you know how to block IPs with Firewall modifying, as well, it would save me a lot of googling:)

Categories

Resources