C
C#2y ago
Luna

✅Modifying visual elements created in runtime

i'll send a snippet of my code
void x(){
TabPage tp = new TabPage("example");
TextBox tb = new TextBox();
tb.Text = "wasd";
tb.Dock = DockSytle.Fill;
tp.Controls.Add(tb);

tabControl1.TabPages.Add(tp);
}

void y(){
// how can i edit tb in this function
}
void x(){
TabPage tp = new TabPage("example");
TextBox tb = new TextBox();
tb.Text = "wasd";
tb.Dock = DockSytle.Fill;
tp.Controls.Add(tb);

tabControl1.TabPages.Add(tp);
}

void y(){
// how can i edit tb in this function
}
5 Replies
Luna
LunaOP2y ago
for example i wanna change the text of this textbox in another function
Arch Leaders
Arch Leaders2y ago
You need to expose that object to the other function Example:
class YourClass
{
private TextBox _tb = new();

public YourClass()
{
TabPage tp = new("example");
_tb.Text = "wasd";
_tb.Dock = DockSytle.Fill;
tp.Controls.Add(_tb);

tabControl1.TabPages.Add(tp);
}

public void YourFunction()
{
_tb.Text = "new text";
}
}
class YourClass
{
private TextBox _tb = new();

public YourClass()
{
TabPage tp = new("example");
_tb.Text = "wasd";
_tb.Dock = DockSytle.Fill;
tp.Controls.Add(_tb);

tabControl1.TabPages.Add(tp);
}

public void YourFunction()
{
_tb.Text = "new text";
}
}
Luna
LunaOP2y ago
i have to declare this visual elements in my class, not inside the function? make sense i think
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Luna
LunaOP2y ago
Oh Sorry
Want results from more Discord servers?
Add your server