Laden...

Forenbeiträge von Marcel99 Ingesamt 2 Beiträge

07.01.2022 - 14:20 Uhr

Hallo myCSharp Nutzer,
ich wollte ein form erstellen, in dem man mit Hilfe von einer ComboBox Name und Nachname, aus einer Tabelle, einer Person auswählen kann. Mit dem Inhalt der ComboBox soll vergliechen werden welche Zeile in der Tabelle die gleichen Werte hat und es soll die ID der Person in einer TextBox ausgegeben werden.

Was ich bis jetzt gemacht habe:

Tabelle erstellt:


CREATE TABLE [dbo].[Kunden] (
    [Kunden_ID] NVARCHAR (50) NOT NULL,
    [Vorname]   NVARCHAR (50) NULL,
    [Nachname]  NVARCHAR (50) NULL

Code um die 2 Werte Name und Nachname in der ComboBox auswählen zu können:


            SqlCommand com = new SqlCommand("select * from Kunden", con);
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                comboBox1.Items.Add(ds.Tables[0].Rows[i][1] + " " + ds.Tables[0].Rows[i][2]);
            }
            con.Close();

Code um die Tabelle mit den Inhalt der Combobox zu vergleichen:


SqlCommand cmd = new SqlCommand("select * from Kunden where Vorname AND ='" + comboBox1.Text + "'and Nachname='" + comboBox1.Text + "'", con);
            con.Open();
            cmd.ExecuteNonQuery();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string id = (string)dr["Kunden_ID"].ToString();
                kunden_id_txtbox.Text = id;
            }
            con.Close();

(die select abfrage oder etwas anderes irgendwas anderes ist natürlich falsch)

Mein Form wird angehängt.
Bei der ComboBox kann man den vollen Namen eines Kunden auswählen.

11.08.2021 - 10:44 Uhr

Guten Tag, ich will ein Programm in Win. Forms erstellen, in welchen man eine Text Datei auswählen kann und nach Wörtern und Zeilen durchsuchen kann. Die Ausgabe soll den Zeileninhalt angeben in welchen das besagte Wort oder die Zeilenangabe drine steckt.
Bis jetzt habe ich es geschafft eine Text Datei auszuwählen und den Text nach Wörtern durchzusuchen, die der Text beinhaltet. Wo bei ich jetzt noch Hilfe bräuchte ist die Eingabe der Zeileneingabe.


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<string> FileLines = new List<string>();
        List<int> iLineNumber = new List<int>();

        private void button1_Click(object sender, EventArgs e)
        {
            // Open a file dialog
            using (OpenFileDialog openDialog = new OpenFileDialog())
            {
                // Set the file dialog to show only *.txt file or all files
                openDialog.Filter = "Text files (*.txt)|*.txt|All Files (*.*)|*.*";

                // Allow only single file selection
                openDialog.Multiselect = false;

                // Make sure the user didn't clicked the 'Cancel' button
                if (openDialog.ShowDialog(this) == DialogResult.OK)
                {
                    // Update the current file label with the filename only (not the full path)
                    textBox1.Text = $"Current file: {Path.GetFileName(openDialog.FileName)}";

                    // Add each line of the txt file into the list
                    foreach (string line in File.ReadAllLines(openDialog.FileName, Encoding.UTF8))
                        FileLines.Add(line);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Clear the list
            listView1.Items.Clear();

            // Count the number of line so you will be able to present it on the results list later on
            int iLineNumber = 1;

            // For each item in the 'FileLines' list
            foreach (var item in FileLines)
            {
                // Check whether the current line contains the term the user typed in the searchbox
                // I'm using 'ToLower()' to ignore case
                if (item.ToLower().Contains(textBox2.Text.ToLower()))
                {
                    // Create new ListViewItem to be added later on to the results list
                    // Add the first column the complete line that contains the term in the searchbox
                    ListViewItem lvi = new ListViewItem(item);

                    // Add the line number to the second column
                    lvi.SubItems.Add(iLineNumber.ToString());

                    // Add the ListviewItem to the results list
                    listView1.Items.Add(lvi);
                }

                // Increment the line number variable
                iLineNumber++;
            }
    }