2012-04-03 10 views
6

में सुंदर कुंजी नाम मेरे पास एक combobox है जो Keys गणना (Winforms) द्वारा पॉप्युलेट किया गया है।सी # (फॉर्म)

समस्या यह है कि कुंजी नाम अनुभवहीन उपयोगकर्ताओं के लिए बहुत स्पष्ट नहीं हैं। उदाहरण के लिए, औसत उपयोगकर्ता को पता नहीं हो सकता कि 'ओमपाइप' या 'हांजामोड' का अर्थ क्या है। तो, मैं इस मुद्दे को कैसे हल कर सकता हूं, और कुछ बेहतर कुंजी नाम कैसे प्राप्त कर सकता हूं?

मैं कुंजी और उनके नामों के साथ एक शब्दकोश बनाने की सोच रहा हूं, लेकिन शब्दकोश को पॉप्युलेट करना बहुत समय ले रहा है।

+0

यह है कि ड्यूकमेंटेशन – Sandeep

+0

@JustinNiessner Keys विंडोज रूपों (System.Windows.Forms.Keys) से एक गणना है। – Tibi

+0

आपको हर मूल्य के लिए विवरण के साथ आना होगा, जो समय लेने वाला हिस्सा है। इससे कोई फर्क नहीं पड़ता कि आप उन्हें कैसे स्टोर करते हैं। –

उत्तर

7

एक संसाधन फ़ाइल बनाएं जो उपयोगकर्ता-समझने योग्य स्ट्रिंग के लिए प्रमुख नामों को मानचित्र करे। यदि संसाधन फ़ाइल के पास किसी विशेष कुंजी के लिए कोई मान नहीं है, तो केवल कुंजी नाम के साथ जाएं (जैसा कि आप अभी कर रहे हैं), इस तरह आपको केवल उन लोगों को परिभाषित करना होगा जिन्हें समझना मुश्किल है, और आप नहीं करते उन्हें सब कुछ आगे करना है।

यह आपको विभिन्न भाषाओं में स्थानांतरित करने की अनुमति देता है, यदि आप चाहें तो।

संपादित करें: कोड जोड़ा गया उदाहरण। धारणा, आप नाम "KeyNames.resx"

foreach (var key in Enum.GetValues(typeof(Keys))) 
{ 
    var keyName = KeyNames.ResourceManager.GetString(key.ToString()); 
    if (keyName == null) 
     keyName = key.ToString(); 

    comboBox1.Items.Add(keyName); 
} 
+1

आम तौर पर यदि उपयोगकर्ता को स्थानीयकरण की आवश्यकता नहीं है, तो भी यह उपयोगकर्ता के अनुकूल मूल्यों को हार्ड-कोडिंग करने से बेहतर समाधान है। – jnylen

+0

यह सबसे अच्छा समाधान की तरह लगता है। – Tibi

0

कोड लिखने के आसपास कोई रास्ता नहीं है। यहाँ एक दृष्टिकोण है कि कौन सा इस्तेमाल कर सकते हैं है, शायद कम से कम आवश्यक प्रयास के काफी करीब है, हालांकि:

string GetBaseKeyDescription(Keys k) { 
    switch (k & ~Keys.Modifiers) { 
     case Keys.OemPipe: 
      return "Pipe |"; 
     case Keys.OemPeriod: 
      return "Dot ."; 
     case Keys.HanjaMode: 
      return "(Description of HanjaMode key)"; 
     default: 
      return k.ToString(); 
    } 
} 

मुझे यकीन है कि अगर आप & ~Keys.Modifiers बिट या नहीं की जरूरत है नहीं कर रहा हूँ - अगर आप ऐसा करेंगे, तो आप शायद लिखना चाहें संशोधक को संभालने के लिए अधिक कोड - लेकिन मैंने पहले इसी तरह की चीजें की हैं।

1

आप केवल कुछ चाबी के लिए वर्णन प्रदान करना चाहते हैं, तो संसाधन फ़ाइल है कि आप पाश कर सकते हैं System.Windows.Forms.Keys और एक विधि की आपूर्ति है कि कुंजी enum नाम करने के लिए चूक:

private void Form1_Load(object sender, EventArgs e) 
{ 
    foreach (System.Windows.Forms.Keys key in Enum.GetValues(typeof(System.Windows.Forms.Keys))) 
    { 
     comboBoxKeys.Items.Add(new { Value = key, Description = GetDescription(key) }); 
    } 

    comboBoxKeys.DisplayMember = "Description"; 
} 

private string GetDescription(System.Windows.Forms.Keys key) 
{ 
    switch(key) 
    { 
     case Keys.OemPipe: 
      return "Better oem pipe description"; 

     case Keys.HanjaMode: 
      return "Ninja mode"; 

     default: 
      return key.ToString(); // default name 
    } 
} 
0

आप गणनाओं के लिए गुण असाइन कर सकते हैं। यह सबसे अच्छा तरीका है। अन्यथा आप समानांतर शब्दकोशों को बनाए रखने या switch-case कथन की बढ़ती सूची को बनाए रखने के लिए समाप्त हो जाएंगे।

करने का तरीका यहां enum को चिह्नित होगा:

public enum MyEnums 
{ 
    [Description("OEM Pipe")] 
    OemPipe, 

    [Description("Hanja Mode")] 
    HanjaMode 
} 

आप एक विस्तार विधि के माध्यम से Description विशेषता प्राप्त कर सकते हैं:

public static string ToEnumDescription(this Enum value) 
{ 
    FieldInfo fi = value.GetType().GetField(value.ToString()); 

    DescriptionAttribute[] attributes = 
     (DescriptionAttribute[])fi.GetCustomAttributes(
     typeof(DescriptionAttribute), 
     false); 

    if (attributes != null && 
     attributes.Length > 0) 
     return attributes[0].Description; 
    else 
     return value.ToString(); 
} 

वास्तव में शुमार विवरण प्राप्त करने के लिए, आप इसे इस कहेंगे मार्ग;

var enumAsText = theEnum.ToEnumDescription(); 

तुम भी ऐसा कर सकता है:

MyEnums.OemPipe.ToEnumDescription(); 
+0

वह एक मौजूदा गणना ('System.Windows.Forms.Keys') का उपयोग कर रहा है और इसलिए' विवरण 'विशेषताओं को जोड़ नहीं सकता है। – jnylen

+0

एलओएल, जिसे शुरू में नहीं बताया गया था ... – code4life

1

"OEM" का अर्थ है मूल उपकरण निर्माता। दूसरे शब्दों में, कुंजीपटल बनाने वाली कंपनी की तरह। ये नाम विशेष हैं क्योंकि 'नियमित' कीबोर्ड पर, | उत्पन्न करने या कोरियाई (अनुमान) में हांजा रेडिकल को चालू करने के लिए कोई समर्पित कुंजी नहीं है। | प्राप्त करने के लिए अधिकांश लेआउट पर Shift कुंजी को दबाए रखना आवश्यक है। कुछ कीबोर्ड निर्माता मानक लेआउट में कुंजी जोड़ सकते हैं जो ऐसा करते हैं।

आपको कुछ रोक देना चाहिए, ये कुंजी उपयोगकर्ता के कीबोर्ड पर उपलब्ध होने की संभावना नहीं है, इसलिए उन्हें संभावित शॉर्टकट कीस्ट्रोक के रूप में प्रस्तुत करना उपयोगी नहीं है। सबसे महत्वपूर्ण बात यह है कि आप कुंजी से बाहर निकलने वाली स्ट्रिंग का उपयोग स्वयं में एक बुरा विचार है। जब आपको कुछ दिन अपने आवेदन को स्थानीयकृत करने की आवश्यकता होती है तो यह आपको सिरदर्द का एक बिल्ली देगा ताकि इस दुनिया में अन्य 5-अरब लोग ग्राहकों का भुगतान कर सकें।

+0

सच है। मैं हमेशा स्थानीयकरण से नफरत करता हूं, यह प्रोग्रामिंग में सबसे कष्टप्रद चीजों में से एक है ... – Tibi

3

मुझे लगता है कि आप उपयोगकर्ता को अपने एप्लिकेशन (जैसे शॉर्टकट कुंजी या गेम नियंत्रण) के भीतर से चाबियाँ आवंटित करने की अनुमति दे रहे हैं। दुर्भाग्यवश, कुंजी के लिए दोस्ताना विवरण प्राप्त करने का कोई आसान तरीका नहीं है (माइक्रोसॉफ्ट एक या समकक्ष एपीआई प्रदान नहीं करता है), इसलिए आपको अपने आप पर एक मानचित्रण बनाना होगा।

वर्तमान में स्वीकृत उत्तर शो के रूप में, संसाधन फ़ाइल का उपयोग करना आपके आवेदन के अंतर्राष्ट्रीयकरण की अनुमति देने के लिए ऐसा करने का एक शानदार तरीका है। (एक संसाधन फ़ाइल का उपयोग अभी भी सिफारिश की है हालांकि)

public static string GetDescription(Keys key) 
{ 
    switch (key) 
    { 
     //letters 
     case Keys.A: case Keys.B: case Keys.C: case Keys.D: case Keys.E: case Keys.F: 
     case Keys.G: case Keys.H: case Keys.I: case Keys.J: case Keys.K: case Keys.L: 
     case Keys.M: case Keys.N: case Keys.O: case Keys.P: case Keys.Q: case Keys.R: 
     case Keys.S: case Keys.T: case Keys.U: case Keys.V: case Keys.W: case Keys.X: 
     case Keys.Y: case Keys.Z: 
      return Enum.GetName(typeof(Keys), key); 

     //digits 
     case Keys.D0: 
      return "0"; 
     case Keys.NumPad0: 
      return "Number Pad 0"; 
     case Keys.D1: 
      return "1"; 
     case Keys.NumPad1: 
      return "Number Pad 1"; 
     case Keys.D2: 
      return "2"; 
     case Keys.NumPad2: 
      return "Number Pad 2"; 
     case Keys.D3: 
      return "3"; 
     case Keys.NumPad3: 
      return "Number Pad 3"; 
     case Keys.D4: 
      return "4"; 
     case Keys.NumPad4: 
      return "Number Pad 4"; 
     case Keys.D5: 
      return "5"; 
     case Keys.NumPad5: 
      return "Number Pad 5"; 
     case Keys.D6: 
      return "6"; 
     case Keys.NumPad6: 
      return "Number Pad 6"; 
     case Keys.D7: 
      return "7"; 
     case Keys.NumPad7: 
      return "Number Pad 7"; 
     case Keys.D8: 
      return "8"; 
     case Keys.NumPad8: 
      return "Number Pad 8"; 
     case Keys.D9: 
      return "9"; 
     case Keys.NumPad9: 
      return "Number Pad 9"; 

     //punctuation 
     case Keys.Add: 
      return "Number Pad +"; 
     case Keys.Subtract: 
      return "Number Pad -"; 
     case Keys.Divide: 
      return "Number Pad /"; 
     case Keys.Multiply: 
      return "Number Pad *"; 
     case Keys.Space: 
      return "Spacebar"; 
     case Keys.Decimal: 
      return "Number Pad ."; 

     //function 
     case Keys.F1: case Keys.F2: case Keys.F3: case Keys.F4: case Keys.F5: 
     case Keys.F6: case Keys.F7: case Keys.F8: case Keys.F9: case Keys.F10: 
     case Keys.F11: case Keys.F12: case Keys.F13: case Keys.F14: case Keys.F15: 
     case Keys.F16: case Keys.F17: case Keys.F18: case Keys.F19: case Keys.F20: 
     case Keys.F21: case Keys.F22: case Keys.F23: case Keys.F24: 
      return Enum.GetName(typeof(Keys), key); 

     //navigation 
     case Keys.Up: 
      return "Up Arrow"; 
     case Keys.Down: 
      return "Down Arrow"; 
     case Keys.Left: 
      return "Left Arrow"; 
     case Keys.Right: 
      return "Right Arrow"; 
     case Keys.Prior: 
      return "Page Up"; 
     case Keys.Next: 
      return "Page Down"; 
     case Keys.Home: 
      return "Home"; 
     case Keys.End: 
      return "End"; 

     //control keys 
     case Keys.Back: 
      return "Backspace"; 
     case Keys.Tab: 
      return "Tab"; 
     case Keys.Escape: 
      return "Escape"; 
     case Keys.Enter: 
      return "Enter"; 
     case Keys.Shift: case Keys.ShiftKey: 
      return "Shift"; 
     case Keys.LShiftKey: 
      return "Shift (Left)"; 
     case Keys.RShiftKey: 
      return "Shift (Right)"; 
     case Keys.Control: case Keys.ControlKey: 
      return "Control"; 
     case Keys.LControlKey: 
      return "Control (Left)"; 
     case Keys.RControlKey: 
      return "Control (Right)"; 
     case Keys.Menu: case Keys.Alt: 
      return "Alt"; 
     case Keys.LMenu: 
      return "Alt (Left)"; 
     case Keys.RMenu: 
      return "Alt (Right)"; 
     case Keys.Pause: 
      return "Pause"; 
     case Keys.CapsLock: 
      return "Caps Lock"; 
     case Keys.NumLock: 
      return "Num Lock"; 
     case Keys.Scroll: 
      return "Scroll Lock"; 
     case Keys.PrintScreen: 
      return "Print Screen"; 
     case Keys.Insert: 
      return "Insert"; 
     case Keys.Delete: 
      return "Delete"; 
     case Keys.Help: 
      return "Help"; 
     case Keys.LWin: 
      return "Windows (Left)"; 
     case Keys.RWin: 
      return "Windows (Right)"; 
     case Keys.Apps: 
      return "Context Menu"; 

     //browser keys 
     case Keys.BrowserBack: 
      return "Browser Back"; 
     case Keys.BrowserFavorites: 
      return "Browser Favorites"; 
     case Keys.BrowserForward: 
      return "Browser Forward"; 
     case Keys.BrowserHome: 
      return "Browser Home"; 
     case Keys.BrowserRefresh: 
      return "Browser Refresh"; 
     case Keys.BrowserSearch: 
      return "Browser Search"; 
     case Keys.BrowserStop: 
      return "Browser Stop"; 

     //media keys 
     case Keys.VolumeDown: 
      return "Volume Down"; 
     case Keys.VolumeMute: 
      return "Volume Mute"; 
     case Keys.VolumeUp: 
      return "Volume Up"; 
     case Keys.MediaNextTrack: 
      return "Next Track"; 
     case Keys.Play: 
     case Keys.MediaPlayPause: 
      return "Play"; 
     case Keys.MediaPreviousTrack: 
      return "Previous Track"; 
     case Keys.MediaStop: 
      return "Stop"; 
     case Keys.SelectMedia: 
      return "Select Media"; 

     //IME keys 
     case Keys.HanjaMode: case Keys.JunjaMode: case Keys.HangulMode: 
     case Keys.FinalMode: //duplicate values: Hanguel, Kana, Kanji 
     case Keys.IMEAccept: case Keys.IMEConvert: //duplicate: IMEAceept 
     case Keys.IMEModeChange: case Keys.IMENonconvert: 
      return null; 

     //special keys 
     case Keys.LaunchMail: 
      return "Launch Mail"; 
     case Keys.LaunchApplication1: 
      return "Launch Favorite Application 1"; 
     case Keys.LaunchApplication2: 
      return "Launch Favorite Application 2"; 
     case Keys.Zoom: 
      return "Zoom"; 

     //oem keys 
     case Keys.OemSemicolon: //oem1 
      return ";"; 
     case Keys.OemQuestion: //oem2 
      return "?"; 
     case Keys.Oemtilde:  //oem3 
      return "~"; 
     case Keys.OemOpenBrackets: //oem4 
      return "["; 
     case Keys.OemPipe: //oem5 
      return "|"; 
     case Keys.OemCloseBrackets: //oem6 
      return "]"; 
     case Keys.OemQuotes:  //oem7 
      return "'"; 
     case Keys.OemBackslash: //oem102 
      return "/"; 
     case Keys.Oemplus: 
      return "+"; 
     case Keys.OemMinus: 
      return "-"; 
     case Keys.Oemcomma: 
      return ","; 
     case Keys.OemPeriod: 
      return "."; 

     //unsupported oem keys 
     case Keys.Oem8: 
     case Keys.OemClear: 
      return null; 

     //unsupported other keys 
     case Keys.None:  case Keys.LButton: case Keys.RButton: case Keys.MButton: 
     case Keys.XButton1: case Keys.XButton2: case Keys.Clear: case Keys.Sleep: 
     case Keys.Cancel: case Keys.LineFeed: case Keys.Select: case Keys.Print: 
     case Keys.Execute: case Keys.Separator: case Keys.ProcessKey: case Keys.Packet: 
     case Keys.Attn:  case Keys.Crsel: case Keys.Exsel: case Keys.EraseEof: 
     case Keys.NoName: case Keys.Pa1:  case Keys.KeyCode: case Keys.Modifiers: 
      return null; 

     default: 
      throw new NotSupportedException(Enum.GetName(typeof(Keys), key)); 
    } 
} 

आप संसाधन फ़ाइल में कनवर्ट कर सकते हैं:

संदर्भ के लिए, कुंजी गणन मैं थोड़ी देर पहले लिखा था की एक पूरी जानवर बल कार्यान्वयन है निम्न प्रोग्राम चलाकर, और उसके बाद संसाधन के रूप में output.resx को अपने एप्लिकेशन में जोड़ना।

static void Main(string[] args) 
{ 
    using(ResXResourceWriter writer = new ResXResourceWriter("output.resx")) 
    { 
     //since there are duplicate values, we need to clumsily look at each name, then parse 
     foreach (string name in Enum.GetNames(typeof(Keys))) 
     { 
      object value = Enum.Parse(typeof(Keys), name); 
      string description = GetDescription((Keys)value); 

      if (description != null) 
       writer.AddResource(new ResXDataNode(name, description)); 
     } 
    } 
} 

यह आपको एक संसाधन फ़ाइल देगा जो स्वीकृत उत्तर में व्याख्या के तरीके में उपयोग किया जा सकता है।

संबंधित मुद्दे