Defining Non-Scoped Text At Run-time C++ - c++

Is there an option of defining a text and using it later not as a string or anything, just as a part of a function but being able to redefine it in the middle of the program (the definition to not take place in the preprocessor, but runtime)? For example I have the following code in C++ Windows Forms:
private: System::Void ps1_GotFocus(System::Object^ sender, System::EventArgs^ e)
{
if(this->ps1->Text == L"/ Your text here /") this->ps1->Text = L"";
this->ps1->ForeColor = System::Drawing::Color::FromName( "Black" );
}
private: System::Void ps2_GotFocus(System::Object^ sender, System::EventArgs^ e)
{
if(this->ps1->Text == L"/ Your text here /") this->ps1->Text = L"";
this->ps2->ForeColor = System::Drawing::Color::FromName( "Black" );
}
where ps1 and ps2 are TextBoxes and I'm using it to display a gray 'Your text here' string and when clicked in the TextBox ready for input (when the TB GotFocus) to clear the text and make the input black. Having in mind that I have 9 TextBoxes like that, is it possible to make all this with less code? I tried the same code with #define ps ps1 and a global ps_GetFocus() method outside everything that uses that ps, but as you know the #defines are done in the preprocessor and the last define (ps ps9) is defined even before the program is started.
Is there a way of defining non-scoped text at run-time?

Just have a common ps_GotFocus function for all your text boxes, and use sender (you'll have to cast it to the appropriate type first, not sure how to do that in .Net C++ with that weird ^ thingie, maybe dynamic_cast will work?) instead of the various ps objects.
Something along the lines of:
private: System::Void ps_GotFocus(System::Object^ sender, System::EventArgs^ e)
{
TypeForYourTextBox^ the_sender = dynamic_cast<TypeForYourTextBox^>(sender);
// I'm unsure about the previous line but you get the idea
// You may also want to check that the cast succeeded, ie. the_sender is not null
if (the_sender->Text == L"/ Your text here /") the_sender->Text = L"";
the_sender->ForeColor = System::Drawing::Color::FromName("Black");
}

Related

Past text in rich text box Visual C++ without formating

I'm working on my project, i'm making notepad for my self. At the moment i'm in final state(fixing bugs and adding some final things).Problem that i'm facing now is: when i paste formated text it stays formated, i want it unformated, default font, default size.I'm working in Microsoft Visual 2010 C++.
Code that i'm using for paste:
private: System::Void pasteToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
richTextBox1->Paste();
}
The Paste method has this overload, that takes a parameter specifying the format of the data pasted. Specify the format you want, such as DataFormats::Text or DataFormats::UnicodeText. For example:
// Get the format for the object type.
DataFormats::Format^ myFormat = DataFormats::GetFormat( DataFormats::Text );
// After verifying that the data can be pasted, paste it.
if ( richTextBox1->CanPaste( myFormat ) )
{
richTextBox1->Paste( myFormat );
return true;
}
else
{
MessageBox::Show( "The data format that you attempted to paste is not supported by this control." );
return false;
}

importing a picture from an openfiledialog into a picturebox, in visual c++

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.

How do I replace a certain line on a text file with different text?

I'm new to C++ and CLI/C++ and I'm having a problem. I would like to replace the line in a textfile with different text of my choosing. For example, I have a text file with 12 lines and I want to replace the 6th line, that says "Line 6" with the text "Line 6 edited".
Now, I've only started learning about handling text files 1 day ago and I have figured out how to insert lines, so all the code I have at the moment is this:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
char txtfile[]="C:\\Users\\acer\\Desktop\\aaa.txt";
StreamWriter ^writer = gcnew StreamWriter(gcnew String(txtfile),true);
writer->WriteLine("Line 1");
writer->WriteLine("Line 2");
writer->Close();
delete writer;
}
From what I researched, I found out I have 2 methods of doing this. I could use a temp file or I can load all the lines into an array and store it in memory if the file is not too big. And the File is 1KByte so I would like to use this 2nd method of storing it in memory. Now, my problem is that since I'm new to this language I have no idea how to actually do this. And I have been searching to see if I can find instructions on how to do
this in CLI C++ but I can't find anything. I've been looking at this topic here--> Delete specific line from a text file?
But's it's in C# and I don't know how to convert the code to CLI C++.
EDIT : I have seen this piece of C# code from that other topic that shows how to delete a line and I would like to try it
var file = new List<string>(System.IO.File.ReadAllLines("C:\\path"));
file.RemoveAt(12);
File.WriteAllLines("C:\\path", file.ToArray());
I tried to convert to something like this in CLI/C++:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
char txtfile[]="C:\\Users\\acer\\Desktop\\aaa.txt";
array<String^>^ fileArray = System::IO::File->ReadAllLines(txtfile);
fileArray->Remove(6);
File->WriteAllLines(txtfile, fileArray->ToArray());
}
But I had no success because I don't know the correct syntax for CLI/C++ since I'm newbie. How can I use that C# code in CLI C++? I really appreciate the instructions but I need code examples in CLI/C++.
You seem to be confusing static and non-static methods:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::String^ txtfile = L"C:\\Users\\acer\\Desktop\\aaa.txt";
array<System::String^>^ lineArray = System::IO::File::ReadAllLines(txtfile);
System::Collections::Generic::List<System::String^>^ lineList;
lineList = gcnew System::Collections::Generic::List<System::String^>(lineArray);
lineList->RemoveAt(6);
System::IO::File::WriteAllLines(txtfile, fileList->ToArray());
}

Referencing TabControls from another Form C++/CLI

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.

How to display string in a listbox using C++?

I am trying to extract the content of a .txt file and then display it in a listbox, my developing environment is VC++ 2010.
listBox1,button1 are elements of my Windows form appliaction.
This is what i got so far:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
ifstream ss_dfs_output("C:\\Users\\...\\Sampletext.txt");
string TempStoreToDisp;
vector<string> VecToDisp;
vector<string>::iterator ToDisp_ptr;
string OutToDisp;
while (getline(ss_dfs_output,TempStoreToDisp,'\n'))
{
VecToDisp.push_back(TempStoreToDisp);
}
for (ToDisp_ptr=VecToDisp.begin();ToDisp_ptr!=VecToDisp.end();ToDisp_ptr++)
{
OutToDisp = *ToDisp_ptr;
String ^sss = Convert::ToString(OutToDisp.c_str());
this->listBox1->Items->Insert(0,sss);
}
}
The compiling was successfull but the output seems strange. Seems sss returns "true" but shoudn't it be a string?
How about
this->listBox1->Items->Add(gcnew String(OutToDisp.c_str())); // you can still Insert if you want
MSDN SByte* constructor reference