Deserialize json object with dynamic items in C#

Your code structure is not reflecting your json. Common approach to deserializing json with dynamic property names is to use Dictionary<string, …> (supported both by Json.NET and System.Text.Json). Try the following: public class Bike { public int Value { get; set; } public string Type { get; set; } } public class Person { public … Read more

sending mail along with embedded image using asp.net

If you are using .NET 2 or above you can use the AlternateView and LinkedResource classes like this: string html = @”<html><body><img src=””cid:YourPictureId””></body></html>”; AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html); LinkedResource yourPictureRes = new LinkedResource(“yourPicture.jpg”, MediaTypeNames.Image.Jpeg); yourPictureRes.ContentId = “YourPictureId”; altView.LinkedResources.Add(yourPictureRes); MailMessage mail = new MailMessage(); mail.AlternateViews.Add(altView); Hopefully you can deduce the VB equivalent.

How do I set the windows default printer in C#?

using System; using System.Drawing.Printing; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private void listAllPrinters() { foreach (var item in PrinterSettings.InstalledPrinters) { this.listBox1.Items.Add(item.ToString()); } } private void listBox1_SelectedValueChanged(object sender, EventArgs e) { string pname = this.listBox1.SelectedItem.ToString(); myPrinters.SetDefaultPrinter(pname); } public Form1() { InitializeComponent(); listAllPrinters(); } } public static class myPrinters … Read more

How to programmatically set cell value in DataGridView?

If the DataGridView is databound, you shouldn’t directly modify the content of the cell. Instead, you should modify the databound object. You can access that object through the DataBoundItem of the DataGridViewRow : MyObject obj = (MyObject)dataGridView.CurrentRow.DataBoundItem; obj.MyProperty = newValue; Note that the bound object should implement INotifyPropertyChanged so that the change is reflected in … Read more

How can I display a system tray icon for C# window service.?

Services run in a different window station than the logged in user, so you can’t have a system tray icon for them. From https://learn.microsoft.com/en-us/dotnet/framework/windows-services/introduction-to-windows-service-applications: Windows Service applications run in a different window station than the interactive station of the logged-on user. A window station is a secure object that contains a Clipboard, a set of … Read more

Set background color of WPF Textbox in C# code

textBox1.Background = Brushes.Blue; textBox1.Foreground = Brushes.Yellow; WPF Foreground and Background is of type System.Windows.Media.Brush. You can set another color like this: using System.Windows.Media; textBox1.Background = Brushes.White; textBox1.Background = new SolidColorBrush(Colors.White); textBox1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0)); textBox1.Background = System.Windows.SystemColors.MenuHighlightBrush;