how to change class member visibility? - c#

I have a class with a combobox inside. I want to add items to this combobox from a different class, but I cannot see it.
I've instantiated the class (with the combobox) using 'new', i.e.:
check_reg _check_reg = new check_reg();
but in my second class I see only the
_form1.Choose_Quar_SelectedIndexChanged paramter, which is the handler shown when I double click the combo box in the form, it does not help me add the items.
I'm sure it's a basic question... so please help me with it.
Thx!

For sure you can create a public instance method inside the class "check_reg" to add items to the combobox.
Something like this:
public void AddItem(ListItem li)
{
ddl.Items.Add(li);
}
And you can use it like this:
check_reg _check_reg = new check_reg();
_check_reg.AddItem(new ListItem("Text", "Value"));
Hope this helps.
Cheers

You should use your combobox as a property of your class with public modifier
So first thing go to your YourPage.designer.cs and remove the declaration of the combobox e shift it to the code behind of the page.
change from
protected global::System.Web.UI.HtmlControls.HtmlGenericControl combobox;
to
public global::System.Web.UI.HtmlControls.HtmlGenericControl combobox;
after this you will be able to see the combobox as a public property of the class where it is declared

Related

Trying to bind a combobox to an enum in C#

I have a class that contains a property that is an enum:
public RaTypes RaBucket1Type { get; set; }
My enum is:
public enum RaTypes
{
Red,
Yellow
}
I was able to bind a form's combobox data-source to the enum so that when I click on the drop-down, I see the enumerations:
cmbBucket1Type.DataSource = Enum.GetValues(typeof(RaTypes));
When I load the form, I would like to populate the combo-box with the existing value. I have tried the following:
cmbBucket1Type.DisplayMember = "TradeType";
cmbBucket1Type.ValueMember = "TradeEnumID";
cmbBucket1Type.SelectedValue = EditedAlgorithm.RaBucket1Type;
But this did not work.
Also, I'm not sure I have implemented the ValueChanged event handler correctly either:
EditedAlgorithm.RaBucket1Type = (RaTypes)((ComboBox)sender).SelectedItem;
Can someone help me understand:
How to set the combobox to current value, and
How to handle the event handler so I can set the property to whatever was selected?
Thanks
-Ed
UPDATES
I have tried
cmbBucket1Type.SelectedIndex = cmbBucket1Type.FindString(EditedAlgorithm.RaBucket1Type.ToString());
and
cmbBucket1Type.SelectedItem = EditedAlgorithm.RaBucket1Type;
Neither works.
I think you're using the terminology a little differently than normal, which makes it difficult to understand.
Normally, the terms Add, Populate, and Select are used to mean the following:
Add - Add an item to the existing set of items in the combo box.
Populate - Initialize the combo box with a set of items.
Select (Display) - Choose one among many items in the combo box as the selected item. Normally this item will be displayed in the combo box visible area.
Having cleared that up, I assume following is what you want to do.
Initially populate the ComboBox with a set of values. In your case, values of RaType Enum.
Create an instance of your class which contains the property mentioned. Since you didn't name that class I'll simply name it SomeClass.
Initialize the RaBucket1Type property of the said class instance with an enum value of your choice. I'll initialize it to Yellow.
Have the ComboBox select the said value at start up.
After Form_Load, at any given time, if the user changes the value of the ComboBox, have the change reflected in your class instance property.
For that, I would do something like this:
public partial class MainForm : Form
{
// Your class instance.
private SomeClass InstanceOfSomeClass = null;
public MainForm()
{
InitializeComponent();
// Initialize the RaBucket1Type property with Yellow.
InstanceOfSomeClass = new SomeClass(RaTypes.Yellow);
// Populating the ComboBox
comboBox1.DataSource = Enum.GetValues(typeof(RaTypes));
}
// At selected index changed event
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the selected value.
var selected = comboBox1.SelectedValue;
// Change the `RaBucket1Type` value of the class instance according to the user choice.
InstanceOfSomeClass.RaBucket1Type = (RaTypes)selected;
}
private void MainForm_Load(object sender, EventArgs e)
{
// At form load time, set the `SelectedItem` of the `ComboBox` to the value of `RaBucket1Type` of your class instance.
// Since we initialized it to `Yellow`, the `ComboBox` will show `Yellow` as the selected item at load time.
if (InstanceOfSomeClass != null)
{
comboBox1.SelectedItem = InstanceOfSomeClass.RaBucket1Type;
}
}
}
public enum RaTypes
{
Red,
Yellow
}
public class SomeClass
{
public RaTypes RaBucket1Type { get; set; }
public SomeClass(RaTypes raTypes) { RaBucket1Type = raTypes; }
}
Please do keep in mind this is a basic example to show you how to handle the situation and not a complete finished code. You'll need to do a bunch of error checks to make sure class instances and selected items are not null etc.
I FOUND MY ANSWER:
I had the SelectedIndexChanged event pointing to my event handler which means that when I "added" items to the ComboBox using:
comboBox1.DataSource = Enum.GetValues(typeof(RaTypes));
it was triggering the event handler, and resetting my class property. My event handler was this:
var selectedValue = cmbBucket1Type.SelectedValue;
So the simple solution was to:
Remove the hard-coded event handler from the Visual Studio GUI.
Add the following event handler in code AFTER I assign the DataSource
bucketType1.SelectedIndexChanged += BucketTypeChanged;
This worked.
THANK YOU ALL FOR HELPING!!
-Ed
You can set the selectedValue like this:
cmbBucket1Type.SelectedValue = EditedAlgorithm.RaBucket1Type;
And you can handle the selected value when the combo change like this:
private void cmbBucket1Type_SelectedValueChanged(object sender, EventArgs e)
{
var selectedValue = cmbBucket1Type.SelectedValue;
}

Deriving from ComboBox

I need to derive a class from ComboBox and change its Items property. Here is my code:
public class MyComboBox2 : ComboBox
{
private MyObjectCollection MyItems;
public MyComboBox2()
{
MyItems = new MyObjectCollection(this);
}
//new public ComboBox.ObjectCollection Items
new public MyObjectCollection Items
{
get {
return MyItems;
}
}
}
public class MyObjectCollection : ComboBox.ObjectCollection
{
public MyObjectCollection(ComboBox Owner) : base(Owner)
{
}
new public int Add(Object j)
{
base.Add(j);
return 0;
}
}
As you can see, I am creating a new class MyComboBox2 derived from ComboBox. This class is supposed to have a new Items property, which would be of type MyObjectCollection rather than ComboBox.ObjectCollection. I have a comboBox called myComboBox21 on the form of type MyComboBox2. When I want to add a new object to my ComboBox, I would execute code like this: myComboBox21.Items.Add("text");
In this case, I end up executing the Add method of MyObjectCollection that I implemented myself. However, the ComboBox on the form does not end up containing value 'text'. I am attaching screenshot of debugger showing ComboBox values. MyComboBox21 contains Items Property (which does contain "text", as shown in screenshot "2.png"), and it contains base.Items (which does not contain "text" as shown in "1.png"). So, apparently, MyComboBox21 contains its own Items property (which I can insert to), and its base class's Items property, which gets displayed in the Windows Form. What can I do so that I can successfully add to comboBox with my own method? Since my ComboBox has 2 Items properties, can I specify which Items property's values should be shown in ComboBox?
Just by looking very quickly on the code:
The original Item index is declared as
virtual Object this[int index] {...}
Does the new keyword maybe be exchanged by override in your implementation in order to make the runtime pick the intended code?

Override the Add method of a ComboBox

Is there a way to override the Add() method of Combobx?
The reason I ask this is that I want to Add the class objects to my combbox but for display I want it to show the Name of my objects.
so for example: combbox.Items.Add(myClassObject)
but what we actually see in the combbobx as the user will show as myClassObject.Name
If I right understood your request, you can do that using the binding:
Pseudocode:
comboBox.DataSource = collectionOfData;
comboBox.DisplayMember = "Name";
The data will be added to combo but visualized will be the Name property of the "data".
This all done using DisplayMember Property.
Because ComboBox uses ToString() method of object which is added in Items collections to display on the UI, so override ToString of myClassObject to return whatever you want, it is simple:
class myClassObject
{
public override string ToString()
{
return "whatever you want";
}
}
With this way you do not touch ComboBox Control
You can develop extension Method
public static class Extension
{
public static void Add(this ComboBox, myClassObject value)
{
...
}
}
Create a new control that extends combobox control. Then override Add method.

Best way to copy a comboBox from form1 to form2

First I set the modifier property to "Internal" of comboBox1 on form1.
I used the following code:
form1 f1 = new form1();
object[] obj = new object[f1.comboBox1.Items.Count];
f1.comboBox.Items.CopyTo(obj, 0);
comboBox2.Items.AddRange(obj);`
Is it the best way to do this?
PS: I couldn't make this: Best way to access a control on another form in Windows Forms? to work.
PPS: Making controls is public is not what I like and neither preferred.
If you are wanting two drop-down lists with the same items in them it is much better that you store those items somewhere common and build up both combo boxes from there.
e.g.
public class Context{
...
...
public List<Foo> FooItems {
get{...}
}
}
public class Form1 {
...
combobox.AddRange(this.context.FooItems);
...
}
public class Form2 {
...
combobox.AddRange(this.context.FooItems);
...
}
This way you prevent coupling between your different forms, and still have only one place where you derive the values that go into the list.

Set properties on dynamically added UserControl

I'm dynamically adding a custom user control to the page as per this post.
However, i need to set a property on the user control as i add it to the page. How do I do that?
Code snippet would be nice :)
Details:
I have a custom user control with a public field (property ... e.g. public int someId;)
As I add a UserControl to a page, its type is UserControl. Do i just cast it into MyCustomUCType and set a property on a cast control?
p.s. looks like I've answered my own question after all.
Ah, I answered before you added the additional clarification. The short answer is, yes, just cast it as your custom type.
I'll leave the rest of my answer here for reference, though it doesn't appear you'll need it.
Borrowing from the code in the other question, and assuming that all your User Controls can be made to inherit from the same base class, you could do this.
Create a new class to act as the base control:
public class MyBaseControl : System.Web.UI.UserControl
{
public string MyProperty
{
get { return ViewState["MyProp"] as string; }
set { ViewState["MyProp"] = value; }
}
}
Then update your user controls to inherit from your base class instead of UserControl:
public partial class SampleControl2 : MyBaseControl
{
....
Then, in the place where you load the controls, change this:
UserControl uc = (UserControl)LoadControl(controlPath);
PlaceHolder1.Controls.Add(uc);
to:
MyBaseControl uc = (MyBaseControl)LoadControl(controlPath);
uc.MyProperty = "foo";
PlaceHolder1.Controls.Add(uc);
"As I add a UserControl to a page, its
type is UserControl. Do i just cast it
into MyCustomUCType and set a property
on a cast control?"
That's what I'd do. If you're actually using the example code where it loads different controls, I'd use an if(x is ControlType) as well.
if(x is Label)
{
Label l = x as Label;
l.Text = "Batman!";
}
else
//...
Edit: Now it's 2.0 compatible
Yes, you just cast the control to the proper type. EX:
((MyControl)control).MyProperty = "blah";

Categories

Resources