I like to cancel an event if there is an error, so the rest of the code won't run.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {if(textBox1->Text == "")//cancel event
}
If the text of the textBox1 is empty the event exit so the rest of the code wont run and doesn't create an error.
I do it like this, but is there a better solution to this?
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { if(textBox1->Text == "") Error = 1;
if(Error == 0)//Rest of the code);
}
Thank you.
Imi007
Just 'return;' that would work
Related
I just began learning WinForms and am currently baffled on how to get the senders' (mouses') position (coordinates). I tried searching but to no avail.
This is my, somewhat, of a try but, sadly, it ended up with an error:
private: System::Void pictureBox1_MouseHover(System::Object^ sender, System::EventArgs^ e) {
this->pictureBox1->Location = System::Drawing::Point(sender::Position.X - 5, sender::Position.Y - 5);
MessageBox::Show("Foo", "Bar", MessageBoxButtons::OK, MessageBoxIcon::Stop);
}
So my question here is quite clear, I think: how can I get the senders' position (in this case, the mouses'). Explanations would also be of help. Thank you.
Since I didn't find a valid answer I took the longer route.
Firstly, I declared a boolean in the namespace with the value of false (it will change to true when the mouse will touch the picture). Then I create two new methods: one to get the X and Y of the mouse and execute code if the mouse is touching the picture and the second one to determine whether the mouse is touching the picture or not.
private: System::Void picture_MouseMove(Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
int VMouseX = e->X,
VMouseY = e->Y;
if (VMouseEntered) {
VMouseEntered = false;
this->picture->Location = System::Drawing::Point(VMouseX + 17, VMouseY + 17);
}
}
private: System::Void picture_MouseEnter(System::Object^ sender, System::EventArgs^ e) {
VMouseEntered = true;
}
Then, I create two new EventHandlers for the picture. The first EventHandler is to listen for mouse movement, the second one is to check whether the mouse is touching the picture.
this->picture->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::picture_MouseMove); // Checks for mouse movement.
this->picture->MouseEnter += gcnew System::EventHandler(this, &Form1::picture_MouseEnter); // Checks whether the mouse is touching the picture.
Done. I hope that this will help someone.
On mouse move of a grid, left button pressed is not caught, but right button pressed is caught. Any one know the reason?
private void grid1_MouseMove(object sender, MouseEventArgs e)
{
if (e.RightButton == MouseButtonState.Pressed)
{
//Entered to the loop
}
}
private void grid1_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
//Not enter to the loop
}
}
There could be any number of reasons, but as you didn't provide a Minimal, Complete, and Verifiable example, we can't really tell you for sure. There is certainly nothing wrong with the following code, that works as expected in a new project:
private void grid1_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
//Entered the loop
}
if (e.RightButton == MouseButtonState.Pressed)
{
//Entered the loop
}
}
The most likely reasons why your code never entered your if statement are as follows:
You weren't moving the mouse when clicking the left mouse button.
You weren't over the Grid, when you clicked the left mouse button.
You are handling the left click in a tunnelling event (Preview... event) and setting e.Handled to true.
If these suggestions do not help, then please follow the advice in the linked help page and provide a Minimal, Complete, and Verifiable example that we can use to further help.
I am currently building a windows form application and I have got the next problem,
I can't declare a variable global because the syntax im using won't allow me to do that, also, i need to declare the variable in the method it self and at last, it must loop so its able to count. This is what I have got so far:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int i;
m_bIsTimerOn = true;
while (m_bIsTimerOn)
{
i++;
label1->Text = (i.ToString());
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
m_bIsTimerOn = false;
timer1->Enabled = false;
}
m_bIsTimerOn is a globally boolean. As you might see here my problem is if i press button1 the program is just stuck in the while loop. I would like to know that the moment you press button2 the while loop stops I also would like to know if this is even possible. If you could response in c++ that would also be fine.
Thank you in advance.
I suppose you want to increment i while timer is on. If you need to update i for each timer tick then it's pretty easy, remove that loop (and that variable BTW) and simply do update inside timer Tick event:
private:
int _i;
void button1_Click(Object^ sender, EventArgs^ e) {
_i = 0;
timer1->Enabled = true;
}
void _timer1_Tick(Object^ sender, EventArgs^ e) {
label1->Text = (i++).ToString();
}
void button2_Click(Object^ sender, EventArgs^ e) {
timer1->Enabled = false;
}
If i must be updated independently from timer ticks then you have to move it to a BackgroundWorker or simply in event handler for Application::Idle:
void OnIdle(Object^ sender, EventArgs^ e) {
label1->Text = (_i++).ToString();
}
void button1_Click(Object^ sender, EventArgs^ e) {
_i = 0;
Application::Idle += gcnew EventHandler(this, Form1::OnIdle);
timer1->Enabled = true;
}
void button2_Click(Object^ sender, EventArgs^ e) {
Application::Idle -= gcnew EventHandler(this, Form1::OnIdle);
timer1->Enabled = false;
}
As final note: you may even keep your loop as is and put a call to Application::DoEvents() just after your label1->Text = (i.ToString()); but this will probably consumes a lot of CPU, slow down your application and open your code to reentrancy, I'd really avoid something like that...
Your program is in endless loop because the while loop prevents the messages from getting processed and hence your button2_Click does not get invoked. To make you application capable of processing the messages that occurs, add Application.DoEvents() in your loop as:
while (m_bIsTimerOn)
{
i++;
label1->Text = (i.ToString());
Application.DoEvents(); // causes the application to handle pending events
}
So now, if you press the second button, m_bIsTimerOn will become false and your loop will terminate.
I have been recently working on a program and i tried to use System::String^ to use it for an ofstream file...
I always get an Error...
Note This is a Form so it is a Header File.
This is my Code...(one part)
System::String^ txtPath;
#pragma endregion
private: System::Void btn1_Click(System::Object^ sender, System::EventArgs^ e) {
using namespace std;
ofstream fout (txtPath->c_str());
}
private: System::Void txtBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
txtPath=(Convert::ToString(txtBox1->Text))+"\\weapon_deagle.txt";
}
The Error I get is :
Error 1 error C2039: 'c_str' : is not a member of 'System::String' c:\users\a\documents\visual studio 2010\projects\d1s1k formed\d1s1k formed\Form1.h
101
i'm pretty new to programming in C# so please don't be mean with the following question.
I want to change the Background color of a label (MonoTouch) periodically, but it simply does't.
Anyway, the value of "_hue" is printed in the console periodically.
public void _timer_elapsed(object sender, ElapsedEventArgs e)
{
_hue -= 0.1f;
lblScreen.BackgroundColor = UIColor.FromHSB (_hue,_sat,_bri);
Console.WriteLine (_hue);
}
Do you guys have a hint?
THANX! :-)
public void _timer_elapsed(object sender, ElapsedEventArgs e)
{
_hue -= 0.1f;
InvokeOnMainThread(delegate{
lblScreen.BackgroundColor = UIColor.FromHSB (_hue,_sat,_bri);
});
Console.WriteLine (_hue);
}
Here is a link on threading with MonoTouch.
You are likely receiving the timer event on a background thread. Using BeginInvokeOnMainThread to update the UI will likely fix it.