Opening a form C++ from 2 different forms - forms

I have 3 forms:
MainForm
ManualForm
AutomaticForm
I have a button MainForm-> ManualForm:
private: System::Void btnPowerOn_Click(System::Object^ sender, System::EventArgs^ e)
{
ManualForm^ form = gcnew ManualForm();
form->ShowDialog();
}
I also have a button for AutomaticForm -> ManualForm:
private: System::Void btnManual_Click(System::Object^ sender, System::EventArgs^ e)
{
this->Close();
ManualForm^ manForm = gcnew ManualForm();
manForm->ShowDialog();
}
but when I added this second button it says:
syntax error: identifier 'ManualForm'. (in the AutomaticForm.h)
In both MainForm and AutomaticForm I have the #include "ManualForm.h".
So how can I solve this to let my buttons do their job? (switching between forms)

Ok it seems it is a problem with including each other.
I simple solved it by not closing eachother and keeping the previous Form open.
So for each button I just do the following:
private: System::Void btn_Automatic_Click(System::Object^ sender, System::EventArgs^ e)
{
//this->Close(); removed this line so I don't have to reopen it again
AutomaticForm^ aForm = gcnew AutomaticForm();
aForm->ShowDialog();
}
and when I want to go back to the manualForm, I just simply do this:
private: System::Void btn_Manual_Click(System::Object^ sender, System::EventArgs^ e)
{
this->Close();
}

Related

Opening forms show function. Visual Studio c++

I have main window.
main.cpp
#include "mainWindow.h"
using namespace System;
using namespace System::Windows::Forms;
int main()
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Client::mainWindow mainWin;
Application::Run(%mainWin);
return 0;
}
And i have another form, which should open when the main form openning:
mainWindow.h
private: System::Void mainWindow_Shown(System::Object^ sender, System::EventArgs^ e) {
Client::logForm^ myForm = gcnew logForm();
myForm->Show();
//Client::logForm logF;
//logF.Show();
}
And my question: why using the commented code, the form opens and closes immediately, but uncommented code works good?
Thank you!

Static and non-static method or global class object

I'm making a Windows Forms Application in VS2012 C++.
Situation just for example, real project is more complicated:
I have a Form that contains TextBox, Button and Timer.
Button just triggers the timer. Timer just calls function that increments some variable.
I need to display the function's variable that is incremented, in TextBox.
In Form1.h I add code:
public: void Timer_Function(); //function activated by timer Tick
void Set_Text(String ^str); //function to set TextBox Text
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (timer1->Enabled == false) timer1->Enabled = true;
else timer1->Enabled = false;
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
Timer_Function();
}
In My_app.cpp code like this:
#include "stdafx.h"
#include "Form1.h"
#include "resource.h"
using namespace test_staticfunc;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return 0;
}
void Form1::Timer_Function()
{
Timer_Func();
}
void Form1::Set_Text(String ^str)
{
textBox1->Text = str;
}
void Timer_Func()
{
static int I=0;
I++;
Form1::Set_Text(I.ToString());
}
Function Timer_Func() is specified in "resource.h" like this:
void Timer_Func();
I.e. I'm trying to display the current state of inner variable I of Timer_Func() by passing it to a Form1 public method Set_Text().
So. The error here is that Set_Text() is not a static method.
I tried to make it static, but got an error "ะก2227: The operand to the left of "->Text" is not a pointer to a class, structure, or union." How to get it right? In that case a static method is trying to implement a non-static method, right?
Or another way: to make an instance of Form1 - instead of
Application::Run(gcnew Form1());
insert code
Form1 ^My_form = gcnew Form1();
Application::Run(My_form);
And use Set_Text as non-static method for class instance My_form.
But My_form is available only in main()! I couldn't make My_form anywhere else. Is there way to make it global or something?
May be there are other ways to solve this problem?
Help, please! I've already searched several forums for answer but didn't find the answer. More precisely non of them suited.
P.S. Sorry for my bad english! ^_^

Drawing in Form_Load

So, I have:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
DrawBackground();
}
private: System::Void DrawBackground(){
Graphics^ g1=this->CreateGraphics();
SolidBrush^ p1 = gcnew SolidBrush(System::Drawing::Color::Gray);
g1->FillRectangle(p1,90,150,600,150);
}
This should load this rectangle when form loaded, but it doesn't.
Why ?? How to you write this correctly ?
You cannot draw in the Load event, the form isn't visible yet. Nor is it ever correct to use CreateGraphics(). Whatever you draw won't survive until the next redraw. Override the OnPaintBackground method instead. Like this:
protected:
virtual void OnPaintBackground(PaintEventArgs^ e) override {
__super::OnPaintBackground(e);
SolidBrush brush(System::Drawing::Color::Gray);
e->Graphics->FillRectangle(%brush,90,150,600,150);
}
Note the use of the stack semantics for brush (no hat), that ensures the brush is automatically disposed.

Drag n Drop Operations across MDI Child Forms

I am trying to perform a simple drag-n-drop operation starting from one button in one MDI child form to another button in a different MDI child form. For some reason however the DragDrop event never gets fired when I attempt to drag one button to the other. It may be worth noting that when I drag the button my cursor becomes the black-cancel icon.
My code:
#region ActivatesDragDropControl
[DllImport ("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private const int WM_NCACTIVATE = 0x0086;
#endregion
private void button1_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(LocationNode, DragDropEffects.Link);
// to deactivate
SendMessage(Handle, WM_NCACTIVATE, 0, 0);
}
private void button1_DragDrop(object sender, DragEventArgs e)
{
//never gets here...
}
private void button1_DragEnter(object sender, DragEventArgs e)
{
// to activate
SendMessage(Handle, WM_NCACTIVATE, 1, 0);
}
OK so I played around a bit more and using DragEnter isn't enough; I had to set the DragEventArgs' Event value. In my case:
e.Effect = DragDropEffects.Link;

show modal popup from code behind

i have a dropdownlist
in codebehind,i have this function
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
///////
}
now i want to show modal popup when a particular text is selected from the dropdownlist from this function
if(DropDownList1.SelectedItem.Text.Equals("Some Text"))
{
ModalPopupExtender1.Show();
}