|
ernst20020530 wrote: a safe or pure image cannot contain data segments.
Seems fairly clear to me...what else do you need to know?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
I have installed a mouse hook to trap any mouse action on my application. The hook is installed at OnCreate function of MDI frame using.
m_hMouseHook = ::SetWindowsHookEx(WH_MOUSE, MouseHookProc, NULL, ::GetCurrentThreadId());
And in MouseHookProc function:
LRESULT CMainFrame:: MouseHookProc (int nCode, WPARAM wParam, LPARAM lParam )
{
CMainFrame* pThis = (CMainFrame*)AfxGetMainWnd();
if (nCode < 0) // do not process message
return CallNextHookEx(pThis->m_hKeyHook, nCode, wParam, lParam);
EnterCriticalSection(&(pThis->m_Lock));
pThis->m_nTimerCount = 0;
LeaveCriticalSection(&(pThis->m_Lock));
return CallNextHookEx(pThis->m_hKeyHook,nCode,wParam,lParam);
}
Now, the mouse hook behaving as expected except when I am hovering the mouse on menu items means if open the File menu item and moving the mouse on the menu items available under File menu then the hook is not working (MouseHookProc function is not handled).
Please any one provide any suggestion/idea.
Thanks in advance.
Sandip
|
|
|
|
|
Using the MenuStrip and OpenFileDialog controls with Windows Forms, I can read a data file (openFiledialog) and store the numerical day in an array. But I can't figure out how to access the data stored in the array from another control, for example from the button control using the click event handler? I would be greatly relieved if someone can help me figure how to do this. Many thanx,
eddy
|
|
|
|
|
isaaks wrote: But I can't figure out how to access the data stored in the array from another control, for example from the button control using the click event handler?
What is the scope of that array? If it has a class level scope, you should be able to access it from event handler.
|
|
|
|
|
Thanx for your reply. The array is a managed array (array<float>^ xx = gcnew array<float>(100)) but I don't know how to give it class scope. However, I found that an array (using pointers) with global scope seems to work (float *xx = new float[100]).
|
|
|
|
|
Hard to tell more without seeing the code.
|
|
|
|
|
Hello, thanks for looking at this!
I am a fairly new programmer just trying to learn new things so try to bear with me.
Here is my problem:
I don't know how to code something that creates new variables that store values that change per repetition of a loop.
To clarify, here is my situation:
I am building a math flashcard program that has 3 forms. The first is irrelevant to the question. The second form uses the rand() function to make up 2 numbers for the user to either add or subtract, they enter their answer into a textbox and then hit a button that stores their answer and gives them new numbers to work with (this happens 10 times). The third box is supposed to display the questions and correct answers of the randomly generated questions.
Further clarification of my problem using an example of what happens to me:
1st Iteration of loop starts:
1. Random number generator gives the numbers 2 and 3 for the user to add
2. User enters 5 and hits the continue button
3. The continue button stores the numbers 2, 3, and 5 as variables named "1num", "2num", and "ans" respectively.
2nd Iteration of loop starts:
4. The button gives the user new numbers
5. Random number generator gives the numbers 4 and 5 for the user to add
6. User enters 9 and hits continue button
7. The continue button overwrites the previous values for "1num" 2num and "ans" with 4, 5, and 9
So on so forth
I hope I am being clear enough on what my problem is.
Basically I think I need to include something in the loop that makes new variables to store the new values so it doesn't overwrite previous ones.
Oh boy that was fun to try to explain. If there is any confusion feel free to ask questions.
Thanks for any help!!!
***Oh Geez! Edit***
So after coding some, I realized that I can't even get my program to open the other forms correctly, I have:
#include "DoMath.h"
Form^ DoMath = gcnew Form();
DoMath->Show();
and that opens up a new form when I hit the button on my first form, but its just a blank form, its not my DoMath form. Uhg, I just googled this and tried like 10 different ways, and all of them either don't work or do the same exact thing my code does. It would be much appreciated if someone could give me the correct code for this.
modified on Sunday, March 15, 2009 4:14 AM
|
|
|
|
|
TabascoSauce wrote: The continue button overwrites the previous values for "1num" 2num and "ans" with 4, 5, and 9
Create a type which can store all these 3 values. Create a new instance of this type each time and keep it in a generic list. If you iterate over this list, you get all the answers user given. To explain what I said, create a class something like the below.
ref class QA{
public:
QA(int f, int s, int input)
: firstNumber(f)
, secondNumber(s)
, userInput(input)
{}
property int FirstNumber{
int get(){
return firstNumber;
}
}
property int SecondNumber{
int get(){
return secondNumber;
}
}
property int UserInput{
int get(){
return userInput;
}
}
private:
int firstNumber, secondNumber, userInput;
}; In the loop, create a QA instance and add it to a List(QA) .
using namespace System::Collections::Generic;
List<QA^>^ qaList = gcnew List<QA^>();
qaList->Add(gcnew QA(FirstRandomNumber,SecondRandomNumber,UserInput));
TabascoSauce wrote: Form^ DoMath = gcnew Form();
DoMath->Show();
and that opens up a new form when I hit the button on my first form
Looks like you are not clear with the object oriented concepts. The above code creates form instance each time it is executed. You need to make the form object as a class level variable if you don't want it to be created each time.
|
|
|
|
|
**Edit**
Oh wow after doing a ton of research and reading I got everything to work!
Thank you very much!
modified on Sunday, March 15, 2009 8:22 PM
|
|
|
|
|
Hi,
I'm new to C++ (and these forums), but I have some experience with Java, PHP and several other languages though.
I made a Windows forms application and would like to know an easy way to update the form controls of the mainform, from any cpp file, function, etc... in my project.
For example, a function (outside the form header file) should be able to update a textfield in the form, during its execution. What would be the best way to do this in C++?
In the main function, I tried this:
MainForm^ mainform = gcnew MainForm();
Application::Run(mainform);
mainform->txtUpdates->Text = "test";
But this doesn't work. Can anyone help?
Thanks in advance!
|
|
|
|
|
Lavake wrote: But this doesn't work.
Because controls will be declared as private by default. Changing it to public and exposing to outside of the class is not recommended.
Lavake wrote: For example, a function (outside the form header file) should be able to update a textfield in the form, during its execution. What would be the best way to do this in C++?
Best way is to write a delegate in your class. Let the form subscribe to it and update automatically when delegate is called. If you can't go with delegates, create a public method which updates the required controls on your form. Call this method from the outside function. Ofcource, you need to include the form's header file and your calling function should get an instance of the form.
|
|
|
|
|
Thanks for the help!
Ok so instead of declaring the control as public, I made a public method to update the control, but this doesn't really fix my problem...
MainForm^ mainform = gcnew MainForm();
Application::Run(mainform);
mainform->addUpdates("test");
The above code has the same effect as with mainform->txtUpdates->Text = "test";.
When I run my application no errors are reported, but the textbox "txtUpdates" isn't changed. I don't understand why not.
|
|
|
|
|
Lavake wrote: MainForm^ mainform = gcnew MainForm();
Application::Run(mainform);
mainform->addUpdates("test");
That's wrong. Application::Run blocks until the application exits. So mainform->addUpdates("test") will execute only when the application quits and you will not see the updates. For Application::Run , MSDN says,
The Dispose method of the Form class will be called prior to the return of this method.
Which means, mainform will be disposed before you call mainform->addUpdates("test") . To solve this, you need to rework on your design.
|
|
|
|
|
Hi,
I have a problem to pass some data between forms. Let's say I have 2 form, FormMain and FormChild, FormChild will be called from FormMain. What I want to do is simple. Whenever I change the textBox1's text in FormChild, the textBox1's text in FormMain will also contain the same text. After googling for a while, I got 2 solution : by using a delegate or directly instantiate FormMain in FormChild. Both solution unfortunately is in C#, so I need to convert it first. Well, I thought at first that there will be no problem at all to convert it, but that is just plain wrong. Both of the solution I get is just give me some error. Here is the converted source code
1.using delegate
in Form Main.h :
#include "Form Child.h"
namespace Delegates {
public delegate void AddTextChangeDelegate(String^ item);
public ref class FormMain : System::Windows::Forms::Form
{
public:
FormMain()
{
formChild = gcnew FormChild();
}
private:
FormChild^ formChild;
private:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
formChild->AddTextChangeCallback = gcnew AddTextChangeDelegate(&this->AddTextChangeCallbackFn);
formChild->Show();
}
private:
System::Void AddTextChangeCallbackFn(String^ str)
{
textBox1->Text = str;
}
};
}
in Form Child.h
namespace Delegates {
public ref class FormChild : System::Windows::Forms::Form
{
public:
AddTextChangeDelegate^ AddTextChangeCallback;
private:
System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e)
{
AddTextChangeCallback(textBox1->Text );
}
};
}
2. directly instantiate FormMain
the solution I get (in C#) is just simply declare FormMain as a global variable in FormChild, then exposed the textBox in FormMain as a public. But when I do the same thing in C++, the compiler give an error "error C2143: syntax error : missing ';' before '^' ". so probably I just need to include the Form Main.h in Form Child.h, which give me another error "fatal error C1014: too many include files : depth = 1024" (is it because both form include each other's header?, so that is like making some cyclic dependency thing? I'm not sure why). After googling once more, I found that it is still "possible" to declare FormMain instance in Form Child.h. Include Form Main.h in the Form Child.cpp and that's it.
in Form Child.cpp
#include "StdAfx.h"
#include "Form Child.h"
#include "Form Main.h"
System::Void FormChild::btnAdd_Click(System::Object^ sender, System::EventArgs^ e)
{
FormMain^ formMain = gcnew FormMain();
formMain->textBox1->Text = this->textBox1->Text;
}
But then another problem comes out. The instance declared in Form Child.cpp is not the same instance as the previous FormMain. So, what is the correct way to solve this problem? Is the methods mentioned above was wrong? Or perhaps I make a wrong conversion, misplaced the delegate or something? I have search the other solutions in the internet, but a lot of it is either in C# or VB. I could not find it for C++ .NET.
Also is there any way around to access the instance of FormMain from FormChild in the above case?
Thanks
|
|
|
|
|
roshihans wrote: But then another problem comes out. The instance declared in Form Child.cpp is not the same instance as the previous FormMain
Because you are instantiating a new instance in the child form. You need to pass the main form's instance to child form (through constructor injection or some setter methods).
roshihans wrote: So, what is the correct way to solve this problem?
I will stick with delegates. It is the best way to go.
roshihans wrote: AddTextChangeCallback(textBox1->Text );
Don't call it blindly, your delegate instance may be NULL and you will end with null reference exception. Do a sanity check before you call it.
System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e)
{
if(AddTextChangeCallback != nullptr)
AddTextChangeCallback(textBox1->Text );
} roshihans wrote: // declaring delegate, and get "error C2143: syntax error : missing ';' before '^' "
// the compiler seems doesn't recognize this
You might be doing it wrongly.
public ref class FormChild : System::Windows::Forms::Form
{
public:
delegate void AddTextChangeDelegate();
AddTextChangeDelegate^ AddTextChangeCallback;
...........
};
|
|
|
|
|
Thanks for your quick reply, Navaneeth!! And now, the delegate thing is works perfectly!! I has been searching a lot about this in the internet without a result and now it's solved! Thanks a lot Navaneeth!!
I dunno if my question is beyond the topic, but can I ask about another thing? this is about the answers that Navaneeth wrote
N a v a n e e t h wrote: Because you are instantiating a new instance in the child form. You need to pass the main form's instance to child form (through constructor injection or some setter methods).
constructor injection? I'm still a beginner in C++, so I'm barely hear about this. When you say constructor injection, is it something like dependency injection? I'm googling it in the internet, and this is that came up. Correct me if I was wrong, but from what I read in Internet, this dependency injection can be done using a container (??), created by third party developer (they mentioned something about pico container and others). It can not be done by .NET itself? I'm still confused
|
|
|
|
|
roshihans wrote: constructor injection?
By constructor injection, I meant to pass the first form's object to second form via constructor. Something like
Form2(Form1^ form1)
{
} As I said, go with delegates. It's the approaprite solution for your scenario.
|
|
|
|
|
N a v a n e e t h wrote: By constructor injection, I meant to pass the first form's object to second form via constructor. Something like
Form2(Form1^ form1)
{
}
But Form1 already include Form2.h (in my case Form1 act as a "parent" form, and instantiate Form2). So I think I can't declare Form2 (Form1^ form1) and keep the form1 object, like you have mentioned above, because I need to include Form1.h inside Form2.h, and that will give me this "fatal error C1014: too many include files : depth = 1024". Or maybe I don't need to include Form1.h inside Form2.h?
Thanks
|
|
|
|
|
roshihans wrote: But Form1 already include Form2.h (in my case Form1 act as a "parent" form, and instantiate Form2). So I think I can't declare Form2 (Form1^ form1) and keep the form1 object, like you have mentioned above
Fair point! You will get circular dependency problems. To solve this, you need to have good understanding about C++ compiling and linking.
You have to use forward declaration and declare parent form in child form's header file. Assume your parent form is named as PForm and child form as CForm . Here is how your PForm 's header file looks like
public ref class PForm : public System::Windows::Forms::Form{
public:
void UpdateText(String^ str)
{
textBox1->Text = str;
}
}; Here is your CForm 's header
ref class PForm;
public ref class CForm : public System::Windows::Forms::Form{
public:
CForm(PForm^ pf)
{
InitializeComponent();
pForm = pf;
}
private:
System::Void textBox1_TextChanged(System::Object^, System::EventArgs^);
PForm^ pForm;
}; CForm.cpp contains
#include "CForm.h"
#include "PForm.h"
System::Void CForm::textBox1_TextChanged(System::Object ^sender, System::EventArgs ^e){
if(pForm != nullptr)
pForm->UpdateText(textBox1->Text);
} You show the CForm from PForm like
CForm^ cForm = gcnew CForm(this);
cForm->Show(); This should work fine. But if you use delegates, you can avoid all these and implementation will be pretty straight forward. So please stick with delegates unless you have a good reason to go with the above method.
Hope it is clear now.
|
|
|
|
|
Thanks a lot Navaneeth, you have help me solve those two problems!! Pheww.. I kinda confused at first because of this circular dependency problems, but it's has clear now.
|
|
|
|
|
I've already posted this on the C# forum, but since I will be implimenting it in managed c++ soon after, I thought I would just run it by you folks also.
I have been building a GUI that interfaces with 3 services via remoted interfaces. I'm using GenuineChannels (http://www.genuinechannels.com) broadcast engine as the framework for event based communication between services & the GUI. Since you can only register one remoted object per AppDomain (I hope that statement is correct), I've had to create a seperate AppDomain for each UserControl in the GUI so more than one remoted object can be registered. Each UserControl contains a TreeView that needs to be updated via messages from the broadcast engine, or remoted events. Everything appears to be working just as I had hoped, but here is my problem. I've tried using different types of delegates at different scopes within the code & also tried refactoring my code, but I cannot seem to come up with a way to update the TreeView instance within the UserControl from a remoted object in a seperate AppDomain. Here's most of the code from the classes involved. Unimportant peices have been removed.
namespace Console
{
public partial class ControlDelegate : UserControl
{
public ControlDelegate()
{
InitializeComponent();
// Create appdomainsetup information for the new appdomain.
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = System.Environment.CurrentDirectory;
setup.ApplicationName = ("Delegate-" + AppDomain.CurrentDomain.FriendlyName);
//Create evidence for new appdomain.
Evidence evidence = AppDomain.CurrentDomain.Evidence;
// Create the application domain.
AppDomain domain = AppDomain.CreateDomain(("Delegate-" + AppDomain.CurrentDomain.FriendlyName), evidence, setup);
DelegateMessenger remote = (DelegateMessenger)domain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, Assembly.GetCallingAssembly().GetName().Name + ".DelegateMessenger");
Thread thread = new Thread(new ThreadStart(remote.Construct));
thread.Start();
}
/****************************/
this method must be static to refer to it, which is my problem.
/****************************/
public static void tViewDelegateHeartBeat(Objects.Message message, String nickname)
{
System.Console.WriteLine(message.action);
/****************************/
The line below is part of the update to TreeView instance, but it's
the root of my problem right now.
/****************************/
//tViewDelegate.Nodes.Add(message.action + " " + nickname);
}
}
public class DelegateMessenger : MarshalByRefObject, IDelegateMessenger
{
public static String Nickname = "Delegate-" + Assembly.GetExecutingAssembly().GetName().Name;
public static DelegateMessenger Instance = new DelegateMessenger();
public static IDelegateChat iDelegateChat;
public static IDelegateServer iDelegateServer;
public static Object IDelegateChatLock = new Object();
public void Construct()
{
try
{
// Setup remoting with assembly objects as remote object.
IDictionary props = new Hashtable();
props["name"] = "gtcpd";
props["prefix"] = "gtcpd";
props["priority"] = "100";
props["port"] = "0";
GenuineTcpChannel channel = new GenuineTcpChannel(props, null, null);
ChannelServices.RegisterChannel(channel, false);
WellKnownClientTypeEntry remotetype = new WellKnownClientTypeEntry(typeof(SharedChannels.GenuineTcp.GenuineTcpChannel), "gtcpd://127.0.0.1:48886/DelegateMessenger");
RemotingConfiguration.RegisterWellKnownClientType(remotetype);
// Bind client's receiver.
RemotingServices.Marshal(Instance, "DelegateMessenger");
// Subscribe to the chat event.
lock (IDelegateChatLock)
{
iDelegateServer = (IDelegateServer)Activator.GetObject(typeof(IDelegateChat), "gtcpd://127.0.0.1:48886/DelegateServer");
iDelegateChat = iDelegateServer.JoinDialog(Nickname);
}
}
catch (Exception ex)
{
MessageBox.Show(Environment.NewLine + String.Format("Exception: {0}. Stack trace: {1}.", ex.Message, ex.StackTrace));
}
}
public Object ReceiveMessage(Objects.Message message, String nickname)
{
/*****************************/
this works fine if the receiving function is static, but I
need to refer to an instance method to be able to update
the UserControl TreeView (tViewDelegate).
/*****************************/
AppDomain.CurrentDomain.DoCallBack(delegate { ControlDelegate.tViewDelegateHeartBeat(message, nickname); });
/*****************************/
this works if I register only one remoted object within
the GUI AppDomain, which won't work with my current design
since I need to access several remoted objects from other
UserControls within the GUI.
/*****************************/
// http://blogs.msdn.com/csharpfaq/archive/2004/03/17/91685.aspx
/* if (tViewDelegate.IsHandleCreated)
{
try
{
tViewDelegate.Invoke(new tViewDelegateCallback(this.tViewDelegateHeartBeat), new Object[] { message, nickname });
}
catch (Exception ex)
{
MessageBox.Show(Environment.NewLine + String.Format("Exception: {0}. Stack trace: {1}.", ex.Message, ex.StackTrace));
}
}
*/
return null;
}
public override Object InitializeLifetimeService()
{
return null;
}
}
}
I'm hoping someone sees something silly that I'm doing and can point it out. The project is still in its infancy, so I could refactor everything if need be. Any ideas?
|
|
|
|
|
Hi, i have lots of textbox on my form, instead of adding several eventhandlers for textchanged or keypressed, i wrote a single one for all. It worked then i decided to move it to another class, but i can't bind it to eventhandler. I don't know what i did wrong. Can anyone help me please?
//If i put the code on form's class it works
this->textBox7->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &AA::SM_KeyPress);
private: System::Void SM_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) { ...}
//But when i try to do this, i got several errors
//1>error C2276: '&' : illegal operation on bound member function expression
//1>error C3350: 'System::Windows::Forms::KeyPressEventHandler' : a delegate constructor expects 2 argument(s)
this->textBox9->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &AA::G1->SM_KeyPress);
G1 is an another class, i added these lines to use library.
#include "Genel.h"
using namespace Util;
private : Util::Genel^ G1;
Thank you very much...
|
|
|
|
|
I recreated your situation
Make sure you crete an instace that points to the class thats going to handle the event in this case HA
HandleAccessor^ HA;
this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(HA,
&HandleAccessor::textBox2_KeyPress);
a programmer traped in a thugs body
|
|
|
|
|
It works!!! Thank you very much...
|
|
|
|
|
Thanks for looking at my question! I am a fairly new programmer, so please bear with me.
I am trying to make a program that takes the user-inputted values from 3 text boxes, adds them together, then displays the total as a label that I placed next to the boxes in real time-(as in the number updates automatically every time the number in one of the text boxes is changed). Also, how do I make sure the value that is inputted is only numbers?
Ok, here is what I have got so far. Obviously it doesn't work because I am asking this question.
north1, north2, and north3 are the three text boxes and northT is the label next to them. The rest is pretty easy to understand from the code.
private: System::Void northT_Click(System::Object^ sender, System::EventArgs^ e)
{
double nTotal = 0.0;
double n1 = 0.0;
double n2 = 0.0;
double n3 = 0.0;
do
{
n1 = Convert::ToDouble(north1->Text);
n2 = Convert::ToDouble(north2->Text);
n3 = Convert::ToDouble(north3->Text);
nTotal = n1 + n2 + n3;
northT->Text = nTotal.ToString();
} while(nTotal > 0);
}
Again, thanks for looking, and any help will be greatly appreciated.
|
|
|
|
|