About ListView in Window Form - c#

I make a ListView control (with some column header) in my window form but when I run this code then it gives me error just like that
Line in my Form.cs file:
string packagename =
File.ReadAllText(Program.ProjectLocation + "\\" +
Program.ProjectName + ".aProj");
Error 'System.Windows.Forms.ColumnHeader'
does not contain a definition for
'ReadAllText' and no extension method
'ReadAllText' accepting a first
argument of type
'System.Windows.Forms.ColumnHeader'
could be found (are you missing a
using directive or an assembly
reference?)
So please help me remove this error.

I think that the problem could be this: did you call your listview or a column File?
If so, correct: System.IO.File.ReadAllText(...);

It seems that File refers to a variable of type ColumnHeader and not the File class in System.IO
What you need to do is right click the word file and choose "Go to Definition", most probably you will find something like ColumnHeader File = new ColumnHeader();

Either
Program.ProjectLocation OR
Program.ProjectName
is type of System.Windows.Forms.ColumnHeader, Add
replace
Program.ProjectLocation.ToString()
Program.ProjectName.ToString()
I don't know which one is System.Windows.Forms.ColumnHeader so make changes according to that
EDIT ANSWER:
Either
Program.ProjectLocation
OR
Program.ProjectName
is type of System.Windows.Forms.ColumnHeader,
Reeplace it with
Program.ProjectLocation.ToString()
Program.ProjectName.ToString()
Reason: ReadAllText("A valid file path as string"); But here you are trying to generate a file path by using column header so its throwing that error
I don't know which one is System.Windows.Forms.ColumnHeader so make changes according to that

Related

CS1061: 'Bookmarks' does not contain a definition for 'Item'

I am trying to program a app for chain letters.
This is my Template: https://learn.microsoft.com/en-us/previous-versions/office/troubleshoot/office-developer/automate-word-create-file-using-visual-c
This is my code: https://github.com/440z/2021-07-01_WindowsFormsAppFuerKettenBriefMitWord
The error occurs in file Form1.cs in line 171.
Word._Document oDoc;
// ...
object oBookMark = "MyBookmark";
oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here"; // L171
error CS1061: 'Bookmarks' does not contain a definition for 'Item' and no accessible extension method 'Item' accepting a first argument of type 'Bookmarks' could be found (are you missing a using directive or an assembly reference?)
I set a using directive and made a assembly reference how described in the template.
Add a reference to Microsoft Word Object Library. To do this, follow these steps:
On the Project menu, click Add Reference.
On the COM tab, locate Microsoft Word Object Library, and then click Select.
and
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
I just don't get it!!!
It looks like Bookmarks has an indexer, so: try one of
oDoc.Bookmark[ref oBookMark].Range.Text = "Some Text Here";
or
oDoc.Bookmark[oBookMark].Range.Text = "Some Text Here";
However, I would expect this to now complain that you're trying to access an invalid key, meaning: there is no existing bookmark keyed by "MyBookmark"

Best way to reuse C# code to pass system.servicemodel.security.usernamepasswordclientcredential credentials to different class files

I'm a beginner at writing and understanding C#. I would like to know how to reuse some code I have that updates a WebService username and password credentials.
Here is code in 1st cs File:
...
public class AuthenticateLogin
{
public static object PassCredentials()
{
ServiceName.UsersClient clientauthentication = new ServiceName.UsersClient();
clientauthentication.ClientCredentials.UserName.UserName = "user";
clientauthentication.ClientCredentials.UserName.Password = "pwd";
}
}
...
Here is code in 2nd cs File:
var test = AuthenticateLogin.PassCredentials();
Console.WriteLine(test.EchoAuthenticated("Successful Login"));
Error Received:
Error CS1061 'AuthenticateLogin' does not contain a definition for 'EchoAuthenticated' and no accessible extension method 'EchoAuthenticated' accepting a first argument of type 'AuthenticateLogin' could be found (are you missing a using directive or an assembly reference?)
Solution desired:
In the second file, I want to be able to use the methods in the 'clientauthentication' of the first file. I need to be able to use the 'clientauthentication' like a common object in multiple other cs files. FYI: I can use the 'clientauthentication' methods fine in the 1st file and it works okay.

Not Able To Initiate a Class in Main Coding

i trying to call a form as show dialog in in main coding
i have added below screen shot to help you guys under what i am asking. I have tried search on the internet but wasn't able to find a solution too.
but itemsDeleteScreen,itemsEditScreen and AddScreens works perfectly
image screenshot - http://i.stack.imgur.com/E66du.png
the items DeleteScreen Code : (Which Works)
itemsDeleteScreen deleteItem = new itemsDeleteScreen();
deleteItem.ShowDialog(this); // loads the Delete Items Screen
the StockInsScreen Code : (Whic Does not Work)
stockInsScreen stkIns = new stockInsScreen();
stockInsScreen.ShowDialog(this);
the error which i getting when i compile is
The type or namespace name 'stockInsScreen' could not be found (are you missing a using directive or an assembly reference?)
Navigate to StockInScreen Class and check the namespace, it would be like(yourproject.foldername) and add that name space in the class you want to use.(Source:From Image you have provided).For information about namespace,look at http://msdn.microsoft.com/en-us/library/z2kcy19k.aspx

C# Reflection: Instantiate an object with string class name

my situation is the next: I'm working with Visual C# 2010 express developing a Windows Forms Application. When the user logins, dinamically build a menustrip with options loaded from a database table. In that table i save id, option name and Form Name.
So, suppose that in my project i have a Form named Contabilidad, it has Contabilidad.cs that is the main class , so if i wanna create a new form and show it i do this:
Contabilidad frmConta = new Contabilidad();
frmConta.Show();
But in this case, because the menu options are stored in database, in database i only have the string "Contabilidad". So, i want to use C# reflection to create a instance of Contabilidad or any other form only with class name in string format.
First i tried this:
Form frmConta= (Form)Activator.CreateInstance(null, "Contabilidad").Unwrap();
Because i read in a StackOverflow question that if i use null i'm referring to current assembly (my forms are all in the same project), but i get this message:
Could not load type 'Contabilidad' from assembly 'AccountingSA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
The class definition is the next:
namespace AccountingSA {
public partial class Contabilidad : Form
{
public Contabilidad()
{
InitializeComponent();
} ...
Also i tried this:
Assembly assembly = Assembly.Load("AccountingSA");
Type t = assembly.GetType("Contabilidad");
Form frmConta = (Form)Activator.CreateInstance(t);
But i get ArgumentNullException with this message:
Value cannot be null. Parameter name: type
Because t variable is null.
What i'm do wrong? Thanks in advance.
Use the fully-qualified name of the type:
Type t = assembly.GetType("AccountingSA.Contabilidad");
From the documentation for Assembly.GetType(string):
name Type: System.String
The full name of the type.
[...] The name parameter includes the namespace but not the assembly.
You're trying to use the name of the class without specifying the namespace. This should be fine:
Assembly assembly = Assembly.Load("AccountingSA");
Type t = assembly.GetType("AccountingSA.Contabilidad");
Form frmConta = (Form)Activator.CreateInstance(t);
Every version of GetType requires the fully-qualified type name; the benefit of using Assembly.GetType is that at least you don't need to also include the assembly name, but as documented you still need the namespace:
The name parameter includes the namespace but not the assembly.
Note that to diagnose something similar in the future, it would have been worth looking at the value of t after the second line - it will be null, which is why the third line threw an exception.
You should add the namespace:
assembly.GetType("AccountingSA.Contabilidad");
Try this
Form frmConta= (Form)Activator.CreateInstance(null, "AccountingSA.Contabilidad").Unwrap();
Try specifying your class this way:
ContabilidadNamespace.Contabilidad, ContabilidadAssembly
Its too late in the thread to answer, but the earlier answer, in current .NET framework (4.7), not working (The line Assembly assembly = Assembly.Load("AccountingSA"); always throws FileIOException). Currently, working code is (Use Type directly)
Type t = Type.GetType("AccountingSA.Contabilidad");
Form frmConta = (Form)Activator.CreateInstance(t);
or other way using Assembly is
Assembly assembly = typeof(Form).Assembly;
Type t = assembly.GetType("AccountingSA.Contabilidad");
Form frmConta = (Form)Activator.CreateInstance(t);

How does one use OpenFileDialog in C# in visual Studio 2010

I have written a custom dialog (form) that I can use in a C# program that behaves much like a "File - Open" menu command and brings up a window where a user can select a file or directory.
The question I have is this. It has "My Computer" as its root. How can I have it so that it searches on a Network? If the file or directory is located on a network.
Or better yet, in Visual Studio 2010, is there some sort of canned FileOpenDialog that I can use right away?
I tried calling the OpenFileDialog as described in the example code at
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx
but the compiler does not seem to like DialogResult.OK as used in this line of code:
if(openFileDialog1.ShowDialog() == DialogResult.OK)
The compiler says:
Error 1 'System.Nullable' does not contain a definition for 'OK' and no extension method 'OK' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)
I tried using the namespace Microsoft.Win32 instead of System.Windows.Forms and neither worked. They both produced this error.
Looks like you are trying to use a WinForms (System.Windows.Forms) dialog.
Here is the MSDN page for WPF dialog boxes from the Microsoft.Win32 namespace.
An excerpt:
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
// Show open file dialog box
bool? result = dlg.ShowDialog();
EDIT: missed the WPF tag. My bad. What Henk Holterman said.
Have you added the namespace that the example tells you to: System.IO?
I might be wrong, but it sounds like you have created a variable called DialogResult which is of type System.Nullable

Categories

Resources