Advertisement
Nikolcho

Text redactor

May 10th, 2020
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Zadacha
  13. {
  14.     public partial class Form : System.Windows.Forms.Form
  15.     {
  16.        
  17.         public Form()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void boldButton_Click(object sender, EventArgs e)
  23.         {
  24.             if (text.SelectionFont.Bold)
  25.             {
  26.                 if (text.SelectionFont.Underline)
  27.                 {
  28.                     if (text.SelectionFont.Italic)
  29.                     {
  30.                         setFont(false, true, true);
  31.                     }
  32.                     else
  33.                     {
  34.                         setFont(false, true, false);
  35.                     }
  36.                 }
  37.                 else
  38.                 {
  39.                     setFont(false, false, false);
  40.                 }
  41.             }
  42.             else
  43.             {
  44.                 text.SelectionFont = new Font(text.SelectionFont, text.SelectionFont.Style | FontStyle.Bold);
  45.             }
  46.         }
  47.  
  48.         private void clear_Click(object sender, EventArgs e)
  49.         {
  50.             text.Clear();
  51.         }
  52.  
  53.         private void italicButton_Click(object sender, EventArgs e)
  54.         {
  55.             if (text.SelectionFont.Italic)
  56.             {
  57.                 if (text.SelectionFont.Bold)
  58.                 {
  59.                     if (text.SelectionFont.Underline)
  60.                     {
  61.                         setFont(true, true, false);
  62.                     }
  63.                     else
  64.                     {
  65.                         setFont(true, false, false);
  66.                     }
  67.                 }
  68.                 else
  69.                 {
  70.                     setFont(false, false, false);
  71.                 }
  72.             }
  73.             else
  74.             {
  75.                 text.SelectionFont = new Font(text.SelectionFont, text.SelectionFont.Style | FontStyle.Italic);
  76.             }
  77.            
  78.  
  79.         }
  80.  
  81.         private void underlineButton_Click(object sender, EventArgs e)
  82.         {
  83.             if (text.SelectionFont.Underline)
  84.             {
  85.                 if (text.SelectionFont.Bold)
  86.                 {
  87.                     if (text.SelectionFont.Italic)
  88.                     {
  89.                         setFont(true, false, true);
  90.                     }
  91.                     else
  92.                     {
  93.                         setFont(true, false, false);
  94.                     }
  95.                 }
  96.                 else
  97.                 {
  98.                     setFont(false, false, false);
  99.                 }
  100.             }
  101.             else
  102.             {
  103.                 text.SelectionFont = new Font(text.SelectionFont, text.SelectionFont.Style | FontStyle.Underline);
  104.             }
  105.          
  106.  
  107.         }
  108.  
  109.         private void setFont(bool wasB, bool wasU, bool wasI)
  110.         {
  111.             if (wasB)
  112.             {
  113.                 if (wasI)
  114.                 {
  115.                     text.SelectionFont = new Font(text.SelectionFont, FontStyle.Bold | FontStyle.Italic | FontStyle.Regular);
  116.                 }
  117.                 else if (wasU)
  118.                 {
  119.                     text.SelectionFont = new Font(text.SelectionFont, FontStyle.Bold | FontStyle.Underline | FontStyle.Regular);
  120.                 }
  121.             }
  122.             if (wasU)
  123.             {
  124.                 if (wasB)
  125.                 {
  126.                     text.SelectionFont = new Font(text.SelectionFont, FontStyle.Bold | FontStyle.Underline | FontStyle.Regular);
  127.                 }
  128.                 else if (wasI)
  129.                 {
  130.                     text.SelectionFont = new Font(text.SelectionFont, FontStyle.Italic | FontStyle.Underline | FontStyle.Regular);
  131.                 }
  132.             }
  133.             if (wasI)
  134.             {
  135.                 if (wasU)
  136.                 {
  137.                     text.SelectionFont = new Font(text.SelectionFont, FontStyle.Underline | FontStyle.Italic | FontStyle.Regular);
  138.                 }
  139.                 else if (wasB)
  140.                 {
  141.                     text.SelectionFont = new Font(text.SelectionFont, FontStyle.Bold | FontStyle.Italic | FontStyle.Regular);
  142.                 }
  143.             }
  144.         }
  145.  
  146.         private void fontSize_SelectedIndexChanged(object sender, EventArgs e)
  147.         {
  148.             FontChange();
  149.         }
  150.         private void FontChange()
  151.         {
  152.             // throw new Not Implemented Exception();    
  153.             float fontsize = 10;
  154.             string fontfamily = fontFont.SelectedItem.ToString();
  155.             string fontname = text.SelectionFont.Name;
  156.             if (fontSize.Text != "") fontsize = float.Parse(fontSize.Text);
  157.             if (fontsize == 0) fontsize = 10;
  158.             if (text.SelectionLength > 0)
  159.             {
  160.                 text.SelectionFont = new Font(fontfamily, fontsize, text.SelectionFont.Style);
  161.             }
  162.         }
  163.  
  164.         private void fontFont_SelectedIndexChanged(object sender, EventArgs e)
  165.         {
  166.             float fontsize = text.SelectionFont.Size;
  167.             string fontfamily = fontFont.SelectedItem.ToString();
  168.             if (fontfamily != "")
  169.             {
  170.                 text.SelectionFont = new Font(fontfamily, fontsize, text.SelectionFont.Style);
  171.             }
  172.         }
  173.         bool click = false;
  174.         private void fontColor_Click(object sender, EventArgs e)
  175.         {
  176.  
  177.             click = true;
  178.             change(true);
  179.         }
  180.         private void change(bool fontOrSize)
  181.         {
  182.             if (click == true)
  183.             {
  184.                 ColorDialog fd = new ColorDialog();
  185.  
  186.                 if (fd.ShowDialog() == DialogResult.OK)
  187.                 {
  188.                     if (fontOrSize)
  189.                     {
  190.                         text.SelectionColor = fd.Color;
  191.                     }
  192.                     else
  193.                     {
  194.                         text.SelectionBackColor = fd.Color;
  195.                     }
  196.                 }
  197.             }
  198.         }
  199.  
  200.         private void fontBGColor_Click(object sender, EventArgs e)
  201.         {
  202.             click = true;
  203.             change(false);
  204.         }
  205.  
  206.         private void save_Click(object sender, EventArgs e)
  207.         {
  208.             string path = Directory.GetCurrentDirectory();
  209.             using (StreamWriter writer = new StreamWriter("saved.txt"))
  210.             {
  211.                 foreach (var item in text.Text)
  212.                 {
  213.                     writer.Write(item);
  214.                 }
  215.             }
  216.         }
  217.  
  218.         private async void load_Click(object sender, EventArgs e)
  219.         {
  220.             OpenFileDialog fd = new OpenFileDialog();
  221.  
  222.             fd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  223.             if (fd.ShowDialog() == DialogResult.OK)
  224.             {
  225.                 using (Stream stream = fd.OpenFile())
  226.                 {
  227.                     using (var reader = new StreamReader(stream))
  228.                     {
  229.                         string line = await reader.ReadToEndAsync();
  230.                         text.Text = line;
  231.                     }
  232.                 }
  233.             }
  234.  
  235.         }
  236.  
  237.         private void Form_Load(object sender, EventArgs e)
  238.         {
  239.  
  240.         }
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement