Button click Open richTextBox and display the readfile - c#

I have 2 buttons and I read different files when I click on these buttons. I used the to display the readfile using MsgBox since the files are big, so i want to display it in a richTextBox.
How can I open a richTextBox and display the read file when I click on any one of these buttons???
private void button1_Click(object sender, EventArgs e)
{
DisplayFile(FileSelected);//DisplayFile is the path of the file
var ReadFile = XDocument.Load(FileSelected); //Read the selected file to display
MessageBox.Show("The Selected" + " " + FileSelected + " " + "File Contains :" + "\n " + "\n " + ReadFile);
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo)comboBox2.SelectedItem;
StreamReader FileRead = new StreamReader(file.FullName);
string FileBuffer = FileRead.ReadToEnd(); //Read the selected file to display
//MessageBox.Show("The Selected" + " " + file + " " +"File Contains :" + "\n " + "\n " + FileBuffer);
// richTextBox1.AppendText("The Selected" + " " + file + " " + "File Contains :" + "\n " + "\n " + FileBuffer);
//richTextBox1.Text = FileBuffer;
}
Is there any other way to do it?

Here is a simple example (code based form design). It's better if you create the form via the GUI designer:
private void button1_Click(object sender, EventArgs e)
{
//test call of the rtBox
ShowRichMessageBox("Test", File.ReadAllText("test.txt"));
}
/// <summary>
/// Shows a Rich Text Message Box
/// </summary>
/// <param name="title">Title of the box</param>
/// <param name="message">Message of the box</param>
private void ShowRichMessageBox(string title, string message)
{
RichTextBox rtbMessage = new RichTextBox();
rtbMessage.Text = message;
rtbMessage.Dock = DockStyle.Fill;
rtbMessage.ReadOnly = true;
rtbMessage.BorderStyle = BorderStyle.None;
Form RichMessageBox = new Form();
RichMessageBox.Text = title;
RichMessageBox.StartPosition = FormStartPosition.CenterScreen;
RichMessageBox.Controls.Add(rtbMessage);
RichMessageBox.ShowDialog();
}

Related

C#, Write in a .txt file, without deleting the previous line [duplicate]

This question already has answers here:
Open existing file, append a single line
(9 answers)
Closed 4 years ago.
Basicly I'm writing a fake malware, which will write in a .txt the Session username of the person which clicked on it. The problem is that when someone execute it will erase the previous lines.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "Fake Malware";
}
private void Form1_Load(object sender, EventArgs e)
{
string createText = " just clicked on the fake Malware";
File.WriteAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName + createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
private void zig(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("Link to a warning message on click, nothing important.");
string createText = " just clicked on the fake Malware";
File.WriteAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName+ createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
}
I think what you are looking for is AppendAllText:
File.AppendAllText("//vm-files/users/ngallouj/zig/zig.txt", "Wow it worked :)");
Here is the fix
private void Form1_Load(object sender, EventArgs e)
{
string createText = " just clicked on the fake Malware";
File.AppendAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName + createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
private void zig(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("#");
string createText = " just clicked on the fake Malware";
File.AppendAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName+ createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
}
}

in C#, Append selection multiple times from 2 ComboBoxes into a TextBox using a button

I am trying to append selections from 2 ComboBoxes into a TextBox with a click of a button and I am not sure how to do this. I can do it once with code like this:
private void BTN_APPEND_Click(object sender, EventArgs e)
{
TB_CONNECTORS.Text = CB_PORT_NUMBER.Text + " " + CB_CONNECTOR.Text;
}
And this will result in
Append with the click of a button
but the question is, how can I append to this one more time?
Did you try this :
if(!TB_CONNECTORS.Text == "")
{
TB_CONNECTORS.Text = TB_CONNECTORS.Text + " & " + CB_PORT_NUMBER.Text + " " + CB_CONNECTOR.Text;
}
else
{
TB_CONNECTORS.Text = CB_PORT_NUMBER.Text + " " + CB_CONNECTOR.Text;
}
This will result in the new value achieved from the 2 comboboxes keeping the existing text of the textbox

How to append text to my file so it adds new content instead of overwrite

Everything is working until I register a new user and click "save" it overwrites whats in the text file with the new info, I would like to add a new line and start to add the contents array again. I know there is an "append" function but I've tried many things and cant seem to get it to work with my current way of writing to a file.
private async void button1_Click(object sender, EventArgs e)
{
string[] contents = new string[4];
contents[0] = "Name: " + txtName.Text;
contents[1] = "Address: " + txtAddress.Text;
contents[2] = "Phone: " + txtPhone.Text;
contents[3] = "Blood Type: " + cmbBloodType.SelectedItem.ToString();
System.IO.File.WriteAllLines( #"C:\Users\Ben\Documents\C#\Final\Bloodbank\BloodBank\bin\Debug\Bloodbank.txt", contents);
}
I think it's because your content is an array, so you have to join this array in one string like:
System.IO.File.AppendAllText(#"C:\Users\Ben\Documents\C#\Final\Bloodbank\BloodBank\bin\Debug\Bloodbank.txt", string.Join(Environment.NewLine, contents));
Have you tried File.AppendAllText()?
Here is an example:
File.AppendAllText(#"C:\Users\Ben\Documents\C#\Final\Bloodbank\BloodBank\bin\Debug\Bloodbank.txt", "content" + Environment.NewLine);
Edited Code: When you go to save your file, try:
private async void button1_Click(object sender, EventArgs e)
{
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
stringBuilder.AppendLine("Name: " + txtName.Text);
stringBuilder.AppendLine("Address: " + txtAddress.Text);
stringBuilder.AppendLine("Phone: " + txtPhone.Text);
stringBuilder.AppendLine("Blood Type: " + cmbBloodType.SelectedItem.ToString());
System.IO.File.AppendAllText(#"C:\Users\Ben\Documents\C#\Final\Bloodbank\BloodBank\bin\Debug\Bloodbank.txt", stringBuilder.ToString() + Environment.NewLine);
}
That should append the text to the end of the file. The official documentation can be found here: https://msdn.microsoft.com/en-us/library/ms143356(v=vs.110).aspx
Hope this helps!

Calculate Average from a Textfile C#

private void button1_Click(object sender, EventArgs e)
{
try
{
var WriteToFile = new System.IO.StreamWriter("student.txt"); //create textfile in default directory
WriteToFile.Write(txtStudNum.Text + ", " + txtStudName.Text + ", " + txtModCode.Text + ", " + txtModMark.Text);
WriteToFile.Close();
this.Close();
}
catch (System.IO.DirectoryNotFoundException ex)
{
//add error message
}
}
private void button3_Click(object sender, EventArgs e)
{
File.AppendAllText("student.txt", "\r\n" + txtStudNum.Text + ", " +
txtStudName.Text + ", " + txtModCode.Text + ", " + txtModMark.Text);
}
private void button4_Click(object sender, EventArgs e)
{
}
I want to calculate the average for txtModMark from the textfile once all the values have been entered. It will go under button 4 so when I click it, it calculates. I need to know how to skip the first few columns per row and get to the last column to perform the average calculation.
What's the point in reading from file then parse it and then convert to INT and then calculate average when you can do it directly using s List<int> like below:
declare a List<int> in your Form
List<int> marks = new List<int>();
Store the marks in button click event
private void button1_Click(object sender, EventArgs e)
{
try
{
var WriteToFile = new System.IO.StreamWriter("student.txt"); //create textfile in default directory
WriteToFile.Write(txtStudNum.Text + ", " + txtStudName.Text + ", " + txtModCode.Text + ", " + txtModMark.Text);
WriteToFile.Close();
marks.Add(Convert.ToInt32(txtModMark.Text)); //add to list
}
catch (System.IO.DirectoryNotFoundException ex)
{
//add error message
}
}
In button4 click event calculate the average
private void button4_Click(object sender, EventArgs e)
{
int totalmarks = 0;
foreach(int m in marks)
totalmarks += m;
MessageBox.Show("Average Is: " + totalmarks / marks.Count);
}

dynamically create a button

In Visual Studio C# windows form how do I dynamically create a button each time an item is inserted into a listbox ?
When the created button is clicked it has to remove the inserted item from the listbox.
I want to add the button th this:
if (comboBox4.Text != "" && listBox1.Text != "" && comboBox3.Text != "")
{
string ha = listBox1.SelectedItem.ToString();
Clipboard.SetText(comboBox4.Text + "stk " + ha + " i farve " + comboBox3.Text);
listBox2.Items.Add(comboBox4.Text + "stk " + listBox1.SelectedItem.ToString() +
" " + comboBox3.Text);
}
Create a button and add to the form.
Pseudo-code (not the complete solution, only the main login behind it):
public class MyEventArgs: EventArgs
{
public ListBoxValue lbv;
}
...
listBox2.Items.Add(comboBox4.Text + "stk " + listBox1.SelectedItem.ToString() + " " + comboBox3.Text);
Button x = new Button();
x.Text = "whatever";
x.Top = some coordinate;
x.Left = some coordinate;
MyEventArgs me = new MyEventArgs();
me.lbv = inserted lisbox item reference;
x.Click += new ClickHandler(this, me);
myForm.Controls.Add(x);
...
private void ClickHandler(object sender, MyEventArgs e)
{
listbox.Items.Remove(e.lbv);
}

Categories