Skip to main content

Posts

Showing posts with the label C#

Featured Post

How to create customizable products in Shopify for Free (No coding!)

Creating customizable products in your Shopify store not only increase your customer satisfaction, it also increases your conversion rate.  Here is a method to add custom options for your Shopify product and convert it into personalized product - Go to Shopify store page and search for "Advanced Product Customizer" app or you can directly the the Shopify app page by clicking on this link Advanced Product Customizer . Install  Advanced Product Customizer  app for free in your Shopify store  From the application dashboard, enable to app embed block to complete the installation process Click "Product Custom Options"  From the Shopify products list, select the product on which you want to add custom options. Advanced Product Customizer  offers Image Swatch, Color Swatch, Text box, File Upload, Radio, Checkbox, Date Picker and more. Here is the a demo video for adding custom option for a Shopify product -

Convert class object to XML using C# in .NET

People is the class whose object needs to be converted in XML string which later can be stored in a DB field or as XML file. public class PersonDetail { public string FirstName { get ; set ; } public string LastName { get ; set ; } } public class People { public People () { PeopleList = new List<ProductImage>(); } public List<PersonDetail> PeopleList{ get ; set ; } } Function to convert class object to XML string: public static string SerializeToXml<T>(T value ) { StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); XmlSerializer serializer = new XmlSerializer( typeof (T)); serializer.Serialize(writer, value ); return writer.ToString(); } C# code to convert class object to XML string: People peopleDetailList = new People(); PersonDetail pDetail = new PersonDetail (); ...

Enable, Disable and Start the task of task scheduler in C#.Net

Below function demonstrates how to enable, disable and run the already created scheduled task. It will first check if the task exists in task scheduler: public static void updateUserTaskInScheduler ( string action) { try { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe" ; startInfo.Arguments = "/C schtasks /query /TN <<TaskNameWithQuotes>>; //Check if task exists startInfo.RedirectStandardOutput = true ; startInfo.UseShellExecute = false ; startInfo.CreateNoWindow = true ; startInfo.WindowStyle = ProcessWindowStyle.Hidden; if (System.Environment.OSVersion.Version.Major < 6 ) { startInfo.Verb = "runas" ; } using (Process process = Process.Start(startInfo)) { ...

Drawing a dotted circle using C#.NET windows form

Dotted circle can be used for showing circular progress. Here is the code to draw a dotted/dashed circle using C# in windows form application: 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.Drawing.Drawing2D ; using System.Threading ; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1 () { InitializeComponent(); } int angle = 0 ; int Startangle = 0 ; private void Form1_Load ( object sender, EventArgs e) { } private void panel1_Paint ( object sender, PaintEventArgs e) { //draw a dashed cricle using(Graphics g = panel1.CreateGraphics()) { System.Drawing.Rectangle rectangle = new System.Drawi...

Convert content of XML file to JSON string in C#.Net

Convert content of XML file to JSON string in C#.Net The below function will convert the content of XML file to JSON string: private string convertXMLToJSON () { string xmlLogPath = "<<Path to the xml file>>" ; // Load the XML file var xDocument = System.Xml.Linq.XDocument.Load(xmlLogPath); // Read the XML file content and write it to string string xml = xDocument.ToString(); // Load the XML string XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); // Convert the XML string to JSON string string jsonText = JsonConvert.SerializeXmlNode(doc); //Release the memory doc = null ; xmlLogPath = null ; xml = null ; xDocument = null ; //Return the JSON string return jsonText; } IMPORTANT NOTE: Releasing object memory is...

Display Tooltip for Combo Box item C#.NET Winforms

In windows form combo box control sometimes while adidng items dynamically we have items whose width is greater than width of combox box control. In this case for making UI more user friendly we can show tooltip over such item. Here is the sample C# code to display such tooltip:  Add a Tooltip control on the form.  Add following code : this . combo_box1 . DropDownStyle = System . Windows . Forms . ComboBoxStyle . DropDownList; this . combo_box1 . DrawMode = DrawMode . OwnerDrawFixed; this . combo_box1 . DrawItem += new DrawItemEventHandler(combo_box1_DrawItem); this . combo_box1 . DropDownClosed += new EventHandler(combo_box1_DropDownClosed); this . combo_box1 . MouseLeave += new EventHandler(combo_box1_Leave); void combo_box1_DrawItem( object sender, DrawItemEventArgs e) { if (e . Index < 0 ) { return ; } string text = combo_box1 . GetItemText(combo_box1 . Items[...

Image Resizer using C#.NET Windows Application

Code to Resize Image (with Sample Application) In this post I am sharing the code to resize an image using C#.NET. Also sharing the code to preview and save resized image in multiple formats (jpeg, bmp, png, gif, tiff). Code to Resize Image for given width and height public static Bitmap ResizeImage(Image image, int width, int height) { var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using ( var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; ...

Draw transparent Label on PictureBox C#.NET

Label control supports transparent property but picture box do not work as container control. So even if we add label control on picturebox the parent control will become main form or any other underlying container control. Hence on adding label control on picture box and making label control background  transparent shows background of form or any other container control is rather than picture box. This can be changed using simple code in form constructor. We need to change parent property of label and recalculate its location because it is now relative to picturebox instead of form. Hetre is the sample code to do this -  public Form1() {         InitializeComponent();         var pos = this.PointToScreen(label1.Location);         pos = pictureBox1.PointToClient(pos);         label1.Parent = pictureBox1;         label1.Location = pos;         labe...

Recursively delete files from Directory C#.NET

Recursively delete files from given directory keeping the directory structure intact C#.NET To get all the files present in the subdirectories of the given directory, use "SearchOption.AllDirectories" DirectoryInfo  DirInfo   =  new   DirectoryInfo ( directoryPath ); DirInfo . GetFiles   ( "*" ,  SearchOption . AllDirectories   ). ToList (). ForEach ( file   => file . Delete ()); The above code will delete all the files for the given directory while keeping the structure of directory intact. To send files in recycle bin instead of deleting it from system use "FileSystem.DeleteFile" function of VisualBasic assembly. Add reference to VisualBasic dll and add "using Microsoft.VisualBasic.FileIO" DirInfo   =   new   DirectoryInfo (   directoryPath   ); List < FileInfo >  fileList   =   DirInfo . GetFiles ( "*" ,   SearchOption . AllDirectories ). ToList (); foreach ...

Split list into sublists using LINQ in C#.NET

Here is the sample LINQ C# code to split list into sublists public   static   List < List <T>> Split < T >(   this   List   <T>  source ,  int   NumberOfGroup   )         {              return   source                 . Select (( x   ,  i )=> new   {  Index   =  i ,  Value   =  x   })                 . GroupBy ( x   =>  x . Index   /  NumberOfGroup   )                 . Select ( x   =>  x . Select   ( v   =>  v . Value ). ToList ())        ...

Capture or Detect mouse click on or outside Windows Form using C#.NET

Sometimes in case of Windows form application where we have multiple windows opened and we need to find on which window user has clicked. One method to find this is to capture the mouse click message.  Here is the sample code to close form if mouse click happen outside the form.    public   partial   class   frmTest  :   Form     {           class   Window CaptureChanged   :   NativeWindow         {               public   CaptureChanged   OnCaptureChanged ;               protected   override   void   WndProc ( ref   Message   m )             {             ...