I created a label named dbgOut
I simply try to set the text of the label but I get System.NullReferenceException.
using System;
using System.Drawing;
using System.Windows.Forms;
using Helper;
using System.Text;
using System.Net.Http;
using Newtonsoft.Json;
namespace test
{
public partial class test: Form
{
public test()
{
dbgOut.Text = "test";
...
Can someone tell me what I did wrong?
What you're doing wrong is trying to mess with a UI component before initializing it. A call to InitializeComponent should be made in the first line of the constructor of the forms so that your UI controls get intiailized and then you can play with them.
More infos: https://www.dotnetperls.com/initializecomponent
Related
I exported some code from Selenium IDE into my C# page in Visual studio 2013 and it keeps giving me this error. I just copied the steps from the exported document into my c# page. The platform finds the LinkText on the actual page but it keeps giving me this error.
I changed it to find the element by id but, then I don't know how to select the menu item in webpage.
I will really appreciate your help with this.
Thank you
I get this error:
{"Unable to locate element: {\"method\":\"link text\",\"selector\":\"My Resume\"}"}**
This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace MyFirstAutoScript
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://yelitzascareerjourney.wordpress.com/");
driver.FindElement(By.LinkText("My Resume")).Click(); //my page brakes here
more commands go here...
}
}
}
You must have to mouseover first on menu item "About Me" before
clicking on sub menu item "My Resume". And you can use Actions commands from Selenium for same.
I don't have much idea about C# but your code would be something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
namespace MyFirstAutoScript
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://yelitzascareerjourney.wordpress.com/");
new Actions(driver).MoveToElement(driver.FindElement(By.LinkText("About Me"))).Click().Build().Perform();
driver.FindElement(By.LinkText("My Resume")).Click(); //my page brakes here
more commands go here...
}
}
}
I had used new Actions(driver).MoveToElement(driver.FindElement(By.LinkText("About Me"))).Click().Build().Perform(); to mouse over on menu item.
Very basic knowledge on c# and is the first i use anything p/invoke related.
Help me pls, i have made this code but it doesnt seem to work.
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 System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("C:\\Users\\lchris\\Desktop\\SevenZipLib_9.13.2\\SevenZipLib\\SevenZipLib\\7z86.dll")]
public static extern void SevenZipArchive(string c);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SevenZipArchive archive = new SevenZipArchive("file.rar"))
{
foreach (ArchiveEntry entry in archive)
{
Console.WriteLine(entry.FileName);
}
}
}
}
}
It tells me that SevenZipArchive is a 'Method' and is being used like a 'type'.
I have include the library to my project already, i just dont know how to use it.
Here is the library:
https://sevenziplib.codeplex.com/
You first need to remove this code:
[DllImport("...")]
public static extern void SevenZipArchive(string c);
You do not need to provide any p/invoke declarations. The library wraps that up for you.
This is a .net assembly. You use it just as you use any other. Take the following steps:
Download the project. You already did that I think.
Build the solution. Make sure that
In your project, add a reference to the assembly that you built in the previous stage. It's SevenZipLib\bin\Debug\SevenZipLib.dll or SevenZipLib\bin\Release\SevenZipLib.dll depending on the target you selected.
Add using SevenZipLib; to your project code to to gain access to the namespace.
Once you've done that your code will work. You can use the tests project that is supplied as part of the download as a rich source of example code.
Once again i need your help.. Anywho.. I've managed to get this code below to "work". the part there doesn't work is "this". I've no idea of what to use in static and how it works. I'm rather new to this aswell, so i might need some explenation for dummies.
Anyway. This code below is suppossed to be my "mainform", where everything is loaded like icons, size, settings, menu and whatever not.
Currently i'm trying to add a global "settings". which can be loaded from all the forms. So each invidual forms would be this.ClientSize = new System.Drawing.Size(1440, 900); and anything else i may add would have same impact on the form. like icons, opacity, whatever not.
Overally it's just a place to store settings there can be accessed from any other forms.
What i've written below here, is what i've managed so far. The SettingsOnProgramStart is recognised in my Settings form, but it does not change the Clientsize or the icon. It's pobably because of the "this" as it shows red lines.
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 lala.events
{
public partial class TrayMenu : Form
{
public TrayMenu()
{
InitializeComponent();
SettingsOnProgramStart();
}
}
public static void SettingsOnProgramStart()
{
//
// Load global settings.
//
this.Icon = new Icon("images/skin/global/icon.ico");
this.ClientSize = new System.Drawing.Size(1440, 900);
}
}
}
Settings file :
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 lala.events
{
public partial class Settings : Form
{
public Settings()
{
InitializeComponent();
TrayMenu.SettingsOnProgramStart();
}
}
}
Thank you for your time and sorry for any problems this may have caused :/
I'd love if anyone could give me a useful link to a "configuration for dummies", where i'd learn about using cfg, ini for my project. so i can get the invidual settings loaded from a file.
You need to pass the form whose settings need to be set in:
public static void SettingsOnProgramStart(Form formToSet)
{
//
// Load global settings.
//
formToSet.Icon = new Icon("images/skin/global/icon.ico");
formToSet.ClientSize = new System.Drawing.Size(1440, 900);
}
Used as:
//When called from another form
TrayForm.SettingsOnProgramStart(this);
As to settings, see MSDN application settings: http://msdn.microsoft.com/en-us/library/k4s6c3a0(v=vs.110).aspx
As an aside; this code doesn't really make sense as part of the TrayForm class, as its not related to that object at all. It should likely be a member of a GlobalSettingsManager class, or something similar.
I decided to try working with C# over c++ but ran into a problem.
The following code snippet shows the problem.
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 Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Device;
namespace Project_AREA
{
public partial class Form1 : Form
{
private Device device;
public Form1()
{
InitializeComponent();
}
}
}
That's as much code as I have right now.
In private Device device;, the keyword Device is not getting recognized although all the libraries have been referenced and the using directives are included.
Any ideas?
Managed to Solve the problem.. for your reference and also if other run in to the same problem. You must add all the following references to the project: (note all files can be found on your had disk at c:\Windows\Microsoft.NET\DirectX for Managed Code) 1: Microsoft.DirectX.dll from folder 1.0.2902.0 2: Microsoft.DirectX.Direct3D.dll from folder 1.0.2902.0 3: MicrosoftMicrosoft.DirectX.Direct3DX.dll from folder 1.0.2911.0 Thanks for the help tho guys :)
I have the following code but still have to type full path to use the System.IO.Ports namespace even though I have the using clause in place. Am I missing something in my reference list?
The = new SerialPort returns a Error 5 'SerialPort' is a 'namespace' but is used like a 'type'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace SerialPort
{
public partial class Form1 : Form
{
System.IO.Ports.SerialPort counter = new SerialPort("COM5");
public Form1()
{
InitializeComponent();
}
Thanks
You're declaring a namespace of SerialPort. Don't do that. That's what's causing the problem.
All you've got to do is change the namespace, and you'll be fine. You could use an alias as per Honza's request, but I think the code would be clearer to everyone if you just renamed the namespace.
It's because your namespace carries the same name. Either rename your namespace or use an alias for the serial port, like this:
using SP = System.IO.Ports.SerialPort
And then you can use
SP counter = new SP("COM5");
But as Jon suggested, renaming your namespace is a clearer solution to anyone who'll read your code.