Masked Textbox data entry

I didn't see anything in forums posted on this yet. My goal is to read data from a Stream^ object in varying lengths of bytes, display the data in user-editable textboxes, then finally be able to save that data back to the Stream^ (which will of course be written back to the file after synchronizing). I'm using C++/.Net to do this in VSExpress 2005. Thanks to Faldrax, I understand how to get the datastream to be accessible to the whole form now without errors. My trouble now is with line 65. I get a C2664 error for offending the compiler's array with my request for a string. I can't seem to get around the textbox (either masked or not) inherent property of String^, yet the data being read is in array^ format. I've tried using both the regular textbox and the masked textbox, but both fail in my implementation of them. For decimal values, the masks are set for "###", and the one masked box that takes letter or numbers is masked "CCCCCCC". Even using Convert::ToString() doesn't work for me. Is there a different way to read the data from the stream? Or at least an easier way to get the types to be compatible while sticking with the .NET? Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Stream^ GameStream;
		//cli::array<char,1>^ GameArray = gcnew array;

		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  WriteButton;
...
...
protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->WriteButton = (gcnew System::Windows::Forms::Button());
...
...
private: System::Void openToolStripMenuItem_Click(System::Object^ /*sender*/, System::EventArgs^ /*e*/) {
			
			OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
			openFileDialog1->InitialDirectory = "C:\\Documents and Settings\\Anon\\Desktop";
			openFileDialog1->Filter = "All files (*.*)|*.*|gs* files (*.gs*)|*.gs*";
			openFileDialog1->FilterIndex = 2;
			openFileDialog1->RestoreDirectory = true;
			openFileDialog1->FileMode::Open;
			

			if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
				{
					if ( (GameStream = openFileDialog1->OpenFile()) != nullptr )
						{
							//Open file operations
							BinaryReader^ reader = gcnew BinaryReader(GameStream);
							BinaryWriter^ writer = gcnew BinaryWriter(GameStream);
							reader->Read(Form1::NameBox->Text,pCharName,7);
Hi,
my thoughts would be to have an array to read to/from for the reader/writer, and then to convert from this array to / from string^.
1
2
3
4
5
6
Const int MaxChars = 100;   //Chose the max value you will want to read or write here
BinaryReader^ reader = gcnew BinaryReader(GameStream);
BinaryWriter^ writer = gcnew BinaryWriter(GameStream);
array<Char>^ buf = gcnew array<Char>(MaxChars>;
reader->Read(buf,0,7);
//Now convert to String^ and put into your textbox 

Note that the Read method of the BinaryReader has the form
Read(Dest, Start, Count)
where Dest is the array, Start is the position in the ARRAY to PUT the data read, and Count is the number of chars to read, so you probably always want that second parameter to be zero.
Hello Again Faldrax!

I tried your suggestion, but I get an error upon trying to compile. error C2664 (or C2440): 'int System::IO::BinaryReader::Read(cli::array<Type,dimension> ^,int,int)' : cannot convert parameter 1 from 'cli::array<Type> ^' to 'cli::array<Type,dimension> ^'
I tried jumbling things around all things I tried resulted in the same error or 'cannot convert from 'cli::array<Type,dimension> ^' to 'cli::array<Type,dimension> ^'. WEIRD!! Is my IDE buggy??
Last edited on
sorry, forgot to post code. I want to declare the array in line 5 so that it will be accessible throughout the form so that eventually I can make an accessible function out of the array for read/write operations with parameters of offset in FileStream, count of bytes to read/write to that stream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Stream^ GameStream;
		static array<wchar_t>^ GameArray = gcnew array<char>(10);

		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  WriteButton;
...
...
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->WriteButton = (gcnew System::Windows::Forms::Button());
...
...
private: System::Void openToolStripMenuItem_Click(System::Object^ /*sender*/, System::EventArgs^ /*e*/) {
			
			OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
			openFileDialog1->InitialDirectory = "C:\\Documents and Settings\\Anon\\Desktop";
			openFileDialog1->Filter = "All files (*.*)|*.*|gs* files (*.gs*)|*.gs*";
			openFileDialog1->FilterIndex = 2;
			openFileDialog1->RestoreDirectory = true;
			openFileDialog1->FileMode::Open;
			

			if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
				{
					if ( (GameStream = openFileDialog1->OpenFile()) != nullptr )
						{
							//Open file operations
							BinaryReader^ reader = gcnew BinaryReader(GameStream);
							BinaryWriter^ writer = gcnew BinaryWriter(GameStream);
							reader->Read(GameArray,0x010C78,7);
							
							//MessageBox::Show( testvalue, "File Length", MessageBoxButtons::OK, MessageBoxIcon::Information);
							//Char_Stats CharName = 0x010C78;
						}
					else{
						MessageBox::Show( openFileDialog1->FileName, "Error Opening File", MessageBoxButtons::OK, MessageBoxIcon::Error);
						}
				}
			else{
				 //Error Message for opening file
				MessageBox::Show( "New File Not Opened - Any Previous Data Not Saved", "Error Opening File",
				MessageBoxButtons::OK, MessageBoxIcon::Information);
				}
		 }
Not sure if it is the reason, but noticed in the example for BinaryReader in the VS help it uses
1
2
array<Char>^memoryData = gcnew array<Char>(arraySize);
binReader->Read( memoryData, 0, arraySize );

IE the array is of Char not wchar_t
I double checked and what the help files and error messages have led me to understand is that it needs to be declared as a 'wchar-t' type. In my line 5 of my declaration of an array I noticed that I didn't have the left and right sides matching. I declared it 'wchar_t' but on the right it was only 'char' so after changing that it now appears to compile correctly. I also had to correct the reader->Read (line65) because it was apparently trying to seek to that posistion in my ARRAY an not in my FileStream so I added a line to Seek the FileStream first then all came together well. Thanks for the help again Faldrax :-)

Now onto the beast of getting it to read multiple fields as a function....
Last edited on
Topic archived. No new replies allowed.