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.
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).
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.
Im trying to make one simple program, that list few names in ListBox, and when selecting one of them, and clicking on one button should be load the data in few TextBoxes... All i need is the ID of the selected item of that ListBox because the data is in array and i can't get the info without that id.
So here is my code:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->listBox1->Items->Clear();
if (sCount != 0) {
for (int i = 1; i <= sCount; i++) {
String^ entry = gcnew System::String(s[i].Show().c_str());
this->listBox1->Items->Add(entry); //Listing the items from the array: s
}
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
s[++sCount].InsertStudent("Name",270,50); //This is how i'm adding items in the array
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
int i = this->listBox1->SelectedItem; //Trying to get the ListBox Item ID
String^ entry = gcnew System::String(s[i].Show().c_str()); //Getting the Item from the array
this->textBox1->Text = entry; //Placing the array item into the textBox1
}
P.S. I want to do it in that way, because the array have class and there i put more than one items in one ID, but in the ListBox is listed only one of them.
Can someone help? Thanks in advance :-)
I found the answer myself :)
Here is what i search, if someone come to this post:
int i = this->listBox1->SelectedIndex;
Edit:
randomly searching through other languages i found the correct answer if anyone needs:
"update DataBase_Name set Column= '"+value+"' where ID='"+value+"';"
I need to insert data into a column and couldn't find much info about it on the web.
the code is
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (this->listBox1->SelectedIndex!=-1)
{
String^ constring = L"datasource=localhost;port=3306;username=root;password=root;";
MySqlConnection^ conDataBase = gcnew MySqlConnection(constring);
MySqlCommand^ cmdDataBase =
gcnew MySqlCommand(insert into products_shop.productname where idProductName='"+this-listBox1->SelectedItem->ToString()+"' ('ProductX','ProductY') values('"+this->textBox2-Text+","+this->textBox3->Text+"');",conDataBase);
MySqlDataReader^ MyReader;
try
{
conDataBase->Open();
MyReader = cmdDataBase->ExecuteReader();
}
catch(Exception^ex)
{
MessageBox::Show(ex->Message);
}
}
else
{
MessageBox::Show(L"Select an item please");
}
}
insert into products_shop.productname where idProductName='"+this->listBox1-SelectedItem-ToString()+"' ('ProductX','ProductY') values('"+this->textBox2-Text+","+this->textBox3-Text+"');
this line is just my randm guess, any help on how the syntax should actually be? or if it works somehow different..
thanks for help
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.