How to use variable in another windows form? - c++-cli

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();
}

Related

incompatible with parameter of type "LPCTSTR"

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);
}
};

Webcam motion Detection using AForge C#

I am using code for motion detection webcam using Aforge. When running the program, it can run perfectly, but when edit libel1 "motion Detection" and libel2 "no motion Detection" it cannot running. Why?
The code I'm using for motion detection:
public Form1()
{
InitializeComponent();
}
FilterInfoCollection fic;
VideoCaptureDevice Device;
MotionDetector motionDetector;
float f;
private void Form1_Load(object sender, EventArgs e)
{
motionDetector = new MotionDetector(new TwoFramesDifferenceDetector(), new MotionAreaHighlighting());
fic = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo item in fic)
{
comboBoxDevice.Items.Add(item.Name);
}
comboBoxDevice.SelectedIndex = 0;
}
private void BtnStart_Click(object sender, EventArgs e)
{
Device = new VideoCaptureDevice(fic[comboBoxDevice.SelectedIndex].MonikerString);
videoSourcePlayer1.VideoSource = Device;
videoSourcePlayer1.Start();
}
private void BtnStop_Click(object sender, EventArgs e)
{
videoSourcePlayer1.Stop();
}
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
if (motionDetector == null) return;
f = motionDetector.ProcessFrame(image);
if (f >0)
{
label1.ForeColor = Color.Red;
label1.Text = "Motion Detected";
}
else
{
label1.ForeColor = Color.Green;
label1.Text = "No Motion Detected";
}
}
private void timer_Tick(object sender, EventArgs e)
{
label3.Text = "Value: " + f.ToString();
}
}
}
I think you want to change a label in your UI when you detect a motion. If you want to change a label in UI, you can't run the change function in the same thread. So, you can change it by defining another thread. Just make sure you prevent race conditions.
// Changing UI Labels (Make thread)
Label1.Invoke((MethodInvoker)delegate {
// What do you want to change?
Label1.Text = "Detecting Motions";
});

Writing and saving text into a file c++ visual studio

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();
}

background worker dont work

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

How radio button is deselected?

bool isChecked = false;
private void Agency3_Checked(object sender, RoutedEventArgs e)
{
isChecked = (bool)(((RadioButton)sender).IsChecked);
}
private void Agency3_Click(object sender, RoutedEventArgs e)
{
RadioButton currentButton = (RadioButton)sender;
if ((bool)currentButton.IsChecked && !isChecked)
{
currentButton.IsChecked = false;
}
else
{
currentButton.IsChecked = true;
isChecked = false;
}
}
This code helps deselect the radio button when it is clicked at the time when it was already selected.
I got this from a post on StackOverflow but it was closed.
Are both functions Agency3_Click() and Agency3_Checked() not called when a radio button is clicked?
What order are they called in which makes this behavior of deselecting a radio button possible?