DynamicRadio Button - c#

I'm using C# in VS2005. For my application I need to create four radio buttons. My form looks like:
A(Radio Button)
B(Radio Button)
C(Radio Button)
D(Radio Button)
Submit (Button)
When a user clicks the submit button I need to know which radio button is checked. How can I determine this?

I would add all the radio buttons to a List<RadioButton> which would help you innumerate through them when the submit is checked to figure out which one is checked.

You can use the Checked Property of a RadioButton to see if it is checked:
bool isChecked = radA.Checked;
I often use the following helper function to get exactly the RadioButton which is checked:
public RadioButton GetCheckedRadioButton(Control container)
{
if (container == null) {
return null;
}
else if (container is RadioButton) {
return GetCheckedRadioButton(container.Parent);
}
else {
foreach (Control childControl in container.Controls) {
if (childControl is RadioButton) {
RadioButton radioBtn = (RadioButton) childControl;
if (radioBtn.Checked) {
return radioBtn;
}
}
}
return null;
}
}
Then, you can simply call this function using one of your controls or it's container, and do a switch statement, as such:
switch(GetCheckedRadioButton(radA)) {
case radA:
// radA is checked
break;
case radB:
// radB is checked
break;
}
Personally, I find it less verbose than the usual:
if(radA.Checked) {
//radA is checked
}
else if(radB.Checked) {
//radB is checked
}

If you know for sure that you will only need 4 RadioButtons, then I'd just write code similar to this.
if (Radio1.Checked) ...
if (Radio2.Checked) ...
if (Radio3.Checked) ...
...
If you know that only one of them can be checked at the same time (using a group box or some other mechanism) then you can optimise this by using "else if" instead.
However if there is a possibility that you will need more, this can get wasteful, so I agree with other answers, you should put them in a collection of your choice and use it to loop through them, to see which ones are checked.

Related

How do I change the properties of a control within a list dynamically (C# Winform)?

I have a list of checkbox controls
public List<CheckBox> _checkBoxes = new List<CheckBox>();
I add a certain amount during runtime using a switch case with the properties such as location and visibility.
This all works fine, but if I want the checkboxes to disappear or become checked at some point the GUI doesn't get updated, the list is updated with the new data but just not visually.
* I ended up doing this*
I created a bunch of checkboxes on the winfrom.Created a method which you can choose which ones are visible. Then created a method to fill a list of type checkbox. Then you can search the panel for control types, once you found the control, it can be manipulated. This isn't elegant and probably isn't the best way, but i am new to custom controls and winforms.
//search panel for checkboxes
foreach (Control c in panel1.Controls)
{
if (c is CustomControls.RoundedCheckBox)
{
CustomControls.RoundedCheckBox checkBox = c as CustomControls.RoundedCheckBox;
//if it is checked add to list
if(checkBox.Checked)
{
_checkBoxes.Add(checkBox);
}
}
}
If you have something like this:
_checkBoxes.Add(new CheckBox());
_checkBoxes[0].Parent = this;
then you should be able to manipulate your checkboxes from the list:
_checkBoxes[0].Checked = false;
But, the problem may occur if you do it in some kind of loop and want to see the results immediately.
In Windows there is something called message loop. Application simply works like that (pseudocode: TL;DR)
while(true)
{
message = GetFirstMessage();
if(message != null)
{
if(message.Id == CloseApplication)
break;
DispatchMessage(message);
RemoveFirstMessage();
}
}
So, application takes message from queue, then process it. A message is everything - button click, mouse move, paint... Everything.
So when a message is dispatched it looks for the control that should receive this message and then it does some work.
So, for example if you have something like that:
foreach(var ch in _checkBoxes)
{
ch.Checked = false;
DoSomeWorkThatTakesTime();
ch.Checked = true;
}
You won't see the change, because you are "trapped" in DispatchMessage. When you set Checked, you really sending a message. But this message cannot be Dispatched right now, because you are inside the foreach loop.
So the only thing you can do here is to tell your application - now, please DO READ message queue. In WinForms it's called "DoEvents", so this will do the work:
foreach(var ch in _checkBoxes)
{
ch.Checked = false;
Application.DoEvents();
DoSomeWorkThatTakesTime();
ch.Checked = true;
Application.DoEvents();
}

If statement condition where element equals element

I am testing a web page that contains a table. You can click a "Create new" link to add records to the grid. Once "Create New" is clicked, a dialog appears with some text boxes, another grid and a Cancel and Save button. You can then click a link to add a record to the dialog's grid which makes another dialog appear with text boxes and a Cancel and Save button. At my test level class, I currently click on these buttons then wait for the dialog's to open or close. I need to formulate a generic method which encompasses both the click and the wait, for each and every button. So instead of 2 lines of code at my test level to click an element and wait for a window, I would have one line of code that handles that. Below is my dilemma:
I need to be able to apply an If condition where a passed parameter of IWebElement equals a certain IWebElement, but it does not allow me to do this. The if statement doesn't find a match for some reason, so code inside the if statement never gets reached. Am I missing something? If not, is there a workaround?
NOTE: Using button.text == SaveOrganizationBtn.Text is a workaround, but would fail in my specific case, because some of these buttons might not have been loaded into the HTML for a certain test (i.e. A form has not been invoked), so the if statement fails. It would never be able to grab the Text property because it cant find the element in the first place.
Example code:
ClickButton(SaveNetworkBtn);
public void ClickButton(IWebElement button)
{
if (button == SaveOrganizationBtn)
{
SaveOrganizationBtn.Click();
WaitForOrganizationFormToClose();
}
if (button == SaveNetworkBtn)
{
SaveNetworkBtn.Click();
WaitForNetworkFormToClose();
}
Use the Equals() method for your scenario. == will not work for this. you need to check it as if(button.Equals(SaveOrganizationBtn)). The result for this will be true, if it is the same object else it will return false.
I hope, it will help you.
I found a solution, however awkward it might look. I will mark this as the answer if no one else provides a better solution.
ClickButton(SaveNetworkBtn);
public bool ClickButton(IWebElement button)
{
bool buttonClicked = false;
if (Driver.FindElements(By.Id("SaveOrg-Button-ID")).Count > 1)
{
if (button == SaveOrganizationBtn)
{
SaveOrganizationBtn.Click();
WaitForOrganizationFormToClose();
buttonClicked = true;
return buttonClicked;
}
}
if (Driver.FindElements(By.Id("SaveNet-Button-ID")).Count > 1)
{
if (button == SaveNetworkBtn)
{
SaveNetworkBtn.Click();
WaitForNetworkFormToClose();
buttonClicked = true;
return buttonClicked;
}
}
return buttonClicked;
}
I don't fully understand your scenario but it seems like something like this might work... you will have to decide. Instead of passing in button, just check if the button exists on the page, if it does... click it.
ClickButton();
public bool ClickButton()
{
IReadOnlyCollection<IWebElement> saveOrg = Driver.FindElements(By.Id("SaveOrg-Button-ID"));
if (saveOrg.Any())
{
saveOrg.ElementAt(0).Click();
WaitForOrganizationFormToClose();
return true;
}
IReadOnlyCollection<IWebElement> saveNet = Driver.FindElements(By.Id("SaveNet-Button-ID"));
if (saveNet.Any())
{
saveNet.ElementAt(0).Click();
WaitForNetworkFormToClose();
return true;
}
return false;
}
NOTE: .Any() works the same as .Count > 0, I found it a while back and like it a little better but it's a personal preference.

Finding which textbox is empty

I have short windows program I use to add information quickly. But now I'm trying to enhance it.
Was looking for a more efficient want to check for empty text boxes and if the box was empty to find which one it was and set the focus back to only that box. Currently I loop through all of them and check to see if any box was empty if it is just display a message. But have to look to see which box is missing text. Heres the code:
bool txtCompleted = true;
string errorMessage = "One or more items were missing from the form";
foreach(Control c in Controls)
{
if (c is TextBox)
{
if (String.IsNullOrEmpty(c.Text))
{
txtCompleted = false;
}
}
}
if (txtCompleted == false)
{
MessageBox.Show(errorMessage);
}
Your approach using foreach looks promising to me. Howver you can use LINQ as well
if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)) {
...
}
You can use the focus() method to set the focus to the empty text box.
Set the focus on the control while in your loop, then break when done.
foreach(Control c in Controls)
{
if (c is TextBox)
{
if (String.IsNullOrEmpty(c.Text))
{
txtCompleted = false;
c.Focus();
MessageBox.Show(errorMessage);
break;
}
}
}
To get a reference to the empty textbox you use almost the same solution as R.T. presents, but use FirstOrDefault instead:
var emptyTextBox = Controls.OfType<TextBox>().FirstOrDefault(t => string.IsNullOrEmpty(t.Text)
if (emptyTextBox != null)
{
// there is a textbox that has no Text set
// set focus, present error message etc. on emptyTextBox
}

Setting a default value on form start for DomainUpDown Control

I have some DomainUpDown controls in my winforms application. I cant find an option to set the default value for them on start up. Is there a way to do this?
At the moment, I have an enum type like this:
public enum ComparisonMode {Settings, Readings};
And I have set the SelectedItemChanged event of each DomainUpDown control to something like this:
private ComparisonMode ComparisonA; //enum to hold state of upDownA
private void upDownA_SelectedItemChanged(object sender, EventArgs e)
{
switch (upDownA.Text)
{
case "Settings":
ComparisonA = ComparisonMode.Settings;
break;
case "Readings":
ComparisonA = ComparisonMode.Readings;
break;
}
}
When I start the application, all the domainupdown controls have nothing selected, so user must set each of them to either Settings or Readings befor starting to work with the application.
How can I set for all of them a default value of for example (Readings)? I am thinking of looping over the controls (As they are inside a TableLayoutPanel).
I came up with this idea so far, It seems to work:
foreach (TableLayoutPanel tlp in tableCriterias.Controls)
{
foreach (Control ctrl in tlp.Controls)
{
var dud = ctrl as DomainUpDown;
if (dud != null)
{
dud.DownButton(); dud.DownButton();
//Going down 2 times to select default value
}
}
}
I do the above loop in the Load event of my form, but it makes everything very slow in the beggining since I have like 100 of these UpDown Controls.
No need to do this, you can set the SelectedIndex like this :
DomainUpDown dd = new DomainUpDown();
dd.Items.Add("settings");
dd.Items.Add("Reading");
dd.SelectedIndex = 0; // this will make sure you get the first item selected

Change text for multiple buttons in one operation

I have a form which consist of many buttons (50+) and they all have the same name except for the suffix number. (btn_0, btn_1, btn_3, etc.)
I want to change the text of those buttons in one operation.
Is there a way of treating buttons like arrays?
btn_[i].Text = "something"?
Maybe execute a string?
"btn_{0}.Text=\"something\""
you will need to access each button at a time to do this.
Do it in a loop like this
foreach(var btn in this.Controls)
{
Button tmpbtn;
try
{
tmpbtn = (Button) btn;
}
catch(InvalidCastException e)
{
//perform required exception handelling if any.
}
if(tmpbtn != null)
{
if(string.Compare(tmpbtn.Name,0,"btn_",0,4) == 0)
{
tmpbtn.Text = "Somthing"; //Place your text here
}
}
}
Have a look for the Overloaded Compare method used.
if you know how many buttons there is you can make a loop. though it's not perfect and there might be a smarter way to do this but I can't see why I wouldn't work
Don't know specifics but the pattern probably goes like this
for each(Control c in this.controls)
{
if(c is Button) //Check the type
{
Button b = c as button;
b.Text="new text";
}
}
or use excel with its autofil and text concatenation abilities to do it as a block of text. eg
btn1.text="hi";
btn2.text="world";
...
why not use jquery to rename all at once?
jQuery("form :button").attr('value','Saved!')

Categories