Accessing a dynamic RadioButtonList array object - c#

I've created an array of RadioButtonList class, but apparently can't seem to access it or use the answer retrieved from it. I always get the exception: Object reference not set to an instance of an object
static int jimmy = 0;
protected void Button5_Click(object sender, EventArgs e)
{
int sizeOfPain = GlobalVariables.sympLCWR1Pain.Count;
RadioButtonList[] RBLPain = new RadioButtonList[sizeOfPain];
Label1.Visible = false;
RadioButtonList1.Visible = false;
Label[] Labella = new Label[sizeOfPain];
if (jimmy < sizeOfPain)
{
Labella[jimmy] = new Label();
RBLPain[jimmy] = new RadioButtonList();
Labella[jimmy].Text = GlobalVariables.sympLCWR1Pain[jimmy];
RBLPain[jimmy].Items.Add("Yes");
RBLPain[jimmy].Items.Add("No");
Panel1.Controls.Add(Labella[jimmy]);
Panel1.Controls.Add(RBLPain[jimmy]);
if (RBLPain[jimmy].SelectedIndex == 0)
{
GlobalVariables.sympLCWR1Yes.Add(GlobalVariables.sympLCWR1Pain[jimmy]);
}
}
else
{
Label2.Text = "YOUS DONE!";
Label3.Text = GlobalVariables.sympLCWR1Yes[0];
Button5.Visible = false;
}
jimmy++;
}
i get the exception at the if condition. Any help would be appreciated thanks :)

What that error means is that you are trying to access something that hasn't yet been instantiated. In your updated code, I see you have the following within your click event handler:
RadioButtonList[] RBLPain = new RadioButtonList[sizeOfPain];
Label[] Labella = new Label[sizeOfPain];
This means that every time the click event is handled, you are redeclaring the RBLPain and Labella arrays. Also, when execution leaves leaves the handler, the variables fall out of scope, so you will not be able to use them in other functions, or use the changes made within the handler from one call to the next. I don't know what the rest of your code is doing, but despite the seemingly unnecessary arrays, execution should survive your click event.
In your original post you were trying to access the SelectedItem.Text property of the RBLPain[jimmy]. In this revision you are checking the SelectedIndex instead. When SelectedIndex is -1, SelectedItem will be null, perhaps this led to your original problem. Regardless of what is changed on your form, because you are creating a new RadioButtonList during every click event, you are not working with the values from your form - SelectedIndex will always be -1 from what I can see.

I dont understand why you checking the condition .If you are creating rbl on buttonclick first item should always get selected. Anyway use RBLPain[jimmy].SelectedIndex=0;before if condition.

Related

Make buttons in List send unique data to method

I made a application which will place out buttons in a grid where the user specifies how big the playfield should be.
I create the buttons in a list, specify some data like backgroundimage, size, and location. I then need to, in some way make the different buttons execute different code. I figured I could do this in one method, (if there aren't any good ways to programmatically create methods), if I could somehow make the buttons send a unique piece of information to the method to identify which button is pressed.
public void buttonplacer()
{
int nbrofbtns = Form2.puzzlesize * Form2.puzzlesize;
List<Button> btnslist = new List<Button>();
for (int i = 0; i < nbrofbtns; i++)
{
Button newButton = new Button();
btnslist.Add(newButton);
this.Controls.Add(newButton);
newButton.Name = "btn" + i.ToString();
newButton.Width = btnsidelength;
newButton.Height = btnsidelength;
newButton.Top = btnsidelength * Convert.ToInt32(Math.Floor(Convert.ToDouble(i / Form2.puzzlesize)));
newButton.Left = btnsidelength * Convert.ToInt32(Math.Floor(Convert.ToDouble(i)) - Math.Floor((Convert.ToDouble(i)) / (Form2.puzzlesize)) * (Form2.puzzlesize));
newButton.BackgroundImage = Lights_out_.Properties.Resources.LightsOutBlack;
newButton.Click += new EventHandler(Any_Button_Click);
}
}
void Any_Button_Click(object sender, EventArgs e)
{
}
(If you want to know I'm doing a game called "Light's out")
Thanks in advance!
The Any_Button_Click method receives an object sender that is the button that got clicked. You just need to cast it to a Button:
void Any_Button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
// do stuff here
}
You can use the button's Location property to figure out where it sits on the game board, or you can assign an arbitrary object to the button with any information you choose at initialization time using the Tag property like this:
button.Tag = "someHelpfulString";
or like this:
button.Tag = new Tuple<int, int>(xpos, ypos);
(where xpos and ypos are positions in the button grid)
or like this:
button.Tag = new ButtonInfoObject(foo, bar, baz);
(Here it's up to you to define the ButtonInfoObject class.)
As an alternative to other answers, and in particular to somewhat address the part "good ways to programmatically create methods", there is part of the C# language called Lambda Expressions. To keep long story short, you could write something along these lines:
newButton.Click += (s, e) =>
{
//here you have access to all variables accessible in current scope,
//including "newButton" and "i";
//you could, for example, call some method passing "i" as an argument
//or just put that method's code inside this block
};
The only downside of this approach is that you need to take some extra care if you're planning to unregister the handler at some later point (see this question or this question for reference).
EDIT
As pointed in comments I overlooked the fact that i stays in scope for the whole for loop, so using it inside lambda is pretty much pointless (all handlers will use it's final value). To make it behave like expected one can simply define a variable inside the loop so it goes out of scope at the end of each iteration and is stored separately for each handler:
var btnNo = i;
newButton.Click += (s, e) =>
{
//use "btnNo" instead of "i"
//you can still safely use "newButton" reference
//since it's defined inside the loop
}
Use the sender object to get the button's name that was pressed:
void Any_Button_Click(object sender, EventArgs e)
{
switch ((sender as Button).Name)
{
case "btn0":
//...
break;
case "btn1":
//...
break;
//...
}
}

How to properly send different parameters down for a standard Button Click

I know this has to have an easy answer, but I'm utterly failing to fathom the wealth of information on custom events, event handlers, and delegates. I have a custom messagebox class. I am trying to add the capability to do something based off of the state of a check box if the OK button is clicked. The buttons and the checkbox are added dynamically based upon input into a static Show method somewhat like the following:
if (!String.IsNullOrWhiteSpace(suicideCheckboxID))
{
suicideCheckBox = new CheckBox();
suicideCheckBox.AutoSize = true;
suicideCheckBox.Text = "Do not show this message again.";
suicideCheckBox.Location = new Point(xMargin, label.Bottom + yMargin);
suicideCheckBox.Checked = false;
suicideCheckBoxHeight = suicideCheckBox.Height;
form.Controls.Add(suicideCheckBox);
}
Button okButton = NewButton(DialogResult.OK, scaleFactor);
int x = (form.Width - okButton.Width) / 2;
okButton.Location = new Point(x, buttonYPosition);
form.Controls.Add(okButton);
form.AcceptButton = okButton;
form.CancelButton = okButton;
That's not the exact code, but it's fairly representative. My impulse is to use okButton.Clicked += new EventHandler(OKButton_clicked), but if I do that, the event generated only carries arguments for object sender and EventArgs e and I really need it to operate off of the state of the checkbox and an additional piece of text to indicate which messagebox is being shown so that the values can be stored in the registry.
My first attempt was to do something like okButton.Clicked += processSuicideCheckbox(suicideCheckboxID, suicideCheckBox);, but that seems to just process the contents and allow one to return an EventHandler that points to a method with the signature of object sender and EventArgs e. What am I missing here? What is the best way to pass in the arguments actually relevant to me?
You don't get to choose what is in the event handler for the Click event. Microsoft has already done that. You are stuck with the (object sender, EventArgs e) signature.
You do have a couple options:
Simply store the state in the class itself; the event handler will have access to it because it is inside the class.
Utilize a closure to do the same thing:
myButton.Click += (s, e) => ActualFunction(checkBox1.Checked);
Note that using the closure (via a lambda expression) is just hiding the details of maintaining this state (creating the class-level variables).

Fill a combo box by selecting item in ListView

Im trying to fill combobox from one of two arraylist, on changing selection in listview with method listView1_SelectedIndexChanged.
The problem is it works fine the first time, but the second time I get the following error: "Object reference not set to an instance of an object."
The error is probably happening here: string pr = listView1.FocusedItem.Text;
Please help.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
ArrayList Profesor1 = new ArrayList();
Profesor1.Add("Kolegij 1 profesor 1");
Profesor1.Add("Kolegij 2 profesor 1");
ArrayList Profesor2 = new ArrayList();
Profesor2.Add("Kolegij 1 profesor 2");
Profesor2.Add("Kolegij 2 profesor 2");
string pr = listView1.FocusedItem.Text; //posible prob
switch (pr)
{
case "Profesor 1": comboBox1.DataSource = Profesor1;
break;
case "Profesor 2": comboBox1.DataSource = Profesor2;
break;
}
}
Is there something else going on in your example affecting the listView1 item? I've mocked up a small clone of your question and I can't seem to replicate the error:
I'm using your code for the event handler, and I'm populating the listView1 thusly:
listView1.Items.Add("Profesor 1");
listView1.Items.Add("Profesor 2");
It sounds like you're causing the listView1_SelectedIndexChanged handler to be fired in the background.
As a slight aside, your array lists aren't going to change, you probably don't want to keep your ArrayList creation in your event handler and keep recreating them each time (they don't seem to have any dynamic information in). particularly as that handler will often be called twice once, for the deselection, and again for a selection.
On that last point, it's also worth having a look at the ItemSelectionChanged event, which could be used to help your initial problem, as it comes with ListViewItemSelectionChangedEventArgs which contain the property IsSelected, which you could use to verify that you have an object selected.
e.g:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)
{
// Your code here.
// e.Item...
}
}

Winforms DateTimePicker set default text and/or value

I have a DateTimePicker with ShowCheckBox = true on my winforms app. If I do this in the forms constructor:
DateFrom.Checked = true;
DateFrom.Value = DateTime.Today.AddDays(-7);
Then set DateFrom.Checked = false; in the FormShown event, it does what I would like, the text in the control defaults to 7 days before today, and the checkbox is unchecked.
If I try to only set the Value, the text stays as today. If I reset Checked = false anytime before the FormShown event, the text stays as today.
Now I've moved this code to a user control, so to use the same "hack" will require even more hacking, so at this point I'm hoping someone has an easier method. Maybe just another property I can set besides from Value that actually works? :)
I tried this also:
DateFrom.Text = DateTime.Today.ToString(DateFrom.CustomFormat);
Instead of setting the value, or in addition to it, to no avail.
Typical, I tried for hours before posting my question, then right after I thought it might somehow be related to the creation of the window handle. So I came up with this solution. Will still be happy to have something better, but this doesn't seem to bad if I have to stay with this:
DateFrom.Checked = true;
DateFrom.Value = DateTime.Today.AddDays(-7);
if (DateFrom.Handle == null)
System.Threading.Thread.Sleep(0);
DateFrom.Checked = false;
Checking Handle forces the window handle to be created, so then I'm able to uncheck the control without it defaulting to today's date for the text when the window handle is created later. I just use Sleep(0) as a trick to make sure the compiler doesn't optimize the code and compile it out all together (not sure if that would even happen, but like to be sure, and condition shouldn't always be false so should never Sleep(0) anyway).
It might also be that the control is simply not redrawing properly, particularly if you have it as a usercontrol. You might try calling Invalidate() on the control after setting the Value to see if that's the problem.
I changed your self answer and made it like this:
public ProcessFailureForm()
{
InitializeComponent();
//Blah blah blah
dtFrom.HandleCreated += delegate //if you need sender or EventArgs use: delegate(object sender, EventArgs e)
{
dtFrom.Checked = true;
dtFrom.Value = DateTime.Today.AddDays(-7);
dtFrom.Checked = false;
};
}
Update:
Actually first i think like you, but after doing this, i tried and find out that the Checked state won't affect the process... so it can be reduced to:
public ProcessFailureForm()
{
InitializeComponent();
//Blah blah blah
dtFrom.HandleCreated += delegate //if you need sender or EventArgs use: delegate(object sender, EventArgs e)
{
dtFrom.Value = DateTime.Today.AddDays(-7);
};
}

Context.Items clears during page refresh/transfer

I am working on a class project and I've run into a problem I can't figure out. I have a feeling it's actually pretty easy, but I've been working on stuff so long I can't think straight anymore.
I have a login page that allows a user to login and pass 2 data items to the next page using Context.Items and Server.Transfer. Here is the code snippet:
Context.Items["preferred"] = true;
Context.Items["pageNum"] = 1;
Server.Transfer("ProductsShelf.aspx");
On the "ProductsShelf" page I can access those two items and use the data like so:
pageNumber = (int)Context.Items["pageNum"];
I am then using a switch-statement with pageNumber to display certain information:
switch (pageNumber)
{
case 1:
imgProd.ImageUrl = "assets/laptop.bmp";
lbl_Name.Text = "Laptop";
lbl_desc.Text = "This is a cheap laptop!";
lbl_price.Text = "199.99";
break;
}
Obviously there's other entries I'm omitting. What I want to do is click a next or previous button and use the event to change the Context.Items["pageNum"] data so the Page_Load() event uses different data in the switch-statement. Hope that makes sense. Here is one of the button click events:
protected void btn_Prev_Click(object sender, EventArgs e)
{
if (pageNumber == 1 || pageNumber == 2)
{
Context.Items["pageNum"] = 1;
}
else if (pageNumber == 3)
{
Context.Items["pageNum"] = 2;
}
Context.Items["preferred"] = preferredCustomer;
Server.Transfer("ProductsShelf.aspx");
}
The problem is that before the button click event fires, the form posts and clears the Context.Items and pageNumber values. This means that the button event if-statements never fire and it results in:
pageNumber = (int)Context.Items["pageNum"];
Being null, throwing an exception and making me very sad. So my question is, how can I go about retaining the values? Should I switch to Response.Redirect and have something like ?page=1 in the URL? Or will that clear too when the form posts? Hopefully I'm not doing this completely wrong.
If TL;DR, here's a quick summary:
Context.Items has 2 values passed with Server.Transfer
These values determine what's shown on the next page
The form clears Context.Items and variables before button click event fires
The values are null, the if-statement doesn't run, and the app throws an exception
Question: how should I go about retaining those values?
Thanks a lot. :)
HttpContext items can be used within one request only - it will be recreated for next request so your values are bound to lose. You should use view-state to preserve data across post-backs. In page load, you should check if data exists in context and then copy it to view-state. Then in button click events, you can read the data from view-state, put into the context items and do server.transfer.
Here's simple sample code:
private int PageNumber
{
get
{
var value = ViewState["pageNum"];
return null == value? 1: (int)value;
}
set
{
ViewState["pageNum"] = value;
}
}
private bool IsPreferredCustomer
{
get
{
var value = ViewState["preferred"];
return null == value? false: (bool)value;
}
set
{
ViewState["preferred"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
var preferred = Context.Items["preferred"];
if (null != preferred)
{
IsPreferredCustomer = (bool)preferred;
}
var pageNum = Context.Items["pageNum"];
if (null != pageNum )
{
PageNumber = (int)Context.Items["pageNum "];
}
}
Use the same PageNumber property in event code.

Categories

Resources