Click here to Skip to main content
15,844,011 members
Home / Discussions / C#
   

C#

 
AnswerRe: help on WebBrowser Controller to select a value on a drop down list Pin
Richard MacCutchan17-Feb-15 23:45
mveRichard MacCutchan17-Feb-15 23:45 
QuestionThreads vs. Tasks Article Pin
Kevin Marois17-Feb-15 19:03
professionalKevin Marois17-Feb-15 19:03 
AnswerRe: Threads vs. Tasks Article Pin
Pete O'Hanlon17-Feb-15 21:22
subeditorPete O'Hanlon17-Feb-15 21:22 
GeneralRe: Threads vs. Tasks Article Pin
Kevin Marois18-Feb-15 6:30
professionalKevin Marois18-Feb-15 6:30 
QuestionList Box showing Value and Display members? Pin
uniflare17-Feb-15 7:21
uniflare17-Feb-15 7:21 
AnswerRe: List Box showing Value and Display members? Pin
Richard Andrew x6417-Feb-15 7:34
professionalRichard Andrew x6417-Feb-15 7:34 
AnswerRe: List Box showing Value and Display members? Pin
Pete O'Hanlon17-Feb-15 7:54
subeditorPete O'Hanlon17-Feb-15 7:54 
GeneralRe: List Box showing Value and Display members? Pin
uniflare17-Feb-15 11:21
uniflare17-Feb-15 11:21 
Ok did some debugging, it seems that when i set a new datasource, the combobox.DisplayMember defaults to "". Although the combobox.ValueMember stays as "Key".

the fix Im using is to re-set DisplayMember to "Value" on each datasource update.

Is this a bug? Is it supposed to reset these properties when the datasource is set? or is it my implementation that is buggy?

All other factors are identical. The actual problem was only the very FIRST datasource set worked as intended, every subsequent datasource was displayed improperly with displaymembers and valuemember being shown in the listbox.


Revised Code Example: (Working with fix) (Compiles and runs with the components on designer..)
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace etstapp1
{
    public partial class Form1 : Form
    {
        private skbtServerControl sc;
        public Form1()
        {
            InitializeComponent();

            this.sc = new skbtServerControl(this);

            // coxArmaPath = Combo Box (simple drop down)
            this.cBoxArmaPath.ValueMember = "Key";
            this.cBoxArmaPath.DisplayMember = "Value"; // This doesnt seem to stick after the first datasource set??
            // ....
        }

        public void addServerPathItem(String Key, String Value)
        {
            this.cBoxArmaPath.Items.Add(new KeyValuePair<String, String>(Key, Value));
            // This works fine, only the "DisplayMember" is displayed and the "ValueMember" is hidden.
        }

        // This acts differently with the same types???
        public void setServerPathDatasource(List<KeyValuePair<String, String>> source)
        {
            this.cBoxArmaPath.DisplayMember = "Value"; // fix datasource problem
            this.cBoxArmaPath.ValueMember = "Key"; // fix datasource problem

            this.cBoxArmaPath.DataSource = new BindingSource(source, null);
        }

        public void clearPathBox()
        {
            if(this.cBoxArmaPath.DataSource == null){
                this.cBoxArmaPath.Items.Clear();
            }
            else
            {
                this.cBoxArmaPath.DataSource = null;
            }
        }

        private void btnStatic_Click(object sender, EventArgs e)
        {
            this.sc.refreshformWindowWithSTATICDatasource();
        }

        private void btnNormal_Click(object sender, EventArgs e)
        {
            this.sc.refreshFormWindow();
        }

        private void btnDynamic_Click(object sender, EventArgs e)
        {
            this.sc.refreshformWindowWithDatasource();
        }
    }

    public class skbtServerControl
    {

        private CoreConfig CoreConfig;

        private Form1 frmMainWindowHandle;

        public skbtServerControl(Form1 f){
            this.frmMainWindowHandle = f;
            this.CoreConfig = new CoreConfig();
        }

        public void refreshFormWindow()
        {
            // Clear Drop Box (empties drop box)
            this.frmMainWindowHandle.clearPathBox();

            //skbtServerConfig is very simple property object
            foreach (KeyValuePair<String, skbtServerConfig> pair in CoreConfig.getServerConfigList())
            {
                // Populate Drop Box
                this.frmMainWindowHandle.addServerPathItem(pair.Key, pair.Value.getTextualName());
            }
        }

        // This works absolutely fine. ValueMembers are hidden.
        public void refreshformWindowWithSTATICDatasource()
        {
            // Clear Drop Box
            this.frmMainWindowHandle.clearPathBox();

            var pathDataSource = new List<KeyValuePair<String, String>>() 
            {
                new KeyValuePair<String, String>("testKey1", "somevalue1"),
                new KeyValuePair<String, String>("testKey2", "somevalue2"),
                new KeyValuePair<String, String>("testKey3", "somevalue3")
            };

            // Populate Drop Box
            this.frmMainWindowHandle.setServerPathDatasource(pathDataSource);
        }

        // ** HERE IS THE PROBLEM?? **
        // This creates a type that seems different to the above function which works fine...
        public void refreshformWindowWithDatasource()
        {
            // Clear Drop Box
            this.frmMainWindowHandle.clearPathBox();

            var pathDataSource = new List<KeyValuePair<String, String>>();

            //skbtServerConfig is very simple property object
            foreach (KeyValuePair<String, skbtServerConfig> pair in this.CoreConfig.getServerConfigList())
            {
                pathDataSource.Add(new KeyValuePair<String, String>(pair.Key, pair.Value.getTextualName()));
            }

            // Populate Drop Box
            this.frmMainWindowHandle.setServerPathDatasource(pathDataSource);
        }
    }

    public class CoreConfig
    {
        public Dictionary<String, skbtServerConfig> getServerConfigList(){
            return new Dictionary<string, skbtServerConfig>()
            {
                {"somekey1", new skbtServerConfig("somename1")},
                {"somekey2", new skbtServerConfig("somename2")}
            };
        }
    }

    public class skbtServerConfig
    {
        private String name;

        public skbtServerConfig(String name)
        {
            this.name = name;
        }

        public String getTextualName()
        {
            return this.name;
        }
    }
}


Thanks guys, its not pretty and probably not the way to use datasources but at least i know what the problem was and the solution.

Though, If anyone can tell me why its resetting the members?

modified 17-Feb-15 16:32pm.

GeneralRe: List Box showing Value and Display members? Pin
Pete O'Hanlon17-Feb-15 11:32
subeditorPete O'Hanlon17-Feb-15 11:32 
GeneralRe: List Box showing Value and Display members? Pin
uniflare17-Feb-15 12:29
uniflare17-Feb-15 12:29 
AnswerRe: List Box showing Value and Display members? Pin
seemajoshii17-Feb-15 20:09
seemajoshii17-Feb-15 20:09 
QuestionLogin to asp.net application through a link send via email Pin
User 1098516717-Feb-15 6:01
User 1098516717-Feb-15 6:01 
AnswerRe: Login to asp.net application through a link send via email Pin
Afzaal Ahmad Zeeshan17-Feb-15 6:33
professionalAfzaal Ahmad Zeeshan17-Feb-15 6:33 
QuestionVisual studio 2010 C# identity without hyphen Pin
sdfsdfsdfewrew3feff16-Feb-15 16:55
sdfsdfsdfewrew3feff16-Feb-15 16:55 
AnswerRe: Visual studio 2010 C# identity without hyphen Pin
Dave Kreskowiak16-Feb-15 17:12
mveDave Kreskowiak16-Feb-15 17:12 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
sdfsdfsdfewrew3feff17-Feb-15 3:54
sdfsdfsdfewrew3feff17-Feb-15 3:54 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
Dave Kreskowiak17-Feb-15 7:56
mveDave Kreskowiak17-Feb-15 7:56 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
sdfsdfsdfewrew3feff17-Feb-15 8:09
sdfsdfsdfewrew3feff17-Feb-15 8:09 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
Dave Kreskowiak17-Feb-15 8:11
mveDave Kreskowiak17-Feb-15 8:11 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
sdfsdfsdfewrew3feff17-Feb-15 8:17
sdfsdfsdfewrew3feff17-Feb-15 8:17 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
Mark_Wallace17-Feb-15 10:44
Mark_Wallace17-Feb-15 10:44 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
Dave Kreskowiak17-Feb-15 10:49
mveDave Kreskowiak17-Feb-15 10:49 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
Pete O'Hanlon17-Feb-15 9:41
subeditorPete O'Hanlon17-Feb-15 9:41 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
Richard MacCutchan17-Feb-15 8:26
mveRichard MacCutchan17-Feb-15 8:26 
GeneralRe: Visual studio 2010 C# identity without hyphen Pin
Dave Kreskowiak17-Feb-15 8:33
mveDave Kreskowiak17-Feb-15 8:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.