Soran
Forumdaki Değişkenlere Ulaşma

Soru
-
Merhaba;
Foreacg ile Form'daki bütün control'lere ulaşabiliyoruz.
foreach (Control item in form1.Controls) { }
gibi
Ancak form içindeki bütün değişkenlere ulaşabiliyor muyuz?
if(item is String)
dedğimde string ler gelmesini istiyorum. yada int böyle isteğin karşılığı olan form özelliği var mı?
Tüm Yanıtlar
-
C# temellerinde değişkenlerin hangi aralıkta taninacagi anlatılıyor.
Form bir class, class seviyesinde bir değişkene class icinde her yerden ulasirsiniz.
How to create a Minimal, Reproducible Example
The way to Go.
World's most advanced open source (object-) relational Database.
Flutter (for mobile, for web & desktop. -
Aslında yapmak istediğim o zamanda açık olan tüm formlardan değişkenlerinin alınması
foreach (Form item in Application.OpenForms) { foreach (Control contra in item.Controls) { if (contra is TextBox) { //işlemler } } }
Yukarıda isimlerini bilmediğim Açık Formlarda isimlerini bilmediğim Textbox'lara ulaştım bu şekilde yapabileceğim formlarda tanımlanmış ancak isimlerini bilmediğim değişkenlere ulaşmak istiyorum.
-
Bu sonuncu soru degil sanırım. access modifiers'a bakiniz.
How to create a Minimal, Reproducible Example
The way to Go.
World's most advanced open source (object-) relational Database.
Flutter (for mobile, for web & desktop. -
-
Ulasabilirsiniz. Sorun erisim yetkinizin olup olmaması. Erisim yetkiniz var ise, zaten butun kontrollere ulaşıp onların PropertyInfo'larini alabilirsiniz.
Ornek:
void Main() { var f2 = new Form2(); f2.Controls.OfType<TextBox>().Single(tb => tb.Name == "T1").Text = DateTime.Now.ToString(); f2.Controls.OfType<TextBox>().Single(tb => tb.Name == "T2").Text = "Merhaba dunya"; var f1 = new Form1(); f2.Show(); f1.Show(); } class Form1 : Form { private Button b = new Button { Top = 10, Left = 10, Text = "Diger formlar", Name="B1" }; private TextBox t = new TextBox {Multiline=true, Top=50, Width=300, Height=500, Name="T1"}; public Form1() { this.Height=400; this.Width=400; this.Text = "Form 1"; this.Name = "Form1"; this.Controls.Add(b); this.Controls.Add(t); this.b.Click += (sender, e) => { this.t.Clear(); StringBuilder sb = new StringBuilder(); foreach (Form f in Application.OpenForms) { sb.AppendLine($"Form: {f.Name} [{f.Text}]"); foreach (Control c in f.Controls) { sb.AppendLine($"Kontrol: {c.Name} [{c.GetType()}]"); if (c is TextBox) { sb.AppendLine($"{c.Name} text: {((TextBox)c).Text}"); } } } t.Text = sb.ToString(); }; } } class Form2 : Form { private Label lbl1 = new Label { Top = 10, Left = 10, Width=70, Name = "L1", Text="Label1" }; private Label lbl2 = new Label { Top = 50, Left = 10, Width=70, Name = "L2", Text="Label2" }; private TextBox t1 = new TextBox { Top = 10, Left = 100, Width=150, Name = "T1" }; private TextBox t2 = new TextBox { Top = 50, Left = 100, Width=150, Name = "T2" }; public Form2() { this.Text = "Form 2"; this.Name = "Form2"; this.Controls.AddRange(new Control[] {lbl1, t1, lbl2, t2}); } }
How to create a Minimal, Reproducible Example
The way to Go.
World's most advanced open source (object-) relational Database.
Flutter (for mobile, for web & desktop.
- Düzenleyen CetinBasozEditor 27 Ocak 2020 Pazartesi 14:34
-
Bu sonuncu soru degil sanırım. access modifiers'a bakiniz.
How to create a Minimal, Reproducible Example
The way to Go.
World's most advanced open source (object-) relational Database.
Flutter (for mobile, for web & desktop.
https://docs.microsoft.com/tr-tr/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers
Çetin Abinin dediği gibi burayı incelersen sorunun cevabını bulmuş olursun.