Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, June 13, 2020

[C#][Resolved] Could not load file or assembly 'Interop.Microsoft.Office.Core'

Error message:
'test.exe' (CoreCLR: DefaultDomain): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.5\System.Private.CoreLib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'test.exe' (CoreCLR: clrhost): Loaded 'C:\Users\user\source\repos\test\bin\Debug\netcoreapp3.1\test.dll'. Symbols loaded.
'test.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.5\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'test.exe' (CoreCLR: clrhost): Loaded 'C:\Users\user\source\repos\test\bin\Debug\netcoreapp3.1\Interop.Microsoft.Office.Interop.Excel.dll'. Module was built without symbols.
'test.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.5\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module.
Could not load file or assembly 'Interop.Microsoft.Office.Core, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Solution :
Look up "Dependencies" then "COM " in your project at solution explorer, 
found the reference "Interop.Microsoft.Office.Interop.Excel" you added to your project, 
click it and change the value of  "Copy Local" and "Embed Interop Types" As Yes



Reference:
https://stackoverflow.com/questions/28349576/could-not-load-file-or-assembly-interop-microsoft-office-core-version-2-4-0-0

Thursday, April 16, 2020

[C#][Example] Dynamic create LinkLabel and related event handler

place in constructor
Dictionary<String,String> pdfs = new Dictionary<String, String>(){
  {"Apple","http://www.example.com/apple.pdf"},
  {"Banana","http://www.example.com/banana.pdf"},
  {"Orange","http://www.example.com/orange.pdf"},
};
LinkLabel[] labels = new LinkLabel[pdfs.Length];
for(KeyValuePair<string, string> pair in pdfs){
  label[i] = new Label();
  label[i].Name = "label"+i;
  label[i].Location = new Point(27,i*30);
  label[i].TabStop = true;
  label[i].Text = pair.Key;
  label[i].Links[0].LinkData = pair.Value;
  this.Controls.Add(labels[i]);
}
And the Event Handler
private void linkedLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){
  System.Diagnostics.Process.Start(e.Link.LinkData as string);
}
Referecne:
https://www.dotnetperls.com/dictionary

Wednesday, April 15, 2020

[C#][Resolved] Readonly TextBox with ScrollBar

I want a readonly textarea show a long list of news.
But there is no textarea but multiline textbox.
So what should do is create a new TextBox with multiline:
TextBox textBox = new TextBox();
textBox.Multiline = true;
textBox.WordWrap = true;
textBox.ReadOnly = true;
textBox.Text = "your long text";

However, the overflowed content are hidden, let set a vertical scrollbar for user to view the hidden part, insert this line after your insert statment:
textBox.ScrollBars = ScrollBars.Vertical;

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.textbox.multiline?view=netframework-4.8

Tuesday, April 14, 2020

[C#][Resolved] c# textbox get text from resource

Firstly you need create an resource in "Resources.resx" file,
such as add Name as "title" and Value as "A Big Big Title" in your Resource.resx

Name      Value              Comment
========================================
title     A Big Big Title    

And than get it by "Properties.Resources.title". An example:

Label labelTitle = new Label();
labelTitle.Name = "labelTitle";
labelTitle.Size = new System.Drawing.Size(130,20);
labelTitle.TabIndex = 1;
labelTitle.Text = Properties.Resources.title;

And then you will found string "A Big Big Title" was shown in your design panel or at the result of your application.

Reference:
https://stackoverflow.com/questions/1508570/read-string-from-resx-file-in-c-sharp

Monday, March 23, 2020

[C#] Derived and Base Class, sealed keyword

In C#, you can  inherit fields and methods from another class. It’s inheritance..

  1. Derived Class : is child class that inherits from another class.
  2. Base Class : is the parent class which the class being inherited from.

We use the : symbol to inherit from a class.
class Person { // base class
  public string type = "Person";
  public void say() {
    Console.WriteLine("Hello");
  }
}

class Student : Person { // derived class
  public string type = "Student";
  public int studentId = 1;
}
class Program {
  static void Main(string[] args)  {
    Student student = new Student();
    student.say();
    Console.WriteLine(student.type +
         " " + student.studentId);
  }
}

Result :
Hello
Student 1

sealed Keyword

If you don’t want others class inherit from a class, you can use sealed keyword
sealed class Person {
  public string type = "Person";
  public void say() {
    Console.WriteLine("Hello");
  }
}

class Student : Person {
  ...
}
You would get error message that 'Student' : cannot derive from sealed type.

Monday, March 16, 2020

[C#][Resolved] Error : The designer cannot process the code

Error Message:
The designer cannot process the code at line 259, please see the Task List for details. The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again.


Please check are you added bussinese logic in your Designer.cs file.
If yes, please move the your code to the .cs file

An example:
I want a for-loop to create many labels but I added this logic to file "form.Designer.cs", move the logic to "form.cs" solved.

Tuesday, March 3, 2020

[C#] Polymorphism : virtual and override keyword

Overriding

From this example, the field named type is using overriding.

Override field


 Override method

virtual and override keyword

Reference to the example above, the variable student2 and teacher2, this load base class method because it overrides the derived class method, when they share the same name.

However, C# provides an option to override the base class method, by adding the virtual keyword to the method inside the base class, and by using the override keyword for each derived class methods:

Sunday, February 16, 2020

[C#][Example] Dynamic create labels

I put these code into .cs file constructor:
string[] pdfs = new string[]{"Apple","Banana","Mango","Cheery","Orange"};
Label[] labels = new Label[pdfs.Length];
for(int i=0; i<pdfs.Length; i++){
  label[i] = new Label();
  label[i].Name = "label"+i;
  label[i].Location = new Point(27,i*30);
  label[i].TabStop = true;
  label[i].Text = pdfs[i];
  this.Controls.Add(labels[i]);
}

Reference
https://stackoverflow.com/questions/15008871/how-to-create-many-labels-and-textboxes-dynamically-depending-on-the-value-of-an

Sunday, February 2, 2020

[C#][Resolved] Check LinkLable open a link with default browser

Add "System.Diagnostics.Process.Start("http://www.your-website.com");" in your event handler, as usual if you double click the linkLabl in your design view, you would be leaded to related method.

An example:
private void linklabelGuide_LinkClicked(object sender,LinkLabelLinkClickedEventArgs e){
  string url = "https:/www.google.com";
  System.Diagnostics.Process.Start(url);
}
If you run the application and click the linkLabel, default broswer of your operating system would be opened and load the link you specificed.

Reference:

https://stackoverflow.com/questions/7154256/linklabel-open-in-default-web-browser