Ok, So... let say I have string s. And string s contains the name of a button already on my form. How can I use that string to access the button, and so I can change the txt and click event?
It seems so easy, butI cant think of how to do it. |
| Epison07 Tuesday, March 04, 2008 1:49 AM |
You didnt get an exception either? If this code executed and didnt throw an exception, then the button was found and the text was set. Can you set a break point on this and check to see that the buttonis there? You might want to break it down like this to make it easier to debug:
Code Snippet
Button button = ((Button)this.Controls[s]);
button.Text = "Works";
|
| Dan Rigsby Tuesday, March 04, 2008 2:10 AM |
A form has a Controls collection you can use. Each control has a key you can use so you could do:
Code Snippet
((Button)this.Controls[controlName]).Click +=
|
| Dan Rigsby Tuesday, March 04, 2008 1:56 AM |
Ok
So I have a Button named button1 on my form
and I have a string s = "button1"
How do I use your snippet to change the text value.
I tried
Code Snippet ((Button)this.Controls[s]).Text = "Works"
and nothing. |
| Epison07 Tuesday, March 04, 2008 2:06 AM |
You didnt get an exception either? If this code executed and didnt throw an exception, then the button was found and the text was set. Can you set a break point on this and check to see that the buttonis there? You might want to break it down like this to make it easier to debug:
Code Snippet
Button button = ((Button)this.Controls[s]);
button.Text = "Works";
|
| Dan Rigsby Tuesday, March 04, 2008 2:10 AM |
I just tested this and it does work. I would go ahead and check the value with a breakpoint, but also make sure that nothing is overwritting the text after you have changed it. So this code needs to be AFTER you call IntializeComponent() and make sure nothing after this is changing the text to something else. |
| Dan Rigsby Tuesday, March 04, 2008 2:15 AM |
Yeah, i got an exception.
Object reference not set to an instance of an object.
Igot that both times actually.
I have a button on my page named button1 too, I double checked.
|
| Epison07 Tuesday, March 04, 2008 2:16 AM |
Could you try this with a clean form with nothing but the button and add this in the contructor after InitializeComponent(). You should see it working then. There must be something else in your code that is causing this not to work. |
| Dan Rigsby Tuesday, March 04, 2008 2:19 AM |
I had the code in the Load function. I added a new button and moved the code to its click event and the same error popped up. So I dont know what I am going wrong. Maybe I am missing a library or something. I am using
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO; |
| Epison07 Tuesday, March 04, 2008 2:21 AM |
So I just tried it on a new form and it worked. Does this it of code have to be in the constructor or should it work anywhere? |
| Epison07 Tuesday, March 04, 2008 2:26 AM |
Ok, I think I might know why.... The button I am changing is in a panel. Does this change the way it works. If so, how do I change it to work if the button is in panel1???
|
| Epison07 Tuesday, March 04, 2008 2:35 AM |
Nope it can be anywhere. It gets added to the Controls collection in the IntializeComponent() method, so you should be good to go. I dont think there is more i can say to help you out with the current information wehave If you continue to have issues, you might want to post more code. |
| Dan Rigsby Tuesday, March 04, 2008 2:35 AM |
Code Snippet
namespace pizzaregister
{
public partial class Register : Form
{
private DataTable table;
private OrderParser help = new OrderParser();
private int OrderId;
public Register()
{
InitializeComponent();
}
public Register(int OId)
{
InitializeComponent();
OrderId = OId;
}
private void Register_Load(object sender, EventArgs e)
{
//Recalls an order
table = help.MakeTable();
help.Parser(table, OrderId);
CurrentOrder.DataSource = table;
CurrentOrder.Columns[0].Width = 200;
CurrentOrder.Columns[1].Width = 50;
}
private void LoadButtons()
{
FileStream file = new FileStream("C:\\LoadButtons.txt", FileMode.OpenOrCreate, FileAccess.Read);
StreamReader sr = new StreamReader(file);
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
label1.Text = s;
}
}
private void button_Click(object sender, System.EventArgs e)
{
Button pushedButton;
pushedButton = sender as Button;
label1.Text = pushedButton.Name.ToString();
help.AddNewDataRow(table, pushedButton.Name.ToString(), "$0.00");
}
private void button31_Click(object sender, EventArgs e)
{
string s = "button1";
Button button = ((Button)this.Controls[s]);
button.Text = "Works";
}
}
}
I have a grid view, a label, and 31 buttons. The first 5 are in panel1 and button6 - 30 are all in panel2 and button31 is just on the forum. Everything else works fine, expect for the naming of button1. Any ideas about where the problem could be? |
| Epison07 Tuesday, March 04, 2008 2:42 AM |
Actually, that is the problem. If I add panel1 between this. and controls it works just fine. Thanks a bunch of the help |
| Epison07 Tuesday, March 04, 2008 2:50 AM |
Yeah if the buttons are on a panel, then you need to look at this.panel1.Controls["button1"].
If this worked for you, could you mark the post that answered this so that it can be categorized as anwered? |
| Dan Rigsby Tuesday, March 04, 2008 2:56 AM |