Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 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.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace File_Browser
  13. {
  14. public partial class TextEditor : Form
  15. {
  16. FileStream fileStream;
  17. Byte[] dataBytes;
  18. string fileName;
  19.  
  20. public TextEditor()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25. public void loadTextEditor(string file)
  26. {
  27. fileName = file;
  28. StreamReader streamReader;
  29. string line;
  30. this.Text += " - Editing " + fileName;
  31. try {
  32. richTextBox1.Text = "";
  33. streamReader = new StreamReader(fileName);
  34. line = streamReader.ReadToEnd();
  35. richTextBox1.Text = line;
  36. streamReader.Close();
  37. } catch (Exception ex) {
  38. MessageBox.Show("Error Reading File: " + ex.StackTrace);
  39. this.Close();
  40. }
  41. }
  42.  
  43. private void SaveButton_Click(object sender, EventArgs e)
  44. {
  45. if (richTextBox1.Text.Length > 0)
  46. {
  47. try {
  48. fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
  49. dataBytes = Encoding.ASCII.GetBytes(richTextBox1.Text);
  50. fileStream.Write(dataBytes, 0, dataBytes.Length);
  51. fileStream.Close();
  52. MessageBox.Show("Successfully Saved");
  53. } catch (Exception err) {
  54. MessageBox.Show(err.Message);
  55. }
  56.  
  57. }
  58. }
  59.  
  60. private void AppendButton_Click(object sender, EventArgs e)
  61. {
  62. if (richTextBox1.Text.Length > 0)
  63. {
  64. try
  65. {
  66. fileStream = new FileStream(fileName, FileMode.Append);
  67. dataBytes = Encoding.ASCII.GetBytes(richTextBox1.Text);
  68. fileStream.Write(dataBytes, 0, dataBytes.Length);
  69. fileStream.Close();
  70. MessageBox.Show("Successfully Appended");
  71. }
  72. catch (Exception err)
  73. {
  74. MessageBox.Show(err.Message);
  75. }
  76.  
  77. }
  78. }
  79.  
  80. private void SplitButton_Click(object sender, EventArgs e)
  81. {
  82. string text;
  83. string[] words;
  84. if (richTextBox1.Text.Length > 0)
  85. {
  86. string split = Microsoft.VisualBasic.Interaction.InputBox("Iveskite simbolį pagal kurį išskirti");
  87. text = richTextBox1.Text;
  88. words = text.Split(split[0]);
  89. richTextBox1.Text = "";
  90. foreach (string item in words)
  91. {
  92. richTextBox1.Text += (item + Environment.NewLine);
  93. }
  94. }
  95. }
  96.  
  97. private void SubstringButton_Click(object sender, EventArgs e)
  98. {
  99. string text;
  100. if (richTextBox1.Text.Length > 4)
  101. {
  102. text = richTextBox1.Text;
  103. string pos1 = Microsoft.VisualBasic.Interaction.InputBox("Iveskite pradžios poziciją");
  104. string pos2 = Microsoft.VisualBasic.Interaction.InputBox("Iveskite pabaigos poziciją");
  105. try
  106. {
  107. richTextBox1.Text = text.Substring(Int32.Parse(pos1), Int32.Parse(pos2));
  108. }
  109. catch (Exception ex)
  110. {
  111. MessageBox.Show("Error while substringing: " + ex.Message);
  112. }
  113. }
  114. }
  115.  
  116. private void SearchButton_Click(object sender, EventArgs e)
  117. {
  118. string text;
  119. if (richTextBox1.Text.Length > 0)
  120. {
  121. text = richTextBox1.Text;
  122. string ats = Microsoft.VisualBasic.Interaction.InputBox("Iveskit teksta kurio ieskosite");
  123. if (text.Contains(ats))
  124. {
  125. int pos = text.IndexOf(ats);
  126. richTextBox1.Focus();
  127. richTextBox1.Select(pos, ats.Length);
  128. }
  129. else
  130. {
  131. MessageBox.Show("Ieškomos frazės nėra");
  132. }
  133. }
  134. }
  135.  
  136. private void ReplaceButton_Click(object sender, EventArgs e)
  137. {
  138. string text;
  139. if (richTextBox1.Text.Length > 0)
  140. {
  141. text = richTextBox1.Text;
  142. string ats = Microsoft.VisualBasic.Interaction.InputBox("Iveskit teksta kuri pakeisite");
  143. string repl = Microsoft.VisualBasic.Interaction.InputBox("Iveskit teksta kuriuo pakeisite");
  144. if (text.Contains(ats))
  145. {
  146. richTextBox1.Text = text.Replace(ats, repl);
  147. }
  148. else
  149. {
  150. MessageBox.Show("Keičiamo teksto nėra");
  151. }
  152. }
  153. }
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement