Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 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 Forms1
  13. {
  14. public partial class Form2 : Form
  15. {
  16. FileStream fileStream;
  17. Byte[] dataBytes;
  18. string fileName;
  19.  
  20. public Form2()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25. private void SaveButton_Click(object sender, EventArgs e)
  26. {
  27. if (richTextBox1.Text.Length > 0)
  28. {
  29. fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
  30. dataBytes = Encoding.ASCII.GetBytes(richTextBox1.Text);
  31. fileStream.Write(dataBytes, 0, dataBytes.Length);
  32. fileStream.Close();
  33. }
  34. }
  35.  
  36. private void OpenButton_Click(object sender, EventArgs e)
  37. {
  38. try
  39. {
  40. StreamReader streamReader;
  41. string line;
  42. OpenFileDialog opf = new OpenFileDialog();
  43. opf.Title = "select file to open";
  44. opf.Filter = "TXT document|*.txt";
  45. if (opf.ShowDialog() == DialogResult.OK)
  46. {
  47. richTextBox1.Text = "";
  48. streamReader = new StreamReader(opf.FileName);
  49. line = streamReader.ReadToEnd();
  50. richTextBox1.Text = line;
  51. streamReader.Close();
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. MessageBox.Show("error reading file: " + ex.StackTrace);
  57. }
  58. }
  59.  
  60. private void AppendButton_Click(object sender, EventArgs e)
  61. {
  62. if (richTextBox1.Text.Length > 0)
  63. {
  64. SaveFileDialog sfdiag = new SaveFileDialog();
  65. sfdiag.Title = "Select file destination";
  66. sfdiag.Filter = "TXT document|* .txt";
  67. if (sfdiag.ShowDialog() == DialogResult.OK)
  68. {
  69. fileStream = new FileStream(sfdiag.FileName, FileMode.Append);
  70. dataBytes = Encoding.ASCII.GetBytes(richTextBox1.Text);
  71. fileStream.Write(dataBytes, 0, dataBytes.Length);
  72. fileStream.Close();
  73. }
  74. }
  75. }
  76.  
  77. private void SplitButton_Click(object sender, EventArgs e)
  78. {
  79. string text;
  80. string[] words;
  81. string splitSym = Microsoft.VisualBasic.Interaction.InputBox("Split symbol");
  82. if (richTextBox1.Text.Length > 0)
  83. {
  84. text = richTextBox1.Text;
  85. words = text.Split(splitSym[0]);
  86. richTextBox1.Text = "";
  87. foreach (string item in words)
  88. {
  89. richTextBox1.Text += item + Environment.NewLine;
  90. }
  91. }
  92. }
  93.  
  94. private void SubstringButton_Click(object sender, EventArgs e)
  95. {
  96. string text;
  97. string nuo = Microsoft.VisualBasic.Interaction.InputBox("nuo");
  98. string iki = Microsoft.VisualBasic.Interaction.InputBox("iki");
  99. if (richTextBox1.Text.Length > Convert.ToInt32(iki))
  100. {
  101. text = richTextBox1.Text;
  102. richTextBox1.Text = "";
  103. richTextBox1.Text = text.Substring(Convert.ToInt32(nuo), Convert.ToInt32(iki));
  104. }
  105. }
  106.  
  107. private void SearchButton_Click(object sender, EventArgs e)
  108. {
  109. string text;
  110. if (richTextBox1.Text.Length > 0)
  111. {
  112. text = richTextBox1.Text;
  113. string ats = Microsoft.VisualBasic.Interaction.InputBox("Iveskit teksta kurio ieskosite");
  114. if (text.Contains(ats))
  115. {
  116. int pos = text.IndexOf(ats);
  117. richTextBox1.Focus();
  118. richTextBox1.Select(pos, ats.Length);
  119. }
  120. else
  121. {
  122. MessageBox.Show("Ieškomos frazės nėra");
  123. }
  124. }
  125. }
  126.  
  127. private void ReplaceButton_Click(object sender, EventArgs e)
  128. {
  129. string rplc, text;
  130. if (richTextBox1.Text.Length > 0)
  131. {
  132. text = richTextBox1.Text;
  133. string replText = Microsoft.VisualBasic.Interaction.InputBox("Iveskite teksta kuri replace'nsit");
  134. if (text.Contains(replText))
  135. {
  136. rplc = Microsoft.VisualBasic.Interaction.InputBox("Iveskite nauja teksta");
  137. richTextBox1.Text = richTextBox1.Text.Replace(replText, rplc);
  138. }
  139. else
  140. {
  141. MessageBox.Show("Nera tokio teksto");
  142. }
  143. }
  144. }
  145.  
  146. public void newTextFile(string file)
  147. {
  148. file = fileName;
  149. try
  150. {
  151. StreamReader streamReader;
  152. string line;
  153. richTextBox1.Text = "";
  154. streamReader = new StreamReader(fileName);
  155. line = streamReader.ReadToEnd();
  156. richTextBox1.Text = line;
  157. streamReader.Close();
  158. }
  159. catch (Exception ex)
  160. {
  161. MessageBox.Show("Error reading file: " + ex.StackTrace);
  162. }
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement