Skip to content
Loading
Help with my first C# - using If statement
  • Jay Stewart
    1. public void update()  
    2.         {  
    3.             var pa = (System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));  
    4.             if (pa == "x86")  
    5.             { strSubKeyPath = "SOFTWARE"; }  
    6.             else  
    7.             { strSubKeyPath = "SOFTWARE\\Wow6432Node"; }  
    8.             RegistryKey pRegKey = Registry.LocalMachine.OpenSubKey(strSubKeyPath, true);  
    9.             pRegKey.CreateSubKey("BMSLCS");  
    10.             pRegKey = pRegKey.OpenSubKey("BMSLCS"true);  
    11.   
    12.             Object myFriendlyName = pRegKey.GetValue("FriendName");  
    13.             if (myFriendlyName == null) { pRegKey.SetValue("FriendName"""); }  
    14.             else { txtFriendlyName.Text = myFriendlyName.ToString(); }  
    15.             Object myOwner = pRegKey.GetValue("LAB-Client");  
    16.             if (myOwner == null) { pRegKey.SetValue("LAB-Client"""); }  
    17.             else { txtOwner.Text = myOwner.ToString(); }  
    18.             Object myLabLocation = pRegKey.GetValue("Lab-Location");  
    19.             if (myLabLocation == null) { pRegKey.SetValue("Lab-Location"""); }  
    20.             else { txtLabLocation.Text = myLabLocation.ToString(); }  
    21.             Object myDepartment = pRegKey.GetValue("ISSystemNetworked");  
    22.             if (myDepartment == null) { pRegKey.SetValue("ISSystemNetworked"""); }  
    23.             else { cbDepartment.Text = myDepartment.ToString(); }  
    24.         }  
    You could have a method like above and then from both load and click call the method 
     
     
    1.  private void bOk_Click(object sender, EventArgs e)  
    2. {  
    3.      update;  
    4. }  
     
    1. private void Form1_Load(object sender, EventArgs e)  
    2.         {  
    3.             update;  
    4.              
    5.         }  
     
     
    Not tested but just an idea 
  • wow thx.. this is so much nicer! Hah.. ive overlooked that icon so many times looking for just "Code"... i saw Highlighter in it and figured that would "Highlight" .
     
    so another quick q... is it common to double up the code say at form load and on button click?
     
    I tried removing some from ok click but to no avail. so it may be a bit sloppier than needed but it works!
     
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Text;  
    7. using Microsoft.Win32;  
    8. using System.Windows.Forms;  
    9.   
    10. namespace LCSInventoryLite  
    11. {  
    12.         public partial class Form1 : Form  
    13.     {  
    14.         private string strSubKeyPath;  
    15.   
    16.         public Form1()  
    17.         {  
    18.             InitializeComponent();  
    19.         }  
    20.   
    21.         private void Form1_Load(object sender, EventArgs e)  
    22.         {  
    23.             var pa = (System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));  
    24.             if (pa == "x86")  
    25.             { strSubKeyPath = "SOFTWARE"; }  
    26.             else  
    27.             { strSubKeyPath = "SOFTWARE\\Wow6432Node"; }  
    28.             RegistryKey pRegKey = Registry.LocalMachine.OpenSubKey(strSubKeyPath, true);  
    29.             pRegKey.CreateSubKey("BMSLCS");  
    30.             pRegKey = pRegKey.OpenSubKey("BMSLCS"true);  
    31.   
    32.             Object myFriendlyName = pRegKey.GetValue("FriendName");  
    33.                 if (myFriendlyName == null) {pRegKey.SetValue("FriendName"""); }  
    34.                 else {txtFriendlyName.Text = myFriendlyName.ToString(); }  
    35.             Object myOwner = pRegKey.GetValue("LAB-Client");  
    36.                 if (myOwner == null) { pRegKey.SetValue("LAB-Client"""); }  
    37.             else {txtOwner.Text = myOwner.ToString(); }  
    38.             Object myLabLocation = pRegKey.GetValue("Lab-Location");  
    39.                 if (myLabLocation == null) { pRegKey.SetValue("Lab-Location"""); }  
    40.                 else {txtLabLocation.Text = myLabLocation.ToString(); }  
    41.             Object myDepartment = pRegKey.GetValue("ISSystemNetworked");  
    42.                 if (myDepartment == null) { pRegKey.SetValue("ISSystemNetworked"""); }  
    43.                 else { cbDepartment.Text = myDepartment.ToString(); }  
    44.         }  
    45.   
    46.         private void bOk_Click(object sender, EventArgs e)  
    47.         {  
    48.             var pa = (System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));  
    49.             if (pa == "x86")  
    50.             { strSubKeyPath = "SOFTWARE"; }  
    51.             else  
    52.             { strSubKeyPath = "SOFTWARE\\Wow6432Node"; }  
    53.             RegistryKey pRegKey = Registry.LocalMachine.OpenSubKey(strSubKeyPath, true);  
    54.             pRegKey.CreateSubKey("BMSLCS");  
    55.             pRegKey = pRegKey.OpenSubKey("BMSLCS"true);  
    56.   
    57.             pRegKey.SetValue("FriendName", txtFriendlyName.Text);  
    58.             pRegKey.SetValue("LAB-Client", txtOwner.Text);   
    59.             pRegKey.SetValue("Lab-Location", txtLabLocation.Text);   
    60.             pRegKey.SetValue("ISSystemNetworked", cbDepartment.Text);   
    61.             Application.Exit();  
    62.         }  
    63.   
    64.         private void bCancel_Click_1(object sender, EventArgs e)  
    65.         {  
    66.             Application.Exit();  
    67.         }  
    68.   
    69.     }  
    70. }  
     
  • Jay Stewart
    Click the code icon at top of box 
     
    <> paste code
     
    1. This is code  
     
  • seems way easier... and ill give it a try. I have much to learn! hah.. but I am planning on learning it. WiseScript is so old and what we typically use and need to get away from that.
    Thnx! will report back soon...
     
     
     
    side note.. how are you guys putting in the code like that? i couldnt find a sticky, or help anywhere.. still looking... but would make it easier for me posting up during my q's and replies!!!
  • Abraham Olatubosun
    Try it this way:
    1. string x86 ="x86";  
    2. string x64 = "x64";  
    3. string AMD = "AMD";
    4. var chk = (System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));  
    5.   
    6. if(chk == "X86")  
    7. {  
    8.    Object myFriendlyName = pRegKey.GetValue("FriendName");  
    9. Object myOwner = pRegKey.GetValue("LAB-Client");  
    10. Object myLabLocation = pRegKey.GetValue("Lab-Location");  
    11. Object myDepartment = pRegKey.GetValue("ISSystemNetworked");  
    12. txtFriendlyName.Text = myFriendlyName.ToString();  
    13. txtOwner.Text = x86;   //myOwner.ToString();  
    14. txtLabLocation.Text = myLabLocation.ToString();  
    15. cbDepartment.Text = myDepartment.ToString();  
    16. }  
    17. else if(chk == "x64")
    18. {  
    19.  Object myFriendlyName = pRegKey.GetValue("FriendName");  
    20. Object myOwner = pRegKey.GetValue("LAB-Client");  
    21. Object myLabLocation = pRegKey.GetValue("Lab-Location");  
    22. Object myDepartment = pRegKey.GetValue("ISSystemNetworked");  
    23. txtFriendlyName.Text = myFriendlyName.ToString();  
    24. txtOwner.Text = x64;    //myOwner.ToString();  
    25. txtLabLocation.Text = myLabLocation.ToString();  
    26. cbDepartment.Text = myDepartment.ToString();  
    27. }
    28.   else
    29. {
    30.  
      1.  Object myFriendlyName = pRegKey.GetValue("FriendName");    
      2. Object myOwner = pRegKey.GetValue("LAB-Client");    
      3. Object myLabLocation = pRegKey.GetValue("Lab-Location");    
      4. Object myDepartment = pRegKey.GetValue("ISSystemNetworked");    
      5. txtFriendlyName.Text = myFriendlyName.ToString();    
      6. txtOwner.Text = AMD;    //myOwner.ToString();    
      7. txtLabLocation.Text = myLabLocation.ToString();    
      8. cbDepartment.Text = myDepartment.ToString();    
    31. }