Wowywow
Member
- Joined
- Nov 9, 2019
- Messages
- 16
- Programming Experience
- Beginner
So this was part of another exercise and professor just randomly came up with this extra part just for me ( others got different ones, and much easier ones... ). What I need to do is when he enters extra numbers into the last line of richtextbox ( where the numbers sorted in descending order are ) and then whenever I press "Spacebar" those numbers should be sorted. EX: 20 14 12 2 ( I type in: 14 ), So the last lines changes to "20 14 14 12 2" and so on. I made the "Spacebar press event", but the problem is how do I update the last line of text and add the numbers he types into the string ( I already failed the assignment, because I couldn't make the program during the class. So this is purely for self education ).
The problem is when I try to update the last line it just copies everything... is it possible to even do this with the way I set this up?
This is how the form looks like:
The data should be included.
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string descendingNums = string.Empty;
// private kint. struct
struct trecDalis
{
private string line2;
private string line3;
private string line4;
public void priskLine(string aLine2, string aLine3, string aLine4)
{
line2 = aLine2;
line3 = aLine3;
line4 = aLine4;
}
public void print(RichTextBox rch)
{
rch.Text += '\n' + line2 +
'\n' + line3 +
'\n' + line4 + '\n';
}
}
private void Form1_Load(object sender, EventArgs e)
{
string file = System.IO.Directory.GetCurrentDirectory() + @"\duom.txt";
string data;
if (File.Exists(file))
{
label1.Text = "Failas yra";
data = File.ReadAllText(file);
string[] lines = data.Split('\n');
// skaiciai didejimo tvarka
int[] ascending = lines[0].Split(' ').Select(int.Parse).ToArray();
Array.Sort(ascending);
richTextBox1.Text += "---Skaičiai didėjimo tvarka---" + '\n';
for (int i = 0; i < ascending.Length; i++)
{
richTextBox1.Text += Convert.ToString(ascending[i]) + " ";
}
//--------------------------------
// priskiriama struct
trecDalis uzd = new trecDalis();
uzd.priskLine(lines[1], lines[2],lines[3]);
richTextBox1.Text += '\n' + "---Private kintamieji struct---";
uzd.print(richTextBox1);
//--------------------------------
// balsiai
string str = lines[4] + lines[5] + lines[6];
int bA = 0, bE = 0, bI = 0, bO = 0, bU = 0;
char[] arr = str.ToArray();
for (int i = 0; i < str.Length; i++)
{
if (arr[i] == 'a')
{
bA++;
}
else if (arr[i] == 'e')
{
bE++;
}
else if (arr[i] == 'i')
{
bI++;
}
else if (arr[i] == 'o')
{
bO++;
}
else if (arr[i] == 'u')
{
bU++;
}
}
richTextBox1.Text += "---Balsiai---" + '\n';
richTextBox1.Text += "A=" + Convert.ToString(bA) + '\n';
richTextBox1.Text += "E=" + Convert.ToString(bE) + '\n';
richTextBox1.Text += "I=" + Convert.ToString(bI) + '\n';
richTextBox1.Text += "O=" + Convert.ToString(bO) + '\n';
richTextBox1.Text += "U=" + Convert.ToString(bU);
// --------------------------------------------
// skaiciai maziejimo tvarka
string numDesc = lines[7] + lines[8] + lines[9];
int[] descending = numDesc.Split(' ', '\n', '\r').Select(int.Parse).ToArray();
Array.Sort(descending);
Array.Reverse(descending);
richTextBox1.Text += '\n';
richTextBox1.Text += "---Skaičiai mažėjimo tvarka---" + '\n';
for (int i = 0; i < descending.Length; i++)
{
richTextBox1.Text += Convert.ToString(descending[i]) + " ";
}
descendingNums = string.Join("", descending);
}
else
{
label1.Text = "Failo nėra";
}
}
private void Space_Pressed(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == (char)Keys.Space)
{
string[] lines = richTextBox1.Lines;
lines[13] = richTextBox1.Text;
richTextBox1.Lines = lines;
richTextBox1.Text += descendingNums;
}
}
}
}
The problem is when I try to update the last line it just copies everything... is it possible to even do this with the way I set this up?
This is how the form looks like:
The data should be included.