Visual C++ - Call a function from a button click method - visual-studio

I created a Visual C++ project, and made a button in the form. Visual Studio generates this method for a button click event:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
}
This code is in the Form1.h file, no problems for now. However, if I try to call a function, in the event method, using other classes, like std::cout (doesn't matter if I define it in the Form1.h file or I #include it from another file), the compiler gives me an error (C2079).
Why does this happen? Please be patient as I am a total noob in Windows GUI programming, thanks in advance to whoever can help.

Well, there's an explanation that's part of C2079. It's something like 'identifier' uses undefined class/struct/union 'name'. This is NOT related to either Windows or GUI programming. The problem is simply that you must have defined the name you're using.
For instance, std::cout is defined in <iostream> which you probably did not include. BTW, exactly where did you expect std::cout to print to? A normal GUI doesn't have a text console.

Related

How to open a file with the default CSV editor in Windows Forms?

I have this backup program I am working on that uses Windows Forms for the design.
All is working well except for one button so I will spare the entire code.
I have one Button that allows the user to open a CSV file from a certain location to edit in their default CSV editor program (i.e., Excel, Libre etc.) and the Button works fine as this:
private: System::Void button17_Click(System::Object^ sender, System::EventArgs^ e) {
String^ CSVLoc = textBox1->Text;
string CSVLoc1 = msclr::interop::marshal_as<std::string>(CSVLoc);
system(("start " + CSVLoc1 + "\\SystemIPs.csv").c_str());
}
However, I would like to avoid using the system function as I do not like that it flashes a command window when launching the default editor program.
I have tried:
private: System::Void button17_Click(System::Object^ sender, System::EventArgs^ e) {
String^ CSVLoc = textBox1->Text;
string CSVLoc1 = msclr::interop::marshal_as<std::string>(CSVLoc);
WinExec(("start " + CSVLoc1 + "\\SystemIPs.csv").c_str(), SW_HIDE);
}
but that does nothing when I click the button.
WinExec worked as a replacement for my other System function commands but not here.
The function of the button is not to do any editing from my program so the need to read or write to it is not necessary. Just need it to open in the default editor without a command window popup.
I've searched for hours to try to find a solution with no luck.
I am open to any suggestion here even if it is using the system function but something niffy way to hide that I am not aware of at this time.
The program is designed for Windows only and set to run as a Windows Application. Using CLR and .Net is 4.7.2. As I mention everything works well, just trying to find a way to hide that pesky command window popup.
As Jimi said, you could use Process.Start Method to open a csv file with the default editor.
First, please add namespace reference.
using namespace System::Diagnostics;
Second, you could try the following code to open it.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ str = textBox1->Text;
Process::Start(str);
}

wxWidgets event focus textcontrol

I have another wxWidgets question regarding events and focus.
I have already looked at the tutorials and this old question here but I am still running into problems C++ Event (Focus) Handling
Basically I have a dialog with two wxTextCtrl elements and a Button.
What I would like to achieve is, that when I click on button it needs to tell me which of the two elements previously had the focus.
In the constructor of my Dialog I created all the elements and then connected them to the eventhandler like this: Ttop->Connect(TOP,wxEVT_KILL_FOCUS,(wxObjectEventFunction)&UI_ADDENTRY::hasfocus);
Tbottom->Connect(BOTTOM,wxEVT_KILL_FOCUS,(wxObjectEventFunction)&UI_ADDENTRY::hasfocus);
then there is the eventhandler that safes the id into focus
void UI_ADDENTRY::hasfocus(wxFocusEvent& event){
focus= event.GetId();
event.Skip();}
however when i try to access focus in the Button function it always tells me: 0 instead of TOP or BOTTOM / the ids that I gave the textcontrols
void UI_ADDENTRY::OnRecord(wxCommandEvent &event){
wxString tmp;
tmp << this->focus;
wxMessageBox(tmp);}
What am I doing wrong? is there another way of finding out which of the two textbox has been in focus last?
Thank you
The most fool proof way is to catch EVT_SET_FOCUS in your text controls and remember the last one that received it. This is not more difficult than what you are doing but should work without problems.
FWIW EVT_KILL_FOCUS can't, unfortunately, be consistently implemented on all platforms, in particular GTK+ doesn't give any information about the window focus is being lost to.
In think u mean event.GetWindow().GetId(). Though I'm not sure how ur casting from int to string.

Visual Studio C++ How to get the Form not freezing while calling a time-consuming function?

I am making a C++/CLI Forms application.
In the main window of my app I have a button. When I click that button I call the Load function. Below there is the C++/CLI code:
private: System::Void Button1_Click(System::Object^ sender, System::EventArgs^ e) {
Load();
}
The function Load() is a time-consuming function. It uses the cURL library to send several HTTP GET request to a website.
In the Form I also included a ProgressBar and a textLabel showing the current request being sended.
The problem is that when I click the button and call the function the Form just freezes. I can't see the progressBar and Textlabel changing it's value while the function Load() is called, the Form is just freezed. When the function Load() has finished sending request, suddenly the progressBar change It's value to 100%.
I hope I described my problem clearly enough to understand it.
Move your task to another thread, or call Application.DoEvents();, just after you updating your scrollbar value.
Either break the task into smaller parts (design a finite state machine or use continuations) or use a separate thread.
The first approach takes more getting used to, but it's easier for an experienced programmer to get right. Threading requires synchronization which is very detail-oriented and causes a lot of hidden sporadic bugs which are extremely difficult to debug.
Call Form1.Refresh() every time you update an element of the form (say Form1). It will show the results immediately.
Before any line command that make probably any load time ...Write This:
System::Windows::Forms::Application::DoEvents();

How to provide Input to Dialogs designed by Qt Designer

I am a Qt beginner and working with Qt Designer to develop some small UI elements. I read http://doc.trolltech.com/4.5/designer-using-a-ui-file.html to use these GUI elements in my code and using multiple inheritance approach.
I am introducing bookmark feature which somewhat look like http://img293.imageshack.us/img293/3041/screenshotyb.png. Now the problem I am facing is How can I show all existing bookmark folders in the drop down(say folders are in a QVector). So my main problem is how can I pass some inputs to the UI element.
I think I'm clear, please let me know if further explanation is required. Sorry for adding links directly, rich formatting in my browser is not working.
EDIT :
As some suggested, I have to go via code, but in that case is it possible that create all other components like textEdit, labels, buttons and add combobox using code. Because I have already developed code for bookmarks and adding folder feature in already existing thing.
Thanks For suggestions.
Finally I came up with the solution. I was using multiple inheritance implementation of UI file generated by QT Designer. So solution look like :
Dialog.ui will be UI file generated by QtDesigner
//bookmarDialog.h
#include "ui_Dialog.h"
class BookmarkDialog : public QWidget, private Ui::Dialog
{
Q_OBJECT
public:
BookmarkDialog (QWidget *parent = 0);
}
//bookmarkDialog.cpp
#include "bookmarkDialog.h"
BookmarkDialog::BookmarkDialog()
: QWidget(parent)
{
setupUi(this);
QList folders = getAllFolders();
comboBox->insertItems(0,folders);//comboBox is defined in UI file
}
With Qt Designer, you can add items to a combo box (double-click on the combobox to show up the editor). But if your folder list will vary, you'll have to do it by code.
Have a look to QCombobox documentation (Qt doc is really good).
How are your storing your folders in the vector ? As strings ?
Il your QVector is containing strings, you can easily convert it into a QStringList and use it to populate your combobox :
QVector<QString> FolderList;
myComboBox->addItems(FolderList.toList());
Then, you can connect the signal currentIndexChanged(const QString&) of your QComboBox to a slot to do something when the folder has changed.
I think you have to do it in code. You can fill the combo box in the designer as soon as you are using static values. It is something you are doing dynamically like obtaining the bookmarks folders then you have to do it in your business logic code.
Maybe QtDesigner has been improved since the last time I used and now is possible to do complex things like that, but even in that case, from my experience, I would recommend you not to depend so much of QtDesigner. If you want to do complicated things is faster to do it in code and you will commit less mistakes and will have more control over what you are doing.
You can set the values to the combo box like this:
Suppose the vector contains the folder names as strings and is called folders.
for (int i = 0; i < folders.count(); i++)
{
comboBox.addItem(folders.at(i));
}
If this is not what you are looking for give me a comment and I will try to help.

How to add a file selection dialog using QT Createor?

As an old Borland C++ Bulder coder who has moved to Linux, I was very pleased to find QT and QT Creator.
But I have fallen at the first hurdle: I have designed a form, with some controls, and added a menu. Now, when the user selects menu File/Open, I would like to display a file selection dialog- and I can't see how.
It's obviously a simple problem, so if someone could point me right, I would be grateful.
include the QFileDialog
#include <QFileDialog>
then on any method you can write something like this
QString path = QFileDialog::getExistingDirectory (this, tr("Directory"), directory.path());
if ( path.isNull() == false )
{
directory.setPath(path);
}
for more information see this

Resources