How do I get a value from the sender's children?
MouseUp on a Canvas creates a Grid.
private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
{
Grid grid = new Grid();
Label timeLabel = new Label();
timeLabel.Content = "06:00"; //this could be anything
timeLabel.Name = "TimeStart"
grid.Children.Add(timeLabel);
canvas.Children.Add(grid);
grid.MouseDown += new MouseButtonEventHandler(ClickEvent);
}
When the user clicks on an already existing Grid, I want a MessageBox containing timeLabel.Content to appear, in this case, "06:00"
This is not working (I've tried some others as well, same result)
void ClickEvent(object sender, RoutedEventArgs e)
{
Grid test = (Grid)sender;
Label label = (Label)test.FindName("TimeStart");
MessageBox.Show(label.Content.ToString());
}
Error
An unhandled exception of type 'System.NullReferenceException' occurred in MissionControl M.exe
Additional information: Object reference not set to an instance of an object.
You Can use Registername for your label control and give a name, then access it using FindName
private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
{
NameScope.SetNameScope(grid, new NameScope());
Label timeLabel = new Label();
timeLabel.Name = "label1";
grid.RegisterName("label1", timeLabel);
timeLabel.Content = "06:00";
}
void ClickEvent(object sender, RoutedEventArgs e)
{
Grid test = (Grid)sender;
if (test != null)
{
Label label = (Label)test.FindName("label1");
MessageBox.Show(label.Content.ToString());
}
}
You named your grid, yet you try to find your label by name. Pick one or the other. Probably, naming your label instead of your grid makes the most sense.
you should name you lable and then FindName
or you can use then first grid children :
Grid test = (Grid)sender;
if(test != null)
{
Label label = test.Children[0] as Lable;
MessageBox.Show(label.Content.ToString());
}
Related
So i have this block of code and i have a button called AddNewButton which adds a StackPanel into a already created StackPanel called MainStackPanel which is irrelevant but the "GroupPanel" has child controls such as "GroupName", "GroupTextBox" and "GroupEdit".
Now the "GroupEdit" button has a click event that runs the void named "GroupEdit_Click" and in that void i use Button GroupEdit1 = sender as Button; Now this works and makes me able to access the buttons properties and change content but my problem is: How do i access the other controls such as "GroupPanel", "GroupName" and "GroupTextBox". I will use the AddNewButton a few times so when i access the separate controls they need to be accessed seperately
I tried to get rid of as much unnecessary code.
private void AddNewButton_Click(object sender, RoutedEventArgs e)
{
StackPanel GroupPanel = new StackPanel();
TextBlock GroupName = new TextBlock();
GroupName.Text = "Group ";
TextBox GroupTextBox = new TextBox();
GroupTextBox.Visibility = Visibility.Collapsed;
Button GroupEdit = new Button();
GroupEdit.Content = "Edit Group";
GroupEdit.Click += new RoutedEventHandler(GroupEdit_Click);
GroupPanel.Children.Add(GroupName);
GroupPanel.Children.Add(GroupTextBox);
GroupPanel.Children.Add(GroupEdit);
}
private void GroupEdit_Click(object sender,RoutedEventArgs e)
{
Button GroupEdit1 = sender as Button;
GroupEdit1.Content = "Done";
//Now how do i access these controls?
GroupName.Visibility = Visibility.Collapsed;
GroupTextBox.Visibility = Visibility.Visible;
}
}
You could maintain a private List of your dynamically added GroupEdit controls and assign them numbered tags.
private List<TextBox> dynamicGroupEdits = new List<TextBox>();
private void AddNewButton_Click(object sender, RoutedEventArgs e)
{
...
dynamicGroupEdits.Add(GroupEdit);
GroupEdit.Tag = dynamicGroupEdits.Count;
GroupPanel.Tag = GroupEdit.Tag;
GroupTextBox.Tag = GroupEdit.Tag;
...
}
private void GroupEdit_Click(object sender,RoutedEventArgs e)
{
...
tag = GroupEdit1.Tag;
// Loop through all child controls and set visibility according to tag
for each (var c in LogicalTreeHelper.GetChildren(GroupEdit1.Parent)
{
if(c is TextBox && c.Tag == tag)
c.Visible =Visibility.Visible;
else if(c is TextBlock && c.Tag == tag)
c.Visibility = Visibility.Collapsed;
}
}
I am adding got focus event mytextboxes when I create them
`TextBox Xi = new TextBox();
Xi.Name = "X" + i.ToString();
Xi.Width = 10;
Xi.Height = 10;
Xi.GotFocus += Xi_GotFocus;`
But I can't get focused control's name
void Xi_GotFocus(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
is there any way get the name of these controls ? Thank you
you can use below mentioned code
void Xi_GotFocus(object sender, RoutedEventArgs e)
{
TextBox t = sender as TextBox;
string name = t.Name;
}
I am trying to access my dynamically created TextBox in C#, inside an event handler of a Button.
void MainFormLoad(object sender, EventArgs e)
{
this.Width=600;
this.Height=400;
this.FormBorderStyle= FormBorderStyle.FixedDialog;
TextBox t=new TextBox();
this.Controls.Add(t);
t.Location = new Point(60,40);
Label Mylable=new Label();
this.Controls.Add(Mylable);
Mylable.Location=new Point(15,43);
Mylable.Text="string : ";
t.Width=200;
t.Name="MyText";
t.Refresh();
Button Myb=new Button();
Myb.Location=new Point(270,40);
this.Controls.Add(Myb);
Myb.Text="Reverse it!";
Myb.Name="Mybo";
Myb.Click += new EventHandler(this.Myb_Clicked);
this.Refresh();
}
void Myb_Clicked(object sender, EventArgs e) {
// HOW SHOULD I GAIN ACCESS to MyText.Text HERE
MessageBox.Show();
}
Give a name to your dynamic TextBox:
TextBox t=new TextBox();
t.Name = "MyTextBox";
this.Controls.Add(t);
And then:
void Myb_Clicked(object sender, EventArgs e) {
string text = this.Controls["MyTextBox"].Text;
}
Wrong answer: object sender is the TextBox. You can cast sender to textbox and use it.
A decent way would be to make your textbox a class level member. And then you have access to it. If not, link TextBox.Text to a string property and use that.
You could keep a reference to your TextBox in your class
publc class MyForm: Form
{
TextBox myBox = null; // class member
void MainFormLoad(object sender, EventArgs e)
{
this.Width=600;
this.Height=400;
this.FormBorderStyle= FormBorderStyle.FixedDialog;
TextBox t=new TextBox();
myBox = t; // keep it for future reference
// rest of your code
}
void Myb_Clicked(object sender, EventArgs e) {
if (myBox !=null)
{
myBox.Text= "Clicked!";
}
MessageBox.Show();
}
}
I have two controls created on Form_Load, a button and a combobox. I also have an event for the button, but the event should be able to see the newly created combobox.
When I try to call the combobox by it's name it says that it doesn't exist in this context.
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
ComboBox kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
kombo.Items.Add("Panel"); //just an example
}
Is there a way to make this work?
Only controls which are in used in markup with runat="server" will be class variables on your page. They are actually defined in the designer file.
What you'll want to do is in the class add something like the following where you have a class variable, then assign kombo in your page-load function. Then, it will exist in your click event handler.
// kombo is now scoped for use throughout this class
ComboBox kombo = null;
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
// Assign to our kombo instance
kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
// Using the kombo we created in form load, which is still referenced
// in the class
kombo.Items.Add("Panel"); //just an example
}
You will have to use the FindControl() method to find the object first.
private void przycisk_Click(object sender, EventArgs e)
{
ComboBox kombo = (ComboBox)FindControl("kombo");
kombo.Items.Add("Panel");
}
I had code to find label in gridview and I checked on it,s label text and it gives me the index not text and this error apear object refrence not set to ..... so I wnat to give CU.Username = LBL.Text; text no index of control
code
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
LblRseult.Visible = true;
LblRseult.Text = "Successfully Process";
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
cUser CU = new cUser(this);
LBL = (Label)GridView1.Rows[e.RowIndex].Cells[1].FindControl("Label1");
CU.Username = LBL.Text;
if (CU.BasiclyExists())
{
LblRseult.Visible = true;
LblRseult.Text = "This user already exists";
}
}
You can access control inside TemplateField this way:
Label lbl = GridView1.Rows[e.RowIndex].FindControl("Label1") as Label;