Dostęp do textBox w innej klasie

0

Witam!
W programie mam sporą liczbę textboxów i chciałbym sie do nich odwołać innej klasie utworzonej w tej samej formie. Czy można jakoś zmodyfikować dostęp to tychże textboxów żeby można było zmiennej przypisać wartość tych textboxa np

 class cPrzykład
               {
                   string a = textBox1.text ;
               }

próbowałem robiąc coś takiego:

 public partial class Form1 : Form
    {
        
        public Form1()
        {
     
            
            InitializeComponent();
           
            
        }
        
        
        string prop
        {
            get { return textBox1.Text; }
        }
.
.
.
class cPrzyklad
        {
           
            Form1 f = new Form1();
            string s = f.prop;
         
        }

Ale otrzymałem komunikat błędu CS0236 - A field initializer cannot reference the nonstatic field, method, or property 'field'.

Wartości podane w texboxach są mi potrzebne do obliczeń a jak na razie to nie potrafię sie do nich dostać.

0

A więc możesz ustawić tego textboksa jako statyczne pole (okropnie nieeleganckie i ogólnie złe rozwiązanie). Druga ładniejsza metoda to przesłanie obiektu do metody używając słowa kluczowego this, mniej więcej tak:

class Form1 : Form
{
Jakastam klasa = new Jakastam(this);
}
class Jakastam
{
Form edytowalnaForma;
public Jakastam(Form Formatka)
{
edytowalnaForma = Formatka
}
edytowalnaForma.textBox1.Text = "AAA";
}
0

Dzięki Lukke :)
To teraz jeszcze jedno pytanko, żeby nie tworzyć nowego posta:
Chcę porownać zawartość dwóch textboxów i szukam bardziej uniwersalnej metody niż ta napisana przeze mnie :

private void textBox9_Validating(object sender, CancelEventArgs e)
        {
            if (Int16.Parse(textBox9.Text) > Int16.Parse(textBox3.Text))
            {
                e.Cancel = true;
                MessageBox.Show("za duża wartość");
            }
            .....

Chodzi mi o stworzenie metody która by pasowała do każdego texboxa (żeby do wszystkich 30 nie powtażać tego kodu). W powyższej metodzie zmienia sie textbox, który jest sprawdzany

0

To znaczy ktory jest sprawdzany?

Zakladajac, ze sprawdzany jest textBox3 to metoda moze wygladac tak:

private void textBoxValidating(object sender, CancelEventArgs e)
        {
            if (Int16.Parse(((TextBox)sender).Text) > Int16.Parse(textBox3.Text))
            {
                e.Cancel = true;
                MessageBox.Show("za duża wartość");
            }
            .....

Teraz pod wszystkie Textboxy z wyjatkiem 3 mozesz podpiac takie zdarzenie.

pozdrawiam
johny

0

TextBox9 jest sprawdzany w tamtym przypadku i o dokładnie taki kod mi chodziło, dzięki :)

0

Witam, troszkę nie rozumiem co napisał Lukke, a mam taki sam problem że brakuje mi dostępu do textbox.

#pragma once
#include <string>

namespace ZalRatio {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace std;
	

	/// <summary>
	/// Summary for Formularz
	/// </summary>
	public ref class Formularz : public System::Windows::Forms::Form
	{
	public:
		Formularz(void)
		{
			InitializeComponent();
			
		}
		~Formularz()
		{
			if (components)
			{
				delete components;
			}
		}
		
	#pragma region okreslenie prywatnosci form
	
	public: System::Windows::Forms::Label^  label1;
	public: System::Windows::Forms::Label^  label2;
	public: System::Windows::Forms::TextBox^  textBox1;
	public: System::Windows::Forms::TextBox^  textBox2;
	public: System::Windows::Forms::Button^  button1;
	public: System::Windows::Forms::TextBox^  textBox3;
	public: 
	public: System::Windows::Forms::TextBox^  textBox4;
	public: System::Windows::Forms::Label^  label3;
	public: 
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::RadioButton^  radioButton1;
	private: System::Windows::Forms::RadioButton^  radioButton2;
	private: System::Windows::Forms::RadioButton^  radioButton3;
	private: System::Windows::Forms::RadioButton^  radioButton4;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::Label^  label6;
	protected: 
#pragma endregion 
	public:
		
		 
		/// <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)
		{
			
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton3 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton4 = (gcnew System::Windows::Forms::RadioButton());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(13, 28);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(40, 13);
			this->label1->TabIndex = 0;
			this->label1->Text = L"Licznik";
			this->label1->Click += gcnew System::EventHandler(this, &Formularz::label1_Click);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(13, 97);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(58, 13);
			this->label2->TabIndex = 1;
			this->label2->Text = L"Mianownik";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(16, 44);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 20);
			this->textBox1->TabIndex = 2;
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(16, 113);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(100, 20);
			this->textBox2->TabIndex = 3;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(99, 277);
			this->button1->Name = L"button1";
			this->button1->RightToLeft = System::Windows::Forms::RightToLeft::Yes;
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 4;
			this->button1->Text = L"Wprowadź";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Formularz::button1_Click);
			// 
			// textBox3
			// 
			this->textBox3->Location = System::Drawing::Point(170, 113);
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(100, 20);
			this->textBox3->TabIndex = 8;
			// 
			// textBox4
			// 
			this->textBox4->Location = System::Drawing::Point(170, 44);
			this->textBox4->Name = L"textBox4";
			this->textBox4->Size = System::Drawing::Size(100, 20);
			this->textBox4->TabIndex = 7;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(167, 97);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(58, 13);
			this->label3->TabIndex = 6;
			this->label3->Text = L"Mianownik";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(167, 28);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(40, 13);
			this->label4->TabIndex = 5;
			this->label4->Text = L"Licznik";
			// 
			// radioButton1
			// 
			this->radioButton1->AutoSize = true;
			this->radioButton1->Location = System::Drawing::Point(16, 167);
			this->radioButton1->Name = L"radioButton1";
			this->radioButton1->Size = System::Drawing::Size(31, 17);
			this->radioButton1->TabIndex = 9;
			this->radioButton1->TabStop = true;
			this->radioButton1->Text = L"+";
			this->radioButton1->UseVisualStyleBackColor = true;
			// 
			// radioButton2
			// 
			this->radioButton2->AutoSize = true;
			this->radioButton2->Location = System::Drawing::Point(88, 167);
			this->radioButton2->Name = L"radioButton2";
			this->radioButton2->Size = System::Drawing::Size(28, 17);
			this->radioButton2->TabIndex = 10;
			this->radioButton2->TabStop = true;
			this->radioButton2->Text = L"-";
			this->radioButton2->UseVisualStyleBackColor = true;
			// 
			// radioButton3
			// 
			this->radioButton3->AutoSize = true;
			this->radioButton3->Location = System::Drawing::Point(170, 167);
			this->radioButton3->Name = L"radioButton3";
			this->radioButton3->Size = System::Drawing::Size(29, 17);
			this->radioButton3->TabIndex = 11;
			this->radioButton3->TabStop = true;
			this->radioButton3->Text = L"*";
			this->radioButton3->UseVisualStyleBackColor = true;
			// 
			// radioButton4
			// 
			this->radioButton4->AutoSize = true;
			this->radioButton4->Location = System::Drawing::Point(240, 167);
			this->radioButton4->Name = L"radioButton4";
			this->radioButton4->Size = System::Drawing::Size(30, 17);
			this->radioButton4->TabIndex = 12;
			this->radioButton4->TabStop = true;
			this->radioButton4->Text = L"/";
			this->radioButton4->UseVisualStyleBackColor = true;
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(96, 204);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(37, 13);
			this->label5->TabIndex = 13;
			this->label5->Text = L"Wynik";
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(96, 233);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(35, 13);
			this->label6->TabIndex = 14;
			this->label6->Text = L"label6";
			// 
			// Formularz
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(303, 312);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->radioButton4);
			this->Controls->Add(this->radioButton3);
			this->Controls->Add(this->radioButton2);
			this->Controls->Add(this->radioButton1);
			this->Controls->Add(this->textBox3);
			this->Controls->Add(this->textBox4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Name = L"Formularz";
			this->Text = L"Formularz";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		



	private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				
				 label6->Text = "lol";
			 }
};

	


	private class CRationalNum {
		private:
			int numerator;
		    int denominator;


		CRationalNum(){
			numerator = 0;
			denominator = 1;
		}

		CRationalNum(int licznik, int mianownik){
			numerator = licznik ; 
			if (mianownik == 0 ) mianownik = 1; 
			denominator = mianownik;
		}

		CRationalNum(const CRationalNum &ulamek){
			numerator = ulamek.numerator;
			denominator = ulamek.denominator;
		}

		CRationalNum set (){
			string napis1;
		//	napis1 = textBox1->Text; 
		 
		}
		
		double ulamek_dziesietny()
		{
			return numerator/denominator;
		}

		CRationalNum operator+(CRationalNum &a){
		
			return CRationalNum( this->numerator + a.numerator, this->denominator + a.denominator );
		}


		};

	
}
 

z tego co widzę należy utworzyć obiekt klasy w FORM, ale gdy wpisuje kod przed konstruktorami Formularza wyskakuje mi błąd już

public ref class Formularz : public System::Windows::Forms::Form
	{
	CRationalNum klasa = new CRationalNum(this);
	public:
		
		Formularz(void)
		{
			InitializeComponent();
			
		} 

Mógłby ktoś pomóc ?

0

główna forma:

public partial class MainWindow : Form
{
    public MainWindow()
    {
    }

    private void Test()
    {
        NewForm newForm = new NewForm(this);
        newForm.Show();
    }
}

nowa forma z dostepem to textboxa z glownej formy

public partial class NewForm : Form
{
    MainWindow parentForm = null;

	public NewForm(Form parentForm)
    {
        InitializeComponent();
        this.parentForm = parentForm as MainWindow;
		this.parentForm.textBox1.Text = "Hello";
    }
}
1

Upublicznianie wewnętrznych kontrolek okna to bardzo zły pomysł. Należy je opakowywać w metody lub właściwości, a nie upubliczniać!

1 użytkowników online, w tym zalogowanych: 0, gości: 1