Mechanics schrieb:
martinvs75 schrieb:
mit VS2010 C++/CLI.
Keine gute Idee. Hier gibts mehrere längere Threads, warum C++/CLI zum Programmieren ungeeignet ist. Musst selber danach suchen, bin grad zu faul
+1
martinvs75 schrieb:
(Ich habe mal in einem Buch darüber gelesen, es war etwas mit
textBox->Lines [1], aber ich komme damit leider nicht weiter.)
Eine ListBox tuts nicht!?
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class form_t : public System::Windows::Forms::Form
{
public: form_t() { InitializeComponent(); }
protected: ~form_t() { delete components; }
private: System::Windows::Forms::Button ^ add_line_button;
private: System::Windows::Forms::TextBox ^ line;
private: System::Windows::Forms::ListBox ^ listbox;
private: System::Windows::Forms::TextBox ^ position;
private: System::Windows::Forms::Label ^ position_label;
private: System::Windows::Forms::Label ^ line_label;
private: System::ComponentModel::IContainer ^ components;
private:
#pragma region Windows Form Designer generated code
void InitializeComponent( void )
{
this->add_line_button = ( gcnew System::Windows::Forms::Button() );
this->line = ( gcnew System::Windows::Forms::TextBox() );
this->listbox = ( gcnew System::Windows::Forms::ListBox() );
this->position = ( gcnew System::Windows::Forms::TextBox() );
this->position_label = ( gcnew System::Windows::Forms::Label() );
this->line_label = ( gcnew System::Windows::Forms::Label() );
this->SuspendLayout();
//
// add_line_button
//
this->add_line_button->Location = System::Drawing::Point( 354, 316 );
this->add_line_button->Name = L"add_line_button";
this->add_line_button->Size = System::Drawing::Size( 75, 23 );
this->add_line_button->TabIndex = 3;
this->add_line_button->Text = L"Add";
this->add_line_button->UseVisualStyleBackColor = true;
this->add_line_button->Click += gcnew System::EventHandler( this, &form_t::add_line_button_click );
//
// line
//
this->line->Location = System::Drawing::Point( 179, 318 );
this->line->Name = L"line";
this->line->Size = System::Drawing::Size( 149, 20 );
this->line->TabIndex = 2;
this->line->PreviewKeyDown += gcnew System::Windows::Forms::PreviewKeyDownEventHandler( this, &form_t::line_preview_key_down );
//
// listbox
//
this->listbox->FormattingEnabled = true;
this->listbox->Location = System::Drawing::Point( 13, 13 );
this->listbox->Name = L"listbox";
this->listbox->Size = System::Drawing::Size( 416, 290 );
this->listbox->TabIndex = 4;
this->listbox->SelectedIndexChanged += gcnew System::EventHandler( this, &form_t::listbox_selected_index_changed );
//
// position
//
this->position->Location = System::Drawing::Point( 80, 319 );
this->position->Name = L"position";
this->position->Size = System::Drawing::Size( 40, 20 );
this->position->TabIndex = 1;
this->position->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler( this, &form_t::position_keypress );
this->position->PreviewKeyDown += gcnew System::Windows::Forms::PreviewKeyDownEventHandler( this, &form_t::position_preview_key_down );
//
// position_label
//
this->position_label->AutoSize = true;
this->position_label->Font = ( gcnew System::Drawing::Font( L"Segoe UI", 11.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast< System::Byte >( 0 ) ) );
this->position_label->Location = System::Drawing::Point( 12, 317 );
this->position_label->Name = L"position_label";
this->position_label->Size = System::Drawing::Size( 62, 20 );
this->position_label->TabIndex = 0;
this->position_label->Text = L"Position";
//
// line_label
//
this->line_label->AutoSize = true;
this->line_label->Font = ( gcnew System::Drawing::Font( L"Segoe UI", 11.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast< System::Byte >( 0 ) ) );
this->line_label->Location = System::Drawing::Point( 136, 317 );
this->line_label->Name = L"line_label";
this->line_label->Size = System::Drawing::Size( 37, 20 );
this->line_label->TabIndex = 0;
this->line_label->Text = L"Text";
//
// form_t
//
this->AutoScaleDimensions = System::Drawing::SizeF( 6, 13 );
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size( 441, 353 );
this->Controls->Add( this->line_label );
this->Controls->Add( this->position_label );
this->Controls->Add( this->position );
this->Controls->Add( this->listbox );
this->Controls->Add( this->line );
this->Controls->Add( this->add_line_button );
this->Name = L"form_t";
this->Text = L"form";
this->ResumeLayout( false );
this->PerformLayout();
}
#pragma endregion
private: System::Void add_line_button_click( System::Object ^ sender, System::EventArgs ^ e )
{
try {
int pos = Convert::ToInt32( position->Text );
if( listbox->Items->Count <= pos ) {
array< String ^ > ^ padding_items = gcnew array< String ^ >( pos - listbox->Items->Count + 1 );
for( int i = 0; i < padding_items->Length; ++i )
padding_items[ i ] = gcnew String( "" );
listbox->Items->AddRange( padding_items );
}
listbox->Items[ pos ] = line->Text;
listbox->SelectedIndex = pos;
} catch( System::FormatException ^ ) {
Windows::Forms::MessageBox::Show( "Please enter a number!" );
} catch( System::OverflowException ^ ) {
Windows::Forms::MessageBox::Show( "The number you entered is too big!" );
}
line->Text = "";
position->Text = "";
position->Focus();
}
private: System::Void position_preview_key_down( System::Object ^ sender, System::Windows::Forms::PreviewKeyDownEventArgs ^ e )
{
if( e->KeyData == Keys::Enter && position->Text->Length )
line->Focus();
}
private: System::Void position_keypress( System::Object ^ sender, System::Windows::Forms::KeyPressEventArgs ^ e )
{
if( !Char::IsDigit( e->KeyChar ) && !Char::IsControl( e->KeyChar ) )
e->Handled = true;
}
private: System::Void line_preview_key_down( System::Object ^ sender, System::Windows::Forms::PreviewKeyDownEventArgs ^ e )
{
if( e->KeyCode == Keys::Enter )
add_line_button_click( sender, gcnew System::EventArgs );
}
private: System::Void listbox_selected_index_changed( System::Object ^ sender, System::EventArgs ^ e )
{
if( listbox->SelectedIndex == -1 )
return;
position->Text = Convert::ToString( listbox->SelectedIndex );
line->Text = Convert::ToString( listbox->Items[ listbox->SelectedIndex ] );
line->SelectAll();
line->Focus();
}
};
#include "form.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
int main( array< String ^ > ^ argv )
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault( false );
form_t form;
Application::Run( %form );
}