So here's this code :
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
mn=textBox1->Text;
MessageBox::Show(mn+" "+tsk, "Info" );
String^ fileName = "records.txt";
StreamWriter^ sw = gcnew StreamWriter("records.txt");
sw->Write(mn,tsk);
sw->Close();
}
Everytime I try to write something new into the file from the program, it just writes the new text and doesn't keep old. How can I save it, so it doesn't delete ?
The documentation for StreamWriter constructor states that you must set the append parameter to true to avoid a mere overwrite of the file. Your code should be:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
mn=textBox1->Text;
MessageBox::Show(mn+" "+tsk, "Info" );
String^ fileName = "records.txt";
StreamWriter^ sw = gcnew StreamWriter("records.txt", true); //append to file
sw->Write(mn,tsk);
sw->Close();
}
Related
How to use variable in another windows form? For example:
This is Form 1 event button click. After clicking th button i want to apear "num"
in textbox "Form2" form.
private: System::Void button_Click(System::Object^ sender, System::EventArgs^ e)
{
int num = 10;
Form2^ f = gcnew Form2();
f->Show();
}
lets suppose that in form2 is textbox named "text"
private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e)
{
text->Text = num;
}
Form2 doesnt see "num" variable.
Im using c++/CLI (visual studio)
Try this instead:
private: System::Void button_Click(System::Object^ sender, System::EventArgs^ e)
{
int num = 10;
Form2^ f = gcnew Form2();
f->text->Text = num.ToString();
f->Show();
}
So i got this error:
argument of type System::String ^ is incompatible with parameter of
type LPCTSTR
When i try to use this code:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ currentDesktop = System::Environment::GetEnvironmentVariable("USERPROFILE") + "\\Desktop\\Test";
CreateDirectory (currentDesktop, NULL);
String^ value = (this->listBox1)->Text;
MessageBox::Show ("File has been created successfully. You've choosen: " + value, "Success", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
};
So i don't know exactly where is the problem, So help me please.
Thank you guys, and thank you #Ben Voigt
This code working fine:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ currentDesktop = System::Environment::GetEnvironmentVariable("USERPROFILE") + "\\Desktop\\Test";
msclr::interop::marshal_context context;
LPCTSTR desktop = context.marshal_as<LPCTSTR>(currentDesktop);
CreateDirectory (desktop, NULL);
String^ value = (this->listBox1)->Text;
MessageBox::Show ("File has been created successfully. You've choosen: " + value, "Success", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
};
I'm having an issue with duplicated content from redirected output.
In my forms I have two buttons: Run and Clear.
public partial class BatchRun : Form
{
Process process = new Process();
public BatchRun()
{
InitializeComponent();
}
private void RunBTN_Click(object sender, EventArgs e)
{
//initiate the process
this.process.StartInfo.FileName = sMasterBATname;
this.process.StartInfo.UseShellExecute = false;
this.process.StartInfo.CreateNoWindow = true;
this.process.StartInfo.RedirectStandardOutput = true;
this.process.OutputDataReceived += new DataReceivedEventHandler(StandardOutputHandler);
this.process.StartInfo.RedirectStandardInput = true;
this.process.Start();
this.process.BeginOutputReadLine();
}
public void StandardOutputHandler(object sender, DataReceivedEventArgs outLine)
{
BeginInvoke(new MethodInvoker(() =>
{
Label TestLBL = new Label();
TestLBL.Text = text.TrimStart();
TestLBL.AutoSize = true;
TestLBL.Location = new Point(10, CMDpanel.AutoScrollPosition.Y + CMDpanel.Controls.Count * 20);
CMDpanel.Controls.Add(TestLBL);
CMDpanel.AutoScrollPosition = new Point(10, CMDpanel.Controls.Count * 20);
}));
}
private void ClearBTN_Click(object sender, EventArgs e)
{
CMDpanel.Controls.Clear();
this.process.CancelOutputRead();
this.process.Close();
this.process.Refresh();
}
}
This works great if I want to run process only once i.e. close the forms once process has completed.
However, I need to allow user to rerun the same process or run a new one hence I have added a clear button the clear various controls etc.
The problem I'm having is that after clicking clear button, I want to click run button again without closing which should then run the sMAsterBAT file(CMD).
StandardOutputHandler seems to be including content of the previous run as well as the new one resulting in duplicated labels in my CMDpanel.
Is this stored in some kind of buffer? If so, How do i clear it to allow me a rerun?
Could someone explain why this is happening and how to resolve it please.
Spoke to someone at work who fixed it for me. So easy lol
private void ClearBTN_Click(object sender, EventArgs e)
{
CMDpanel.Controls.Clear();
this.process.CancelOutputRead();
this.process.Close();
this.process.Refresh();
this.process = new Process(); // this line resolved my issue!!
}
}
I want to read a text file from the Internet and I want while reading the file a picturebox, that is a gif animation, show and after the reading is finished picturebox hide.
I use background worker. I have a lable that shows the state, but when I click BtnCheck Button bg doesn't work and the lable doesn't change.
My code:
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void BtnCheck_Click(object sender, EventArgs e)
{
PbLoading.Visible = true;
if (backgroundWorker1.IsBusy != true)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
LbleState.Text = "Reading txt File...";
webClient1 = new WebClient();
if (CheckForInternetConnection())
{
try
{
Stream stream = webClient1.OpenRead(TxtWebAdrss);
StreamReader reader = new StreamReader(stream);
String content = reader.ReadToEnd();
reader.Close();
LbleState.Text = "Reading Finished .";
}
catch
{
LbleState.Text = "Error reading";
}
}
else LbleState.Text = "Internet not connected!";
}
You may just need to do a bit more research into this class. You should perform UI changes on the UI thread.
There are three event handlers that you can use and these are,
backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
The following link should help,
http://msdn.microsoft.com/en-us/library/System.ComponentModel.BackgroundWorker(v=vs.110).aspx
I have just started building a app using “XihSolutions.DotMSN.dll” version: 2.0.0.40909,
My problem is that it is not firing the “Nameserver_SignedIn” event. Not sure if I am doing something wrong.
your help will be really helpful.
void Nameserver_SignedIn(object sender, EventArgs e)
{
throw new Exception("User Signed In");
}
private string message = string.Empty;
void NameserverProcessor_ConnectionEstablished(object sender, EventArgs e)
{
message = "Connected";
SetMessage();
}
void SetMessage()
{
if (tbMessage.InvokeRequired)
tbMessage. Invoke(new ThreadStart(SetMessage));
else
tbMessage.Text += Environment.NewLine+ message;
}
private void btnSingIn_Click(object sender, EventArgs e)
{
if (messenger.Connected)
{
// SetStatus("Disconnecting from server");
messenger.Disconnect();
}
// set the credentials, this is ofcourse something every DotMSN program will need to
// implement.
messenger.Credentials.Account = tbUserName.Text;
messenger.Credentials.Password = tbPwd.Text;
// inform the user what is happening and try to connecto to the messenger network.
//SetStatus("Connecting to server");
messenger.Connect();
}
You might use MSNPSharp instead - DotMSN is old and may not support the current MSN protocol. There link is here:
http://code.google.com/p/msnp-sharp/