radiobutton in different tabs stay checked - c#

I have a winforms app witch has a tabcontrol and in each tab some radiobuttons the code works but there is a problem.
If i go to a tab and check a radiobutton and after go to another tab and check another radiobutton the previous button stays selected.
Is there a way to whenever i check a radiobutton on another tab the app unchecks the previous button on the other tab?
The code i use is:
if (!radioButton1.Checked && !radioButton2.Checked && !radioButton3.Checked && !radioButton4.Checked)
{
MessageBox.Show("Please select an option!", "Warning");
return;
}
else if (radioButton1.Checked)
{
build build = new build("#../Images/x1.jpg", "definition", "c1");
build.ShowDialog();
}
else if (radioButton2.Checked)
{
build build = new build("#../Images/x2.jpg", "definition2","c2");
build.ShowDialog();
}
etc...
Thanks in advance,

If I understand your question correctly, you just want to uncheck all other radiobuttons? You could just use to uncheck them:
radioButtonX.Checked = False;

Have a look at the events for the tabControl. Make an event for the tabPageX.Enter and clear the radio button (radioButtonX.Checked = false) in that.
Really, unless this is a throw-away test software, I'd recommend looking into a more sophisticated architecture like Model View Presenter. (for what its worth).

Related

How to scroll the area like drop down and list box in Winapp driver with c#?

I am automating desktop application where i have list box in which many items are listed and i want to click on the item which is hidden and that item will shows after scrolling the list. Which code will work for this? I am working with WinAppdriver, appium , c# and MSTest.
I need such lines of code with which list will scroll till the element finds.
Someone guide me this solution and it is working for me now. So, i am posting answer here.
The List Box has accessibility item "lbStates".
We want to click the item "NC" in it which has the text "NC".
In this case the "Displayed" property of most of the list items will be false.
We can click the down button in the ListBox to scroll down or We can press the keys buttons and test if the value which we want to click is displayed or not.
It is possible that the value we want to click is not present in the listbox, in such case our code will go in a deadlock and for that purpose I've introduced the integer maxClicks.
[TestMethod]
public void ListBoxTest()
{
//lbStates
var lbStates = sessionWinForm.FindElementByAccessibilityId("lbStates");
var allListItems = lbStates.FindElementsByTagName("ListItem");
var valueToClick = "NC";
var maxClicks = 10;
foreach(var ali in allListItems)
{
Debug.WriteLine($"{ali.Displayed} - {ali.Text}");
if(ali.Text.Equals(valueToClick) && !ali.Displayed)
{
var downButton = lbStates.FindElementByAccessibilityId("DownButton");
var listItemToClick = lbStates.FindElementByName(valueToClick);
while(!listItemToClick.Displayed && (maxClicks-- > 0))
{
downButton.Click();
listItemToClick = lbStates.FindElementByName(valueToClick);
if (listItemToClick.Displayed)
{
listItemToClick.Click();
}
}
}
}
}
you can just find your element with winappdriver and it will scroll itself to the element , i have automated a windows application with had a input box that accept a file and had a browser button after clicking browse button it itself scroll till the element is found.
Regarding the code you may just try finding the element normal like it is in view .
Hope this helps. :)

Best way to put two Controls on each other and manage their visibility - C#

just for example:
I have two text boxes, both should be in the same location of the form and both are in the same size.
I need to make visible one of them due to a contrition.
OK, everything it perfect:
if (condition)
{
txtBox1.Visible = false;
txtBox2.Visible = true;
}
else
{
txtBox1.Visible = true;
txtBox2.Visible = false;
}
but there is a small problem in design mode! I can not handle and select one of text boxes easily in design mode, one is send to back and one is send to front and accessing them will cause troubles!
How can I manage such situations with control ordering and design? Is my way putting two control on each other right? is there any better way?
One option is to put one TextBox in the location where you want it (let's say textbox1 will be in the right location) and put the other textbox nearby on the designer. This way you can easily select them at design time.
Then, in your Form_Load event, you just put textBox2 at the same location as textbox1:
private void Form1_Load(object sender, EventArgs e)
{
textbox2.Location = textbox1.Location;
textbox2.Visible = false;
A better question might be, why are you doing this? A textbox is a textbox, and you already have one in the position that you want. A textbox is there to collect user input. Why show and hide another one in the same place? The user won't know the difference, and your program state will presumably not care the name of the control in which the user has entered data...
There are multiple ways you can select a single control out of these two. First, by hitting the tab key until you land up on the control you want to select. To optimise the selection you need to first select a different control whose tab order is smaller than both of these.
Second, you can select any control and hit F4 to open properties window. On top of properties window there is a dropdown where you get to see the Control name. You can then choose the control of your choice from this properties window and make changes to properties.
you can do somethings like this, Here is the sample code,
txtBox1.Visible = false;
Point loc = new Point(50, 60); //new location point
txtBox2.Location = loc; //changing location of txtBox2
txtBox2.Visible = true;

Button with a CheckBox

We have a requirement to create a button with a CheckBox embedded in it.
After trying various option, we found an option of having a CheckBox with an appearance of Button but that does not suffice as we need to have an event being fired when the Button is being clicked but CheckBox.Checked state will be used for other modifications.
Can someone guide me on how to proceed with this task?
If you really want to you can do something like this:
checkBox1.Parent = button1; // make it large enough
checkBox1.Location = new Point(5, (button1.Height - checkBox1.Height) /2 + 1) ;
checkBox1.TextAlign = ContentAlignment.MiddleLeft;
button1.TextAlign = ContentAlignment.MiddleRight;
Make sure to set Texts and alignments for both to prevent clashes. Also make sure to test if they act as intended..!
You can place the Button wherever you want, align it to the right or clear its Text..
Of course you can wrap it in a class, if you need it repeatedly..

How to select items in a ListView?

This is my code:
for (int i = 0; i < 30; i++)
{
FileListView.Items.Add(new ListViewItem(new[] { "asd1", "asd2" }));
if (i < 10)
{
FileListView.Items[i].Selected = true;
}
}
FileListView.ItemDrag += new ItemDragEventHandler(FileListView_ItemDrag);
but when I Run the application, I can't see the first 10 items selected. For see them, I need to click on one of them, and they will highlights (but of course deselected immediatly, since it is like click on a single row).
How can I preselect 10 items? So a user see them selected and then can click to drag/drop to some destination...
The items are being selected but the control is not activated. Use FileListView.Select() to activate the control.
It sounds like your ListView is not focused so when you select the items they won't highlight.
You can either focus the control before hand like this:
FileListView.Focus();
Or what's probably better is to disable the HideSelection property. This allows the ListView to display selected items when not focused.
FileListView.HideSelection = false;
Edit: With OPs new information that they need to show blue, give keyboard focus to the control once you're done:
FileListView.Select();
Did you set the multiselect property with the designer or by code ?
FileListView.MultiSelect=true ;
Try also:
FileListView.Invalidate() after the loop.

How to programmatically set selected Panorama item in WP7

I'm using a panorama control in a WP7 app. One of the PanoramaItems takes you to another page, which then allows you send an email through the EmailComposeTask. If you don't select to send the email and press the back button, the Panorama returns to the item you last selected. However, if you do select to send an email (and therefore leave the app), it does not return to the previously selected PanoramaItem. Instead, it returns to the first item in the Panorama. I tried keeping track of the selected index and setting it, but I got an error saying the SelectedIndex is not settable. This is confirmed on MSDN documentation http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.panorama.selectedindex%28VS.92%29.aspx
Is there any way to manually set the selected index/item on a panorama? If not, is there a way for it to remember what was selected, even if the user leaves the app to compose an email?
I'm not sure if you can programmatically force an animation to another PanoramaItem, but you can change the Panorama.DefaultItem.
So you might have 3 PanoramaItem's and on the OnNavigatedTo() handler, change the default item via:
panoramaControl.DefaultItem = panoramaControl.Items[indexToSet];
This should help when you recover from a tombstone.
You could try the solution posted by Silicon Shark in this thread. It's noted to work, but only on the initial display - which shouldn't be a problem for your requirements of restoring state after tombstoning.
How to programmatically set the visible item in a Panorama control?
You can get the currently active page from the panorama's SelectedIndex property.
Unfortunately setting DefualtItem is only an approximation to solving this problem, which you may have discovered already.
Edit: Be aware that setting DefaultItem, changes which page of the panorama is the first page. It's a subtle difference, but you will see how it matters looking at the positioning of the heading and the wrap around of the background image.
Here is a solution. It does work as expected and does not rearrange your panorama, so your user interface is consistent.
pan.SetValue(Panorama.SelectedItemProperty, panoramaItem);
Panorama temp = pan;
LayoutRoot.Children.Remove(pan);
LayoutRoot.Children.Add(temp);
LayoutRoot.UpdateLayout();
this is not a perfect solution in that it does not slide nicely like panorama should, and it is probably not very efficient, but on the other hand you are not changing the default item so your user interface stays consistent.
I tested solutions listed here without success. Here is what I did that works like a charm!
PanoramaItem panItem = (PanoramaItem)panorama.Items[1];
panorama.Items.Remove(panItem);
panorama.Items.Insert(0, panItem);
You need to remove the panel from the list and re-inserting it at the desired position!
Set new selected item by
pan.SetValue(Panorama.SelectedItemProperty, pan.Items[newSelectedItem]);
However, it work only on the initial so my idea is let the panorama control re-init when we change the selected item. This is my code, just add this after Panorama.SelectedItem changing.
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Collapsed;
pan.SetValue(Panorama.SelectedItemProperty, pan.Items[(curIndex + 1) % pan.Items.Count]);
pan.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Visible;
But there is not transition effect now! Although, you can create your self.
It work great for me, this page also create a effect for sliding right http://xme.im/slide-or-change-panorama-selected-item-programatically
I'm using this model to change to a pivot when the device goes into landscape view, I'll probably end up extracting the current item to the application state. The panorama is a no-go in landscape orientation.
private int hub_page_index;
protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
base.OnOrientationChanged(e);
if (panorama.Visibility == Visibility.Visible)
{
hub_page_index = panorama.SelectedIndex;
}
else if (pivot.Visibility == Visibility.Visible)
{
hub_page_index = pivot.SelectedIndex;
}
if (e.Orientation == PageOrientation.Landscape
|| e.Orientation == PageOrientation.LandscapeLeft
|| e.Orientation == PageOrientation.LandscapeRight)
{
// Display Pivot in Landscape orientation
pivot.SetValue(Pivot.SelectedItemProperty, pivot.Items[panorama.SelectedIndex]);
panorama.Visibility = Visibility.Collapsed;
pivot.Visibility = Visibility.Visible;
}
else
{
// Display Panorama in Portrait orientation
panorama.SetValue(Panorama.SelectedItemProperty, panorama.Items[pivot.SelectedIndex]);
pivot.Visibility = Visibility.Collapsed;
panorama.Visibility = Visibility.Visible;
}
}

Categories

Resources