我有一個windows窗體應用程序,其中一些控件添加到設計器中。當我想改變的東西(像)從Form1.cs的內啓用一個文本框從另一個類訪問窗體控件
我簡單地使用
textBox1.Enabled = true;
,但現在我已經叫分離類的Class1.cs
怎麼可能我啓用textBox1從一個靜態函數class1.cs?
{NOTE}我沒有試過任何代碼,因爲我完全無法做到這一點。
我有一個windows窗體應用程序,其中一些控件添加到設計器中。當我想改變的東西(像)從Form1.cs的內啓用一個文本框從另一個類訪問窗體控件
我簡單地使用
textBox1.Enabled = true;
,但現在我已經叫分離類的Class1.cs
怎麼可能我啓用textBox1從一個靜態函數class1.cs?
{NOTE}我沒有試過任何代碼,因爲我完全無法做到這一點。
編輯:很多的編輯。
public partial class Form1 : Form
{
// Static form. Null if no form created yet.
private static Form1 form = null;
private delegate void EnableDelegate(bool enable);
public Form1()
{
InitializeComponent();
form = this;
}
// Static method, call the non-static version if the form exist.
public static void EnableStaticTextBox(bool enable)
{
if (form != null)
form.EnableTextBox(enable);
}
private void EnableTextBox(bool enable)
{
// If this returns true, it means it was called from an external thread.
if (InvokeRequired)
{
// Create a delegate of this method and let the form run it.
this.Invoke(new EnableDelegate(EnableTextBox), new object[] { enable });
return; // Important
}
// Set textBox
textBox1.Enabled = enable;
}
}
實例,你需要有一個包含您要啓用/禁用 –
然後從Form類的實例應該創建文本框表單的參考,這是一個好主意? –
它不工作:( –
你可以讓你的class1有一個事件來啓用文本框。
public class Class1
{
public event Action<object, EventArgs> subscribe ;
private void raiseEvent()
{
var handler = subscribe ;
if(handler!=null)
{
handler(this,EventArgs.Empty);//Raise the enable event.
}
}
}
讓包含TextBox的類以某種方式訂閱它。在文本框包裝類
public class TextBoxWrapper
public void EnablePropertyNotification(object sender, EventArgs args)
{
TextBox1.Enabled = true ; //Enables textbox when event is raised.
}
public TextBoxWrapper()
{
class1Instance.subscribe+=EnablePropertyNotification ;
}
我怎麼能從一個靜態函數class1.cs啓用textBox1? –
我會問爲什麼這是一個靜態方法。如果你給我一個很好的理由,爲什麼然後我會考慮它 –
好的理由是,我已經將所有的異步套接字構建爲靜態套接字,所以我不能再將其全部替換。另一個很好的理由是我想學習兩種方法。 –
您可以將表格的實例傳遞到類
MyForm frm = new MyForm();
MyClass c = new MyClass(frm);
然後你的類可以採取的實例和訪問文本框
public class MyClass
{
public MyClass(MyForm f)
{
f.TextBox1.Enabled = false;
}
}
設計確實不好看
最好在窗體中調用該類,並基於val UE反饋,操縱文本框
//MyForm Class
MyClass c = new MyClass();
c.DoSomethings();
if(c.getResult() == requiredValue)
textBox1.enabled = true;
else
textBox1.enabled = false;
//MyForm Class ends here
UPDATE
public class Class1
{
public static int SomeFunction()
{
int result = 1;
return result;
}
public static void SomeFunction(out int result)
{
result = 1;
}
}
使用
if(Class1.SomeFunction() == 1)
textBox1.Enabled = true;
else
textBox1.Enabled = false;
OR
int result = 0;
Class1.SomeFunction(out result);
if(result == 1)
textBox1.Enabled = true;
else
textBox1.Enabled = false;
如何從靜態函數class1.cs啓用textBox1? –
您的靜態函數可能會返回結果,或者您使用可以從靜態函數中操作的out變量 - 這是關於設計 – codingbiz
你真的不應該改變您的Form
中的UI控件從您的類1,而是創建一個方法或屬性class1,它將告訴是否應該啓用文本框。
例子:
// I changed the name class1 to MySettings
public class MySettings
{
public bool ShouldTextBoxBeEnabled()
{
// Do some logic here.
return true;
}
// More generic
public static bool SetTextBoxState(TextBox textBox)
{
// Do some logic here.
textBox.Enabled = true;
}
// Or static property (method if you like)
public static StaticShouldTextBoxBeEnabled { get { return true; } }
}
然後在您的形式:
MySettings settings = new MySettings();
textBox1.Enabled = settings.ShouldTextBoxBeEnabled();
// Or static way
textBox1.Enabled = MySettings.StaticShouldTextBoxBeEnabled;
// Or this way you can send in all textboxes you want to do the logic on.
MySettings.SetTextBoxState(textBox1);
這是你應該怎麼做:
public static Form1 form = null;
private delegate void SetImageDelegate(Image image);
public Form1()
{
InitializeComponent();
form = this;
}
public static void SetStaticImage(Image image)
{
if (form != null)
form.pic1.Image = image;
}
private void setImage(Image img)
{
// If this returns true, it means it was called from an external thread.
if (InvokeRequired)
{
// Create a delegate of this method and let the form run it.
this.Invoke(new SetImageDelegate(setImage), new object[] { img });
return; // Important
}
// Set textBox
pic1.Image = img;
}
: 我在表單類寫了下面的代碼並且下面的代碼應該屬於其他類:
Form1 frm= Form1.form;
frm.pic1.Image = image;
注意,我改變private static Form1 form = null;
到public static Form1 form = null;
好運...寫的哈桑Eskandari :)
這只是另一種方法:
TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
爲ü想要什麼原因去做?我想你應該改變你的設計 –
改變我的設計!這是Windows窗體的本質,我無法訪問Form1.cs之外的控件,因爲它們是私人的。 –
您是否嘗試過將參考傳遞給班級中的控件?它可能是一個靜態方法的論點。您需要提供class1.cs的上下文。它是派生的控件,你在哪裏舉辦的這等 –