I am trying to convert this tabbed browser into C++ from visual basic.
I am trying to reference the Tab Control from Form1.h.
Here is the code on Form1.h:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
String^ title = String::Concat("TabPage ",(tabControl1->TabCount + 1).ToString());
tab^ newtab = gcnew tab;
newtab->Show();
newtab->TopLevel = false;
newtab->Dock = System::Windows::Forms::DockStyle::Fill;
TabPage^ myTabPage = gcnew TabPage(title);
myTabPage->Controls->Add(newtab);
tabControl1->TabPages->Add(myTabPage);
}
The code on the second form that is trying to create another tab is this:
private: System::Void newTabToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
tab^ newtab = gcnew tab;
newtab->Show();
newtab->TopLevel = false;
newtab->Dock = System::Windows::Forms::DockStyle::Fill;
TabPage^ myTabPage = gcnew TabPage();
myTabPage->Controls->Add(newtab);
tabControl1->TabPages->Add(myTabPage);
}
In visual basic all that is required is to add Form1. to the beginning like so...:
//Original
tabControl1.TabPages.Add(myTabPage);
//New
Form1.tabControl1.TabPages.Add(myTabPage);
How could I do this same thing in C++?
Visual Basic provides a default instance of each class in your project. When you say Form1.tabControl1, you're actually getting a particular global instance of Form1, and accessing the tabControl1 field on that.
Add a way to send the instance of Form1 to the second form, and use that instead of Form1. Something simple like passing the instance of Form1 to the second form in its constructor will probably do the trick.
Related
I have two separate forms list_of_students_page and testing.
I have a button in list_of_students_page which on clicking opens testing form. I have created a button in testing form to move back to list_of_students_page.
Upon running the button in testing form, it works when it is clicked twice.
I am using C++/CLI Windows Form in Visual Studio.
list_of_students_page.h
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
CppCLR_WinformsProjekt1::testing^ testing_f = gcnew CppCLR_WinformsProjekt1::testing;
this->Hide();
testing_f->ShowDialog();
if (testing_f->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
this->Show();
}
}
testing.h
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->DialogResult = System::Windows::Forms::DialogResult::OK;
this->Close();
}
Why is this happening and how should I fix this?
You have:
testing_f->ShowDialog();
if (testing_f->ShowDialog() == System::Windows::Forms::DialogResult::OK)
Having the standalone ShowDialog call doesn't really make sense and does explain why you have to hit the button twice (it's actually different instances of the dialog, it just appears so quickly you can't see that).
I added a new row and updated the datatable. The record was inserted into the table. But when I filled with the same datatable again, the result is an empty datatable. Am I missing something? Did I do something wrong? I just learned about using database in .net recently and I don't know what to do with this problem.
MySqlConnection ^connection;
DataTable ^table1;
MySqlDataAdapter ^adapter1;
MySqlCommandBuilder ^cmb;
private: System::Void opentable(String ^sql){
adapter1 = gcnew MySqlDataAdapter(sql,connection);
cmb = gcnew MySqlCommandBuilder(adapter1);
table1 = gcnew DataTable;
adapter1->Fill(table1);
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
connection = gcnew MySqlConnection("XXXXXXXXXX");
connection->Open();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
opentable("SELECT * FROM ms_per WHERE PerCode='001'");
if(table1->Rows->Count==0){ //table1->Rows->Count is 0
DataRow ^newrow = table1->NewRow();
newrow["PerCode"] = "001";
table1->Rows->Add(newrow);
}
adapter1->Update(table1);
opentable("SELECT * FROM ms_perhist");
dataGridView1->DataSource=table1;
//ms_perhist is not an empty table, but after this the DataTable is empty.
I am seeing if I can wrap these lines into a single procedure so I can use it with other tables/queries:
adapter1 = gcnew MySqlDataAdapter(sql,connection);
cmb = gcnew MySqlCommandBuilder(adapter1);
table1 = gcnew DataTable;
adapter1->Fill(table1);
Note 1: I tried the following line. Data in ms_perhist shows up correctly, so the newrow-update lines may have something to do with the problem.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
opentable("SELECT * FROM ms_per WHERE PerCode='001'");
opentable("SELECT * FROM ms_perhist");
dataGridView1->DataSource=table1;
}
I have tested your code in c# and it's working fine. May be C++ creating new datable instance when you are executing the following line
table1 = gcnew DataTable;
So don't use that line twice in your code. But I am not sure about C++
It appears I was using an outdated MySQL.Data.dll. I checked the modified date and it was from 2008.
I changed the MySQL Connector version to the most recent version and the problem is fixed.
When debugging,I found my program have stopped after
myServer->Invoke(myServer->myShowMessage);
I tried to find where it goes,and I set several breaks but I didn't find where it was going.However,I also set a break on ShowMessageMethod where it should go but I am sure it wasn't going to there.It was just look like dealing with other things and trapping in it.I suspect there was something wrong with the UI thread,but I have little knowledge about the underlying in Windows Message. I have create some other threads in main thread, is that makes things all different? I just followed the Invoke method code on msdn. And Here are my codes:
Server::Server(){
InitializeComponent();
myShowMessage = gcnew ShowMessageDelegate(this,&Server::ShowMessageMethod);
}
System::Void Server::start_Click(System::Object^ sender, System::EventArgs^ e){
Thread^ myThread = gcnew Thread(gcnew ThreadStart(this,&Server::ListenThreadFunc));
myThread->Start();
}
void Server::ShowMessageMethod(){
String^ message = "Get Successful";
this->APTBX->AppendText(message);
}
void Server::ListenThreadFunc(){
ServerListen^ mySL = gcnew ServerListen(this);
mySL->ListenThread();
}
ServerListen::ServerListen(Server^ _s){
myServer = _s;
}
void ServerListen::ListenThread(){
array<Object^>^myStringArray = { convert.toStringDelegate(information) };
Object^ re = myServer->Invoke(myServer->myShowMessage);
}
thanks in advance.
M stuck in importing a picture from an openfiledialog into a picturebox, in visual c++ windows form at Visual studio 2012... i searched it on different forums, and found one possible solution:-
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.Title = "Select a picture";
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
if (op.ShowDialog() == true)
{
imgPhoto.Source = new BitmapImage(new Uri(op.FileName));
}
}
But in this solution or other solutions close to this one, does not allow me to create
" new OpenFileDialog(); "
another solution, offered by microsoft for cursor file was...
private:
System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
// Displays an OpenFileDialog so the user can select a Cursor.
OpenFileDialog * openFileDialog1 = new OpenFileDialog();
openFileDialog1->Filter = "Cursor Files|*.cur";
openFileDialog1->Title = "Select a Cursor File";
// Show the Dialog.
// If the user clicked OK in the dialog and
// a .CUR file was selected, open it.
if (openFileDialog1->ShowDialog() == DialogResult::OK)
{
// Assign the cursor in the Stream to
// the Form's Cursor property.
this->Cursor = new
System::Windows::Forms::Cursor(
openFileDialog1->OpenFile());
}
}
same problem in this too....
can any one suggest the easiest approach to do the required task
In your first example, you are trying to use C# sintax. In C++/CLI you won't use . but the ->, neither new or * for handles. You actually need gcnew and ^.
Apart from this, look at this simple code:
Note: To be sure this example will compile, create a new project called TestImage, add a button called btnLoad and a picturebox called pbImage.
In c++ it is better to separate your header file (.h ) from your c++ file ( .cpp ). In your header file, declare just the prototype of the click event:
private: System::Void btnLoad_Click(System::Object^ sender, System::EventArgs^ e);
In your .cpp file, you should have something like this:
#include "stdafx.h"
#include "Form1.h"
using namespace TestImage;
System::Void Form1::btnLoad_Click(System::Object^ sender, System::EventArgs^ e) {
OpenFileDialog^ ofd = gcnew OpenFileDialog();
//insert here the filter if you want
//ofd->Filter..
if (ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
pbImage->Image = Image::FromFile(ofd->FileName);
}
}
Remember to resize accordingly to your image pbImage and you will be fine.
Hope this one helps.
I am developing a windows form program and having a hard time finding how to do this in C++. The MSDN has this page, http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image, but the C++ documentation is lacking compared to the VB.
This is what I have so far. This method is supposed to avoid the common flickering issues but I am not sure where to go from there since I need it to go back to the original image after the mouse leaves.
void InitializeComponent(void)
{
this->btnExit->BackColor = System::Drawing::Color::Transparent;
this->btnExit->BackgroundImageLayout = System::Windows::Forms::ImageLayout::None;
this->btnExit->Image = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"btnExit.Image")));
this->btnExit->Location = System::Drawing::Point(764, 4);
this->btnExit->Name = L"btnExit";
this->btnExit->Size = System::Drawing::Size(30, 20);
this->btnExit->TabIndex = 3;
this->btnExit->TabStop = false;
this->btnExit->Click += gcnew System::EventHandler(this, &mainForm::btnExit_Click);
}
#pragma endregion
private: System::Void btnExit_OnMouseEnter(System::Object^ sender, System::EventArgs^ e) {
Image^ get ();
void set (Image^ value);
}
Thanks.
private: System::Void btnExit_MouseEnter(System::Object^ sender, System::EventArgs^ e) {
btnExit->Image = Image::FromFile("C:\\Users\\...\\image.png");
}
Works, not sure if it is the proper way to do it.