How to format Code in C#

Morsor Maya 1 Reputation point
2020-12-13T17:15:44.943+00:00
public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            // TODO: Diese Codezeile lädt Daten in die Tabelle "dsCountries.CustomersCountry". Sie können sie bei Bedarf verschieben oder entfernen  
            this.customersCountryTableAdapter.Fill(this.dsCountries.CustomersCountry);  
            this.customersBindingSource.Sort = "CompanyName";  
            if(cboCountry.Items.Count > 1)  
            {  
                cboCountry.SelectedIndex = 1;  
                cboCountry.SelectedIndex = 0;  
            }  
            // TODO: Diese Codezeile lädt Daten in die Tabelle "northwindCustomerDataSet.Customers". Sie können sie bei Bedarf verschieben oder entfernen.  
            //this.customersTableAdapter.ClearBeforeFill = true;  
            //this.customersTableAdapter.Fill(this.northwindCustomerDataSet.Customers);  
  
        }  
  
        private void cboCountry_SelectedIndexChanged(object sender, EventArgs e)  
        {  
            this.customersTableAdapter.Fill(this.northwindCustomerDataSet.Customers, (String) cboCountry.SelectedValue);  
        }  
  
        private void btnDeleteData_Click(object sender, EventArgs e)  
        {  
            //Save to db  
            /*  
             *  
             * customersBindingSource.EndEdit();  
             * customersTableAdapter.Update(northwindCustomerDataSet.Customers);  
            */  
            var confirmResult = MessageBox.Show("Are you sure to delete all selected items??\nYou can only restore this date with an export-file!",  
                                     "Confirm Delete!!",  
                                     MessageBoxButtons.YesNo);  
            if (confirmResult == DialogResult.Yes)  
            {  
                this.northwindCustomerDataSet.Clear();  
            }  
  
        }  
  
        private void btnExport_Click(object sender, EventArgs e)  
        {  
            try  
            {  
                SaveFileDialog saveDialog = new SaveFileDialog  
                {  
                    Title = "Select XML File",  
                    CheckPathExists = true,  
  
                    DefaultExt = "xml",  
                    Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",  
                    RestoreDirectory = true,  
                };  
  
                if (saveDialog.ShowDialog() == DialogResult.OK)  
                {  
                    northwindCustomerDataSet.WriteXml(saveDialog.FileName, XmlWriteMode.WriteSchema);  
                }  
            } catch(Exception ex) {  
                MessageBox.Show("Es ist ein Fehler aufgetreten: " + ex.Message + "\n\n" + ex.StackTrace, "Error exporting",  
                    MessageBoxButtons.OK, MessageBoxIcon.Error);  
            }  
            
        }  
  
        private void btnImport_Click(object sender, EventArgs e)  
        {  
            try  
            {  
                foreach (DataTable dataTable in northwindCustomerDataSet.Tables)  
                    dataTable.BeginLoadData();  
  
                using (OpenFileDialog openFileDialog = new OpenFileDialog())  
                {  
                    openFileDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";  
                    openFileDialog.RestoreDirectory = true;  
  
                    if (openFileDialog.ShowDialog() == DialogResult.OK)  
                    {  
                        northwindCustomerDataSet.ReadXml(openFileDialog.FileName);  
                    }  
                }  
  
                 
  
                foreach (DataTable dataTable in northwindCustomerDataSet.Tables)  
                    dataTable.EndLoadData();  
                northwindCustomerDataSet.AcceptChanges();  
  
            }  
            catch (Exception ex)  
            {  
                MessageBox.Show("Es ist ein Fehler aufgetreten: " + ex.Message + "\n\n" + ex.StackTrace, "Error exporting",  
                    MessageBoxButtons.OK, MessageBoxIcon.Error);  
            }  
              
              
        }  
    }  


   private void openDLL_Click(object sender, EventArgs e)  
        {  
            OpenFileDialog dllDialog = new OpenFileDialog();  
            dllDialog.Title = "Öffne Assembly-DLL";  
            dllDialog.Filter = "Assembly-DLL|*.dll|Assembly-EXE|*.exe";  
            if(dllDialog.ShowDialog() == DialogResult.OK)  
            {  
                try {  
                    typeTree.BeginUpdate();  
                    Assembly assembly = Assembly.LoadFile(dllDialog.FileName);  
                    assemblyName.Text = "Assembly-Name: " + assembly.GetName().Name;  
                    typeTree.Nodes.Clear();  
                    foreach (Type type in assembly.GetTypes())  
                    {  
                        Console.WriteLine(type.ToString());  
                        //Add Type to tree  
                        TreeNode node = new TreeNode(type.Name);  
                        //Check if theire are any methods  
                        if (type.GetMethods().Length != 0)  
                        {  
                            //Add all Methods  
                            TreeNode methodsNode = new TreeNode("Methods: ");  
                            foreach (MethodInfo method in type.GetMethods())  
                            {  
                                TreeNode methodNode = new TreeNode(method.Name);  
                                if (method.GetParameters().Length != 0)  
                                {  
                                    TreeNode paramNode = new TreeNode("Parameters: ");  
                                    foreach (ParameterInfo paramInfo in method.GetParameters())  
                                    {  
                                        if (!paramInfo.IsOut)  
                                        {  
                                            paramNode.Nodes.Add(paramInfo.ParameterType.Name + " " + paramInfo.Name);  
                                        }  
                                    }  
                                    methodNode.Nodes.Add(paramNode);  
                                }  
                                methodNode.Nodes.Add(new TreeNode("Returns: " + method.ReturnType.Name));  
                                methodsNode.Nodes.Add(methodNode);  
                            }  
                            node.Nodes.Add(methodsNode);  
                        }  
                        //Check if theire are any properties  
                        if(type.GetProperties().Length != 0)  
                        {  
                            TreeNode memberNode = new TreeNode("Properties: ");  
                            foreach (PropertyInfo prop in type.GetProperties())  
                            {  
                                memberNode.Nodes.Add(prop.PropertyType.Name + " " + prop.Name);  
                            }  
                            node.Nodes.Add(memberNode);  
  
                            typeTree.Nodes.Add(node);  
                        }  
                    }  
                    typeTree.EndUpdate();  
                }  
                catch (BadImageFormatException)  
                {  
                    MessageBox.Show("Konnte diese Datei nicht laden, da diese ein ungültiges Format hat!", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);  
                }  
            }  
        }  

47701-grafik.png
47702-grafik.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,218 questions
0 comments No comments
{count} votes

8 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points
    2020-12-13T18:53:50.093+00:00

    Hello @Morsor Maya

    If the question "how to format code" means using a shortcut key in Visual Studio to format code shown you can find the shortcut under Visual Studio options, keyboard. Note that the default shortcut is different dependent on the schema used as per the second screenshot

    If this is not what you mean please explain.

    47711-a1.png

    47721-a1a.png

    1 person found this answer helpful.
    0 comments No comments

  2. WayneAKing 4,921 Reputation points
    2020-12-13T22:06:55.807+00:00

    You should expend a little effort to describe what exactly
    you are asking,

    We cannot infer from a bunch of code what you expect as an
    answer to a question only asked vaguely in the thread subject.

    Taking that subject: "How to format Code in C#"
    literally and at face value, look at the menus in the IDE.

    Under Edit->Advanced you should see two menu items:

    "Format Document" and "Format Selection".

    • Wayne
    0 comments No comments

  3. SamuelAnderson 1 Reputation point
    2021-04-11T18:20:18.753+00:00
    private void axInkEdit2_Change(object sender, EventArgs e)
            {
                string text = axInkEdit2.Text;
                int number = 0;
                bool success = Int32.TryParse(text, out number);
                if(success)
                axInkEdit2.Font = new Font("Microsoft Sans Serif", number); 
                else
                Form1.ActiveForm.BackColor = (Color)new ColorConverter().ConvertFromString(text);
    
                Console.WriteLine(number);
    
                number = 0;
                success = false;
                axInkEdit2.ResetText();
            }
    
    0 comments No comments

  4. Nick Jefferson 1 Reputation point
    2021-01-31T19:44:46.657+00:00
    ************************Server*************************
    using System;
    ``using System.Data;
        using System.Data.SqlClient;
    
        namespace Server
        {
            public class RemoteServer : MarshalByRefObject
            {
                public DataSet GetDataSet()
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml("D:/4.Klasse/POSE/C#/Remoting/Server/Server/cd_catalog.xml");
                    return ds;
                }
            }
        }
    
    
    using Server;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Http;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    
    ******************************************Client*****************************
    
    
    namespace Client
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                HttpChannel ch = new HttpChannel(8088);
                ChannelServices.RegisterChannel(ch);
    
                try
                {
                    RemoteServer serverdata = (RemoteServer)Activator.GetObject(typeof(RemoteServer), "http://localhost:8086/Server/RemoteServer.soap");
    
                    DataSet ds = serverdata.GetDataSet();
    
                    dataGridView1.DataSource = ds;
                    dataGridView1.DataMember = ds.Tables[0].TableName;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
    
        }
    }
    
    *****************************************Config***************************************
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
      </startup>
      <system.runtime.remoting>
        <application name="Server">
          <service>
            <wellknown mode="Singleton"
                    type="Server.RemoteServer, Server"
                    objectUri="RemoteServer.soap"/>
          </service>
          <channels>
            <channel ref="http" port="8086"/>
          </channels>
        </application>
      </system.runtime.remoting>
    </configuration>
    
    using System;
    using System.Runtime.Remoting;
    using System.IO;
    using System.Reflection;
    
    
    *************************************TestApp**************************************************
    namespace Server
    {
        class TestApp
        {
            static void Main(string[] args)
            {
                // Get the Application Directory
                string strAppDir = Path.GetDirectoryName(
                    Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
    
                // Use the Configuration File to configure and start the Server
                string strConfigFile = strAppDir + "\\Server.exe.config";
                RemotingConfiguration.Configure(strConfigFile);
    
                // Keep the Server alive to respond to requests
                Console.WriteLine("Server running and waiting for requests ...");
                Console.WriteLine("Press [ENTER] to shut down Server");
                Console.ReadLine();
            }
        }
    }
    

  5. Nick Jefferson 1 Reputation point
    2021-01-31T20:48:23.15+00:00

    62227-formatted.png

    0 comments No comments