I may confusing with the terminology, but here is what I'm trying to do:
I have a move function which will end up moving selected items from one listBox to another. There are three list boxes with two right and left arrow buttons in between each to move the first list box items to the middle, the middle back to the first, etc.
My function accepts the different button names via sender and in the switch statement, I want to choose which listBox are the selected items going to be send from and where they will be sent to. If that makes sense.
The while loop at the bottom is going to perform the actual move, depending on what is set for the "to" and "from" listBoxes.
My question is how can I in the scope of this function reference the names of the three existing listBoxes in each of the cases of the switch statement? I know initializing a new listBox like I've done is wrong and will just create some more listBoxes. Maybe for this case the easiest thing is to explicitly put the while loop in each of the case statements, but for the future in a more complex scenario, I still would like to know how this is done.
private void move(object sender, EventArgs e)
{
Button thisButton = sender as Button;
ListBox to = new ListBox();
ListBox from = new ListBox();
switch (thisButton.Name)
{
case "queueToProgressButton":
to.Name = "queueListBox";
from.Name = "progressListBox";
break;
case "progressToQueueButton":
to.Name = "queueListBox";
from.Name = "progressListBox";
break;
case "progressToCompletedButton":
to.Name = "queueListBox";
from.Name = "progressListBox";
break;
case "completedToProgressButton":
to.Name = "queueListBox";
from.Name = "progressListBox";
break;
}
while (from.SelectedItems.Count > 0)
{
to.Items.Add(from.SelectedItem);
from.Items.Remove(from.SelectedItem);
}
}
You should be using references to the existing list boxes, not allocating new ones. Also, your four branches of the switch are identical in the code you posted; I don't think that's what you intended. I made adjustments to the code based on what I think you meant to do in the switch.
Try something like this instead:
private void move(object sender, EventArgs e)
{
Button thisButton = sender as Button;
ListBox toListBox, fromListBox;
switch (thisButton.Name)
{
case "queueToProgressButton":
toListBox = progressListBox; // notice no "", but actual references
fromListBox = queueListBox;
break;
case "progressToQueueButton":
toListBox = queueListBox;
fromListBox = progressListBox;
break;
case "progressToCompletedButton":
toListBox = completedListBox;
fromListBox = progressListBox;
break;
case "completedToProgressButton":
toListBox = completedListBox;
fromListBox = progressListBox;
break;
// Should add a default just in case neither
// toListBox or fromListBox is assigned here.
}
while (fromListBox.SelectedItems.Count > 0)
{
toListBox.Items.Add(fromListBox.SelectedItem);
fromListBox.Items.Remove(fromListBox.SelectedItem);
}
}
Related
I'm creating an application to scan barcode tickets. When you start the app a list of available shows has to be shown on the screen. To get all the available shows I'm using a webservice which returns me a List<Event>. How do I create a list of buttons with each button representing a show/event from inside the xaml.cs? When clicking the button a new page will be shown where the user can scan the tickets from that show. I'm pretty new to Xamarin.Forms and I quite don't understand how to use the paging/content views. I already searched a lot but the closest to what I need was this: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/get-started-with-xaml?tabs=vswin
Unfortunatly it only shows how to add a certain amount of controls. And not a dynamicly generated amount.
Many thanks in advance!
In xaml insert a stacklayout where you want your buttons to appear. Remember you can also play whith its Orientation and Spacing properties. So:
<StackLayout x:Name="MyButtons"/>
Then in code-behind generate your dinamic buttons list. Put the code in constructor AFTER InitializeComponent(); or somewhere else:
var myList = new List<*Event or something*>(); //your list here
MyButtons.Children.Clear(); //just in case so you can call this code several times np..
foreach (var item in myList)
{
var btn = new Button()
{
Text = item.*your property*, //Whatever prop you wonna put as title;
StyleId = item.*your ID property* //use a property from event as id to be passed to handler
};
btn.Clicked += OnDynamicBtnClicked;
MyButtons.Children.Add(btn);
}
Add a handler:
private void OnDynamicBtnClicked(object sender, EventArgs e)
{
var myBtn = sender as Button;
// who called me?
var myId = myBtn.StyleId; //this was set during dynamic creation
//do you stuff upon is
switch (myId)
{
case "1": //or other value you might have set as id
//todo
break;
default:
//todo
break;
}
}
Once again I am in need of your assistance. I have two combobox's one called cmbRFR and one called cmbSubRFR. The items in the cmbRFR are:
Null
POSITIONING
ARTEFACT
PATIENT ID
EXPOSURE ERROR
TEST IMAGES
I need to set it up so that when the user selects one of the items in cmbRFR, it changes the items displayed in cmbSubRFR. cmbSubRFR should work as follows.
When user selects Null, the combobox should also display Null/a blank item.
When user selects POSITIONING:
Anatomy cut-off
Rotation
Obstructed view
Tube or grid centering
Motion
When user selects ARTEFACT the combobox should also display ARTEFACT.
When user select PATIENT ID:
Incorrect Patient
Incorrect Study/Side
User Defined Error
When user select EXPOSURE ERROR:
Under Exposure
Over Exposure
Exposure Malfunction
When user selects TEST IMAGES:
Quality Control
Service/Test
I have no code to provide for this one as I have no idea how to go about doing this. I have looked around at some other questions that are similar to this however I have found nothing that might help me.
Any suggestions would be helpful.
public partial class Form1 : Form
{
string cmbRFR_item;
public Form1()
{
InitializeComponent();
}
private void change_cmbSubRFR_items()
{
cmbSubRFR.Items.Clear();//Clear all items in cmbSubRFR comboBox.
switch (cmbRFR_item)//Adding your new items to cmbSubRFR.
{
case "Null":
cmbSubRFR.Items.Add("Null/a blank item");
cmbSubRFR.Text = "Null/a blank item";
break;
case "POSITIONING":
cmbSubRFR.Items.Add("Anatomy cut-off");
cmbSubRFR.Items.Add("Rotation");
cmbSubRFR.Items.Add("Obstructed view");
cmbSubRFR.Items.Add("Tube or grid centering");
cmbSubRFR.Items.Add("Motion");
cmbSubRFR.Text = "";
break;
case "ARTEFACT":
cmbSubRFR.Items.Add("ARTEFACT");
cmbSubRFR.Text = "ARTEFACT";
break;
case "PATIENT ID":
cmbSubRFR.Items.Add("Incorrect Patient");
cmbSubRFR.Items.Add("Incorrect Study/Side");
cmbSubRFR.Items.Add("User Defined Error");
cmbSubRFR.Text = "";
break;
case "EXPOSURE ERROR":
cmbSubRFR.Items.Add("Under Exposure");
cmbSubRFR.Items.Add("Over Exposure");
cmbSubRFR.Items.Add("Exposure Malfunction");
cmbSubRFR.Text = "";
break;
case "TEST IMAGES":
cmbSubRFR.Items.Add("Quality Control");
cmbSubRFR.Items.Add("Service/Test");
cmbSubRFR.Text = "";
break;
}
}
private void cmbRFR_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbRFR_item!= cmbRFR.SelectedItem.ToString())//This will control your changes in cmbRFR about selected item and call change_cmbSubRFR_items()
{
cmbRFR_item = cmbRFR.SelectedItem.ToString();
change_cmbSubRFR_items();
}
}
}
There are two ways to go about solving this. First the "correct way", which is to set the DataSource property. Create lists for each combobox, like this:
var _positioningItems = new List<string> { "Anatomy cut-off", "Rotation", "Obstructed view" };
var _patientIdItems = new List<string> { "Incorrect Patient", "Incorrect Study/Side", "User Defined Error" };
Then subscribe to the OnSelectedIndexChange event on the cmbRFR combobox and then in the event handler, set the DataSource to the appropriate list.
The other way to do it, which I'm not in favor of, is to create comboboxes for each cmbRFR item, then hide them all and only show the appropriate one to the user. Subscribe to the OnSelectedIndexChange event on the cmbRFR combobox and hide/show the appropriate combobox.
I'm struggling with WinForms. I have a GroupBox which wraps three RadioButtons. I added them using the design view and inside the constructor I tag every button to corresponding enum value like
public MyApp()
{
radioBtnBasic.Tag = UserChoiceEnum.Basic;
radioBtnLite.Tag = UserChoiceEnum.Lite;
radioBtnStandard.Tag = UserChoiceEnum.Standard;
}
Inside my class I have property property of type Dictionary which uses this enum as a key, so I want when the user clicks on winform button to recognize which radio button is checked and to assign to that dictionary.
I've found how to fetch checked option
var choice = grpBox1.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
Do I need to use switch statement to recognize which Enum is checked or is there some better way?
You get the UserChoiceEnum by:
RadioButton choice = grpBox1.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
UserChoiceEnum userChoice = (UserChoiceEnum)choice.Tag;
If you set Tag you can simply get it back whenever you need. Note that you need to cast it to original type. Something like:
var choiceAsEnum = (UserChoiceEnum)choice.Tag;
created enum :
public enum SearchType
{
ReferenceData = 0,
Trade = 1,
}
Then use SelectedIndexChanged event of radioGroup control.
private void RadioTypes_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.RadioTypes.SelectedIndex < 0) return;
SearchType n = (SearchType)this.RadioTypes.SelectedIndex;
switch (n)
{
case SearchType.ReferenceData:
break;
case SearchType.Trade:
break;
}
}
I have a form in C# with a drop down box. When that drop down box gets changed, I want it to load a custom user control into a panel. I get no compile errors, no runtime errors, but I also get no visible user control. The idea here is that each user control understands the configuration for a different scenario. I want to encapsulate each scenario in its own control and the drop down box allows the user to select the scenario, which loads the control for the user to perform configuration. By adjusting the options in the drop down, I can customize what scenarios a given customer has. As I said, my problem is I cannot get any of the controls to become visible. This is the code I am using for the drop down index change event handler:
private void ddlCollectorType_SelectedIndexChanged (object sender, EventArgs e)
{
m_currentControl = null;
pnlDeviceConfig.Controls.Clear ();
switch ((string) ddlCollectorType.SelectedItem)
{
case "SEL-421":
SEL421ASCIIControl s421 = new SEL421ASCIIControl (this);
m_currentControl = s421;
pnlDeviceConfig.Controls.Add (s421);
break;
case "SEL-421 (FTP)":
break;
case "GE D60":
GED6061850Control geD60 = new GED6061850Control (this);
m_currentControl = geD60;
pnlDeviceConfig.Controls.Add (geD60);
break;
case "GE D60 (TFTP)":
break;
case "MiCOM P442":
break;
}
}
I've only created a couple of the user controls so far, hence the empty case statements. When I make selections that should show me something, I get nothing (confirmed in the debugger that I am hitting the case statement body). Any help will be greatly appreciated!
To follow up on my comment, I'm guessing that the position and/or dimensions of your control are not set.
Try something like:
...
SEL421ASCIIControl s421 = new SEL421ASCIIControl (this);
m_currentControl = s421;
pnlDeviceConfig.Controls.Add (s421);
// TODO: Set real size and position.
s421.Left = 0;
s421.Top = 0;
s421.Width = 100;
s421.Height = 50;
break;
...
You also may use the Dock and Anchor properties of your control.
1 You can replace your Menu with PlaceHolder, he is ideal control for adding controls
here an example : http://www.developerfusion.com/code/3826/adding-controls-to-placeholders-dynamically/
2 And for your case you can try to adjust visible property of panel to true
I am using student record entry window form. I want that after submitting data, all fields of form(i.e radio button, combobox etc) and messages(warning and successful) should be reset so that new user can add data.
is their any built in function to reset form in csharp?
or I have to write clear method for this?
or can I regenerate the form?
You must first clear the controls and then use InitializeComponent method to work perfectly.
this.Controls.Clear();
this.InitializeComponent();
This you can achieve in two ways:-
1) you can clear the fields of the form in one method. say public void clear() And whenever you want to clear the form simply call this method.
2) In second way you can destory the instance of the form and generate the new instance of the same form and then show this.
I will recomeded 1st one for you.
This is what I used to create in my every page
private void ClearControls()
{
try
{
txtUserName.Text = string.Empty;
//txtPassword.Text = string.Empty;
txtFName.Text = string.Empty;
txtMName.Text = string.Empty;
txtLName.Text = string.Empty;
lblUserType.Text = string.Empty;
btnSave.Text = "Save";
fnMessage(false, "");
}
catch (Exception ex)
{
fnMessage(true, ex.Message);
}
}
Thanks
Implement data binding to a given object (good starting point here)
For resetting the form, create a new object and the binding will do it for you.
HTH
You can either re-create the form instance, or perhaps try something similar to this (untested):
foreach (Control ctl in this.Controls)
{
switch (ctl.GetType().ToString())
{
case "TextBox":
ctl.Text = null;
break;
case "ComboBox":
ctl.Text = null;
break;
}
}
Clearly, you can include as many different types of control as you wish and introduce other critieria (i.e. where control name begins with 'xyz' or where control resides within a particular panel).
Compared to other suggestions, the advantage of this approach is that if you have dozens of the same control type (typically textboxes), a few lines of code cover the lot. Additionally, if you add more controls of the covered types, you don't need to revisit the code to update it. Perhaps you could even create it as an extension method of your forms?
If form creation doesn't take too much resources it is easier to create new instance.
As i had same problem and my from was having nested controls. I tried CJM's method and it worked however i had to write a recursive function because of controls nesting (tab controls, containers, user controls etc)
Try out the following snippet if you want to clear whole form recursively
private void clearRecursive(Control control)
{
foreach (Control subcontrol in control.Controls)
{
switch (subcontrol.GetType().ToString().Replace("System.Windows.Forms.", ""))
{
case "TextBox":
TextBox textBox = (TextBox)subcontrol;
textBox.Text = null;
break;
case "ComboBox":
ComboBox comboBox = (ComboBox)subcontrol;
if (comboBox.Items.Count > 0)
comboBox.SelectedIndex = 0;
break;
case "CheckBox":
CheckBox checkBox = (CheckBox)subcontrol;
checkBox.Checked = false;
break;
case "RadioButton":
RadioButton radioButton = (RadioButton)subcontrol;
radioButton.Checked = false;
break;
case "TreeView":
TreeView tv = (TreeView)subcontrol;
foreach (TreeNode node in tv.Nodes)
{
node.Checked = false;
CheckChildren(node, false);
}
break;
case "ListBox":
ListBox listBox = (ListBox)subcontrol;
listBox.Items.Clear();
break;
case "CheckedListBox":
CheckedListBox chklstbox = (CheckedListBox)subcontrol;
for (int i = 0; i < chklstbox.Items.Count; i++)
{
chklstbox.SetItemCheckState(i, CheckState.Unchecked);
}
break;
}
if (subcontrol.HasChildren)
clearRecursive(subcontrol);
}
}
Call InitializeComponent method and Form_Load method.