Copying and Pasting cs Code
In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.
For example, you can follow the pattern in this listing: private void Form1_Load(object sender, EventArgs e)
{
bot = new ChatBot();
// Sets Position for the first bubble on the top
bbl_old.Top = 0 - bbl_old.Height;
// Load Chat from the log file
if (File.Exists("chat.log"))
{
using (StreamReader sr = File.OpenText("chat.log"))
{
int i = 0; // to count lines
while (sr.Peek() >= 0) // loop till the file ends
{
if (i % 2 == 0) // check if line is even
{
addInMessage(sr.ReadLine());
}
else
{
addOutMessage(sr.ReadLine());
}
i++;
}
// scroll to the bottom once finished loading.
panel2.VerticalScroll.Value = panel2.VerticalScroll.Maximum;
panel2.PerformLayout();
}
}
}
{
bot = new ChatBot();
// Sets Position for the first bubble on the top
bbl_old.Top = 0 - bbl_old.Height;
// Load Chat from the log file
if (File.Exists("chat.log"))
{
using (StreamReader sr = File.OpenText("chat.log"))
{
int i = 0; // to count lines
while (sr.Peek() >= 0) // loop till the file ends
{
if (i % 2 == 0) // check if line is even
{
addInMessage(sr.ReadLine());
}
else
{
addOutMessage(sr.ReadLine());
}
i++;
}
// scroll to the bottom once finished loading.
panel2.VerticalScroll.Value = panel2.VerticalScroll.Maximum;
panel2.PerformLayout();
}
}
}