Visual CSharp .NET - Interfaz Gráfica C#

 
Vista:
sin imagen de perfil

Interfaz Gráfica C#

Publicado por Donovan Manuel (1 intervención) el 02/06/2023 03:17:13
Tengo el siguiente problema necesito crear una interfaz grafica la cual sea una calculadora pero mi problema es que no puedo crear lo que seria la interfaz ya que no tengo mucha experiencia en esto, en cambio si tengo los códigos los cuales son de el Designer.cs y el Inicio.cs lo unico que me falla es el diseño puesto que no me funciona la interfaz y tengo problemas si alguien pudiera ayudarme a realizar la interfaz tengo los códigos completos mi único problema es la interfaz ya que prácticamente nunca realice una como tal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
Designer.cs
using System;
using System.Windows.Forms;
 
namespace CalculadoraElectrica
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
 
    partial class MainForm : Form
    {
        private System.ComponentModel.IContainer components = null;
        private ComboBox cmbOpciones;
        private Label lblCarga;
        private TextBox txtCarga;
        private Label lblDiferenciaPotencial;
        private TextBox txtDiferenciaPotencial;
        private Label lblDensidadSuperficial;
        private TextBox txtDensidadSuperficial;
        private Label lblArea;
        private TextBox txtArea;
        private Label lblSeparacion;
        private TextBox txtSeparacion;
        private Label lblNumCapacitores;
        private TextBox txtNumCapacitores;
        private Button btnCalcular;
        private Label lblResultado;
 
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        private void InitializeComponent()
        {
            this.cmbOpciones = new System.Windows.Forms.ComboBox();
            this.lblCarga = new System.Windows.Forms.Label();
            this.txtCarga = new System.Windows.Forms.TextBox();
            this.lblDiferenciaPotencial = new System.Windows.Forms.Label();
            this.txtDiferenciaPotencial = new System.Windows.Forms.TextBox();
            this.lblDensidadSuperficial = new System.Windows.Forms.Label();
            this.txtDensidadSuperficial = new System.Windows.Forms.TextBox();
            this.lblArea = new System.Windows.Forms.Label();
            this.txtArea = new System.Windows.Forms.TextBox();
            this.lblSeparacion = new System.Windows.Forms.Label();
            this.txtSeparacion = new System.Windows.Forms.TextBox();
            this.lblNumCapacitores = new System.Windows.Forms.Label();
            this.txtNumCapacitores = new System.Windows.Forms.TextBox();
            this.btnCalcular = new System.Windows.Forms.Button();
            this.lblResultado = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // cmbOpciones
            //
            this.cmbOpciones.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbOpciones.Items.AddRange(new object[] {
            "Carga",
            "Diferencia de Potencial",
            "Densidad Superficial",
            "Área",
            "Separación",
            "Número de Capacitores"});
            this.cmbOpciones.Location = new System.Drawing.Point(12, 12);
            this.cmbOpciones.Name = "cmbOpciones";
            this.cmbOpciones.Size = new System.Drawing.Size(150, 21);
            this.cmbOpciones.TabIndex = 0;
            //
            // lblCarga
            //
            this.lblCarga.Location = new System.Drawing.Point(0, 0);
            this.lblCarga.Name = "lblCarga";
            this.lblCarga.Size = new System.Drawing.Size(100, 23);
            this.lblCarga.TabIndex = 1;
            //
            // txtCarga
            //
            this.txtCarga.Location = new System.Drawing.Point(0, 0);
            this.txtCarga.Name = "txtCarga";
            this.txtCarga.Size = new System.Drawing.Size(100, 20);
            this.txtCarga.TabIndex = 2;
            //
            // lblDiferenciaPotencial
            //
            this.lblDiferenciaPotencial.Location = new System.Drawing.Point(0, 0);
            this.lblDiferenciaPotencial.Name = "lblDiferenciaPotencial";
            this.lblDiferenciaPotencial.Size = new System.Drawing.Size(100, 23);
            this.lblDiferenciaPotencial.TabIndex = 3;
            //
            // txtDiferenciaPotencial
            //
            this.txtDiferenciaPotencial.Location = new System.Drawing.Point(0, 0);
            this.txtDiferenciaPotencial.Name = "txtDiferenciaPotencial";
            this.txtDiferenciaPotencial.Size = new System.Drawing.Size(100, 20);
            this.txtDiferenciaPotencial.TabIndex = 4;
            //
            // lblDensidadSuperficial
            //
            this.lblDensidadSuperficial.Location = new System.Drawing.Point(0, 0);
            this.lblDensidadSuperficial.Name = "lblDensidadSuperficial";
            this.lblDensidadSuperficial.Size = new System.Drawing.Size(100, 23);
            this.lblDensidadSuperficial.TabIndex = 5;
            //
            // txtDensidadSuperficial
            //
            this.txtDensidadSuperficial.Location = new System.Drawing.Point(0, 0);
            this.txtDensidadSuperficial.Name = "txtDensidadSuperficial";
            this.txtDensidadSuperficial.Size = new System.Drawing.Size(100, 20);
            this.txtDensidadSuperficial.TabIndex = 6;
            //
            // lblArea
            //
            this.lblArea.Location = new System.Drawing.Point(0, 0);
            this.lblArea.Name = "lblArea";
            this.lblArea.Size = new System.Drawing.Size(100, 23);
            this.lblArea.TabIndex = 7;
            //
            // txtArea
            //
            this.txtArea.Location = new System.Drawing.Point(0, 0);
            this.txtArea.Name = "txtArea";
            this.txtArea.Size = new System.Drawing.Size(100, 20);
            this.txtArea.TabIndex = 8;
            //
            // lblSeparacion
            //
            this.lblSeparacion.Location = new System.Drawing.Point(0, 0);
            this.lblSeparacion.Name = "lblSeparacion";
            this.lblSeparacion.Size = new System.Drawing.Size(100, 23);
            this.lblSeparacion.TabIndex = 9;
            //
            // txtSeparacion
            //
            this.txtSeparacion.Location = new System.Drawing.Point(0, 0);
            this.txtSeparacion.Name = "txtSeparacion";
            this.txtSeparacion.Size = new System.Drawing.Size(100, 20);
            this.txtSeparacion.TabIndex = 10;
            //
            // lblNumCapacitores
            //
            this.lblNumCapacitores.Location = new System.Drawing.Point(0, 0);
            this.lblNumCapacitores.Name = "lblNumCapacitores";
            this.lblNumCapacitores.Size = new System.Drawing.Size(100, 23);
            this.lblNumCapacitores.TabIndex = 11;
            //
            // txtNumCapacitores
            //
            this.txtNumCapacitores.Location = new System.Drawing.Point(0, 0);
            this.txtNumCapacitores.Name = "txtNumCapacitores";
            this.txtNumCapacitores.Size = new System.Drawing.Size(100, 20);
            this.txtNumCapacitores.TabIndex = 12;
            //
            // btnCalcular
            //
            this.btnCalcular.Location = new System.Drawing.Point(0, 0);
            this.btnCalcular.Name = "btnCalcular";
            this.btnCalcular.Size = new System.Drawing.Size(75, 23);
            this.btnCalcular.TabIndex = 13;
            //
            // lblResultado
            //
            this.lblResultado.Location = new System.Drawing.Point(0, 0);
            this.lblResultado.Name = "lblResultado";
            this.lblResultado.Size = new System.Drawing.Size(100, 23);
            this.lblResultado.TabIndex = 14;
            //
            // MainForm
            //
            this.ClientSize = new System.Drawing.Size(400, 300);
            this.Controls.Add(this.cmbOpciones);
            this.Controls.Add(this.lblCarga);
            this.Controls.Add(this.txtCarga);
            this.Controls.Add(this.lblDiferenciaPotencial);
            this.Controls.Add(this.txtDiferenciaPotencial);
            this.Controls.Add(this.lblDensidadSuperficial);
            this.Controls.Add(this.txtDensidadSuperficial);
            this.Controls.Add(this.lblArea);
            this.Controls.Add(this.txtArea);
            this.Controls.Add(this.lblSeparacion);
            this.Controls.Add(this.txtSeparacion);
            this.Controls.Add(this.lblNumCapacitores);
            this.Controls.Add(this.txtNumCapacitores);
            this.Controls.Add(this.btnCalcular);
            this.Controls.Add(this.lblResultado);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.Text = "Calculadora Eléctrica";
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
    }
}

1
Incio.cs
using System;
using System.Windows.Forms;
using static System.Windows.Forms.MonthCalendar;

namespace CalculadoraElectrica
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private void btnCalcular_Click(object sender, EventArgs e)
{
double carga, diferenciaPotencial, densidadSuperficial, area, separacion;
int numCapacitores;
double[] capacitancias;

if (!double.TryParse(txtCarga.Text, out carga) ||
!double.TryParse(txtDiferenciaPotencial.Text, out diferenciaPotencial) ||
!double.TryParse(txtDensidadSuperficial.Text, out densidadSuperficial) ||
!double.TryParse(txtArea.Text, out area) ||
!double.TryParse(txtSeparacion.Text, out separacion) ||
!int.TryParse(txtNumCapacitores.Text, out numCapacitores))
{
MessageBox.Show("Ingrese valores numéricos válidos.");
return;
}

switch (cmbOpciones.SelectedIndex)
{
case 0:
// Calcular cambio de energía potencial
double cambioEnergiaPotencial = calcularCambioEnergiaPotencial(carga, diferenciaPotencial);
MessageBox.Show($"El cambio de energía potencial es: {cambioEnergiaPotencial} J");
break;
case 1:
// Calcular potencial eléctrico
double potencialElectrico = calcularPotencialElectrico(carga, diferenciaPotencial);
MessageBox.Show($"El potencial eléctrico es: {potencialElectrico} V");
break;
case 2:
// Calcular capacitancia
double capacitancia = calcularCapacitancia(carga, diferenciaPotencial);
MessageBox.Show($"La capacitancia es: {capacitancia} F");
break;
case 3:
// Calcular distancia entre placas
double distanciaEntrePlacas = calcularDistanciaEntrePlacas(carga, diferenciaPotencial, densidadSuperficial);
MessageBox.Show($"La distancia entre placas es: {distanciaEntrePlacas} m");
break;
case 4:
// Calcular capacitancia con área y separación
double capacitanciaAreaSeparacion = calcularCapacitanciaAreaSeparacion(area, separacion);
MessageBox.Show($"La capacitancia es: {capacitanciaAreaSeparacion} F");
break;
case 5:
// Calcular diferencia de potencial entre placas con densidad de carga superficial y distancia
double diferenciaPotencialDensidad = calcularDiferenciaPotencialDensidad(densidadSuperficial, diferenciaPotencial);
MessageBox.Show($"La diferencia de potencial entre placas es: {diferenciaPotencialDensidad} V");
break;
case 6:
// Calcular capacitancia equivalente en configuración en serie
capacitancias = obtenerValoresCapacitancias(numCapacitores);
if (capacitancias != null)
{
double capacitanciaSerie = calcularCapacitanciaSerie(capacitancias, numCapacitores);
MessageBox.Show($"La capacitancia equivalente en configuración en serie es: {capacitanciaSerie} F");
}
break;
case 7:
// Calcular capacitancia equivalente en configuración en paralelo
capacitancias = obtenerValoresCapacitancias(numCapacitores);
if (capacitancias != null)
{
double capacitanciaParalelo = calcularCapacitanciaParalelo(capacitancias, numCapacitores);
MessageBox.Show($"La capacitancia equivalente en configuración en paralelo es: {capacitanciaParalelo} F");
}
break;
default:
MessageBox.Show("Seleccione una opción válida.");
break;
}
}

private double calcularCambioEnergiaPotencial(double carga, double diferenciaPotencial)
{
return carga * diferenciaPotencial;
}

private double calcularPotencialElectrico(double carga, double distancia)
{
return carga / distancia;
}

private double calcularCapacitancia(double carga, double diferenciaPotencial)
{
return carga / diferenciaPotencial;
}

private double calcularDistanciaEntrePlacas(double carga, double diferenciaPotencial, double densidadSuperficial)
{
return carga / (diferenciaPotencial * densidadSuperficial);
}

private double calcularCapacitanciaAreaSeparacion(double area, double separacion)
{
return 8.854 * Math.Pow(10, -12) * (area / separacion);
}

private double calcularDiferenciaPotencialDensidad(double densidadSuperficial, double distancia)
{
return densidadSuperficial * distancia;
}

private double calcularCapacitanciaSerie(double[] capacitancias, int numCapacitores)
{
double capacitanciaTotal = 0;
for (int i = 0; i < numCapacitores; i++)
{
capacitanciaTotal += 1 / capacitancias[i];
}
return 1 / capacitanciaTotal;
}

private double calcularCapacitanciaParalelo(double[] capacitancias, int numCapacitores)
{
double capacitanciaTotal = 0;
for (int i = 0; i < numCapacitores; i++)
{
capacitanciaTotal += capacitancias[i];
}
return capacitanciaTotal;
}

private double[] obtenerValoresCapacitancias(int numCapacitores)
{
double[] capacitancias = new double[numCapacitores];
for (int i = 0; i < numCapacitores; i++)
{
string prompt = $"Ingrese el valor de capacitancia {i + 1} (F):";
string inputValue = Prompt.ShowDialog(prompt, "Valor de capacitancia");
if (!double.TryParse(inputValue, out capacitancias[i]))
{
MessageBox.Show("Ingrese un valor numérico válido.");
return null;
}
}
return capacitancias;
}

private void cmbOpciones_SelectedIndexChanged(object sender, EventArgs e)
{
int opcionIndex = cmbOpciones.SelectedIndex;
bool opcionRequiereCarga = opcionIndex == 0 || opcionIndex == 1 || opcionIndex == 2 || opcionIndex == 3 || opcionIndex == 4 || opcionIndex == 6;
bool opcionRequiereDiferenciaPotencial = opcionIndex == 0 || opcionIndex == 1 || opcionIndex == 2 || opcionIndex == 3 || opcionIndex == 5;
bool opcionRequiereDensidadSuperficial = opcionIndex == 3 || opcionIndex == 5;
bool opcionRequiereAreaSeparacion = opcionIndex == 4;
bool opcionRequiereNumCapacitores = opcionIndex == 6 || opcionIndex == 7;

lblCarga.Visible = opcionRequiereCarga;
txtCarga.Visible = opcionRequiereCarga;

lblDiferenciaPotencial.Visible = opcionRequiereDiferenciaPotencial;
txtDiferenciaPotencial.Visible = opcionRequiereDiferenciaPotencial;

lblDensidadSuperficial.Visible = opcionRequiereDensidadSuperficial;
txtDensidadSuperficial.Visible = opcionRequiereDensidadSuperficial;

lblArea.Visible = opcionRequiereAreaSeparacion;
txtArea.Visible = opcionRequiereAreaSeparacion;

lblSeparacion.Visible = opcionRequiereAreaSeparacion;
txtSeparacion.Visible = opcionRequiereAreaSeparacion;

lblNumCapacitores.Visible = opcionRequiereNumCapacitores;
txtNumCapacitores.Visible = opcionRequiereNumCapacitores;
}

private void MainForm_Load(object sender, EventArgs e)
{

}

private void MainForm_Load_1(object sender, EventArgs e)
{

}
}

public static class Prompt
{
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 300,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 200 };
Button confirmation = new Button() { Text = "Ok", Left = 150, Width = 75, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;

return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de GoliMan

Interfaz Gráfica C#

Publicado por GoliMan (1 intervención) el 02/06/2023 17:14:13
En el IDe tiene todo para diseñarlo, menu --> Project --> add form, diseñelo con los nombres que ud usa y listo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar