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
Related
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;
}
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;
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'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());
}
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");
}