Jump to content

Erros de Mask


estimado
 Share

Recommended Posts

boas

Não, eu estou a utilizar MaskedTextBoxs

Aliás, onde eu coloquei o codigo é um "ComboBox"

Só se eu tiver alguns erros no codigo, poderão me ver sff?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace Sistema_Manutenção
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bdclienteDataSet1.Table_1' table. You can move, or remove it, as needed.
            this.table_1TableAdapter.Fill(this.bdclienteDataSet1.Table_1);

            this.reportViewer1.RefreshReport();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void cbmFiltrar_SelectedIndexChanged(object sender, EventArgs e)
        {
         
        }

        private void cbmFiltrar_SelectedIndexChanged_1(object sender, EventArgs e)
        {
          
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbmFiltrar.Text == "Código")
            {
                txtPesquisar.Mask = "";
            }
            if (cbmFiltrar.Text == "Nome")
            {
                txtPesquisar.Mask = "";

            }
            if (cbmFiltrar.Text == "CPF")
            {
                txtPesquisar.Mask = "000,000,000-00";
            }
            if (cbmFiltrar.Text == "")
            {
                txtPesquisar.Mask = "";
            }
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            try
            {
                if ((txtPesquisaFuncionario.Text != "") && (txtPesquisaMesAno.Text != ""))
                {
                    string sql = "SELECT * FROM tbBercario WHERE Funcionario LIKE '%" + txtPesquisaFuncionario.Text + "%' AND MesAno=" + txtPesquisaMesAno.Text + "";
                    cmd = new SqlCommand(sql, cn);
                    cn.Open();
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable tbBbercario = new DataTable();
                    da.Fill(tbBbercario);
                    tdBercarioDataGridView.DataSource = tbBbercario;
                }
                else if ((txtPesquisaFuncionario.Text != "") && (txtPesquisaMesAno.Text == ""))
                {
                    string sql = "SELECT * FROM tbBercario WHERE Funcionario LIKE '%" + txtPesquisaFuncionario.Text + "%'";
                    cmd = new SqlCommand(sql, cn);
                    cn.Open();
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable tbBbercario = new DataTable();
                    da.Fill(tbBbercario);
                    tdBercarioDataGridView.DataSource = tbBbercario;
                }
                else
                {
                    MessageBox.Show("Preencha os campos!");
                }

            }
            catch (Exception ex)
            {
                //Caso haja uma exceção
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //Fechar a conexão
                cn.Close();

            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            {
                //Limpar a GroupBox
                LimparCampo();

                //O Campo codFuncionario recebe da datagrid tbCliente a celula número 0 e assim por diante
                codFuncionarioTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[0].Value.ToString();
                nomeTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[1].Value.ToString();
                cpfMaskedTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[2].Value.ToString();
                telephoneMaskedTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[3].Value.ToString();
            }
        }
    }
}

Visual Studio 2010 C#

Link to comment
Share on other sites

Eu tenho o ComboBox!

Não sei porque que nao me da!

Podem-me ajudar?

O código que implementei está mesmo na ComboBox!

if (cbmFiltrar.Text == "Código")
            {
                txtPesquisar.Mask = "";
            }
            if (cbmFiltrar.Text == "Nome")
            {
                txtPesquisar.Mask = "";

            }
            if (cbmFiltrar.Text == "CPF")
            {
                txtPesquisar.Mask = "000,000,000-00";
            }
              if (cbmFiltrar.Text == "")
            {
                txtPesquisar.Mask = "";
            }

Agradecia uma ajuda vossa sff

obrigado desde já

Visual Studio 2010 C#

Link to comment
Share on other sites

boas

Como meti na imagem:

http://img535.imageshack.us/img535/4647/semttulovdf.png

Esta tudo correcto!

Será que tenho algum erro no codigo?

O codigo que estou a utilizar é este:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;



namespace Sistema_Manutenção
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bdclienteDataSet.tbcliente' table. You can move, or remove it, as needed.
            this.tbclienteTableAdapter.Fill(this.bdclienteDataSet.tbcliente);

        }

        private void cbmFiltrar_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbmFiltrar.Text == "Código")
            {
                txtPesquisar.Mask = "";
            }
            if (cbmFiltrar.Text == "Nome")
            {
                txtPesquisar.Mask = "";

            }
            if (cbmFiltrar.Text == "CPF")
            {
                txtPesquisar.Mask = "000,000,000-00";
            }
            if (cbmFiltrar.Text == "")
            {
                txtPesquisar.Mask = "";
            }
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            try
            {
                if ((txtPesquisaFuncionario.Text != "") && (txtPesquisaMesAno.Text != ""))
                {
                    string sql = "SELECT * FROM tbBercario WHERE Funcionario LIKE '%" + txtPesquisaFuncionario.Text + "%' AND MesAno=" + txtPesquisaMesAno.Text + "";
                    cmd = new SqlCommand(sql, cn);
                    cn.Open();
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable tbBbercario = new DataTable();
                    da.Fill(tbBbercario);
                    tdBercarioDataGridView.DataSource = tbBbercario;
                }
                else if ((txtPesquisaFuncionario.Text != "") && (txtPesquisaMesAno.Text == ""))
                {
                    string sql = "SELECT * FROM tbBercario WHERE Funcionario LIKE '%" + txtPesquisaFuncionario.Text + "%'";
                    cmd = new SqlCommand(sql, cn);
                    cn.Open();
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable tbBbercario = new DataTable();
                    da.Fill(tbBbercario);
                    tdBercarioDataGridView.DataSource = tbBbercario;
                }
                else
                {
                    MessageBox.Show("Preencha os campos!");
                }

            }
            catch (Exception ex)
            {
                //Caso haja uma exceção
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //Fechar a conexão
                cn.Close();
            }

        }
        private void LimparCampo()
        {
            foreach (Control c in Controls)
            {
                if (c is GroupBox)
                {
                    for (int i = 0; i < c.Controls.Count; i++)
                    {
                        if (c.Controls[i] is TextBox)
                        {
                            (c.Controls[i] as TextBox).Text = "";
                        }
                        if (c.Controls[i] is MaskedTextBox)
                        {
                            (c.Controls[i] as MaskedTextBox).Text = "";

                        }
                        if (c.Controls[i] is ComboBox)
                        {
                            (c.Controls[i] as ComboBox).Text = "";

                        }
                    }
                }
            }
        }

            private void tbClienteDataGridView_Click(object sender, EventArgs e)
        {
            //Limpar a GroupBox
            LimparCampo();

            //O Campo codFuncionario recebe da datagrid tbCliente a celula número 0 e assim por diante
            codFuncionarioTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[0].Value.ToString();
            nomeTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[1].Value.ToString();
            cpfMaskedTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[2].Value.ToString();
            telephoneMaskedTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[3].Value.ToString();
        }
        }
    }

Poderiam-me ver qual e o problema?

Visual Studio 2010 C#

Link to comment
Share on other sites

O teu problema é que estás a aplicar a Mask à textBox e não às maskedtextBoxes que tens, como já te foi dito. Vê se começas a ler o que te dizem.

O txtPesquisar é uma TexteBox, e não deveria ser.

"Nós somos o que fazemos repetidamente, a excelência não é um feito, e sim, um hábito."
Não respondo a questões por PM que possam ser colocadas no fórum!

Link to comment
Share on other sites

boas

Obrigado 😛

Só mais uma coisa!

Agora esta-me a aparecer outros erros!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;



namespace Sistema_Manutenção
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bdclienteDataSet.tbcliente' table. You can move, or remove it, as needed.
            this.tbclienteTableAdapter.Fill(this.bdclienteDataSet.tbcliente);

        }

        private void cbmFiltrar_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbmFiltrar.Text == "Código")
                {
                    //Define a instrução Sql
                    string sql = "SELECT * FROM tbCliente WHERE codFuncionario =" + txtPesquisar.Text + "";
                    //Lê os dados da variavel sql e conectar no cn
                    cmd = new SqlCommand(sql, cn);
                    //Abre conexão
                    cn.Open();
                    //Define o valor da CommandType para cmd
                    cmd.CommandType = CommandType.Text;
                    /*Representa um conjunto de comandos de dados e uma conexão de banco de dados
                     * que são usados para preencher o DataSet e atualizar um banco de dados SQL Server.*/
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    //Representa uma tabela de dados na memória.
                    DataTable cliente = new DataTable();
                    /* Adiciona ou atualiza linhas em um DataTable para que correspondam na fonte de
                     * dados usando o DataTable.*/
                    da.Fill(cliente);
                    /*A tbClienteDataGridView recebe o DataTable cliente*/
                    tbClienteDataGridView.DataSource = cliente;
                    //Fechar a conexão
                }
                if (cbmFiltrar.Text == "Nome")
                {
                    //define a instrução SQL
                    string sql = "SELECT * FROM tbCliente WHERE cliente LIKE '%" + txtPesquisar.Text + "%'";
                    cmd = new SqlCommand(sql, cn);
                    cn.Open();
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable cliente = new DataTable();
                    da.Fill(cliente);
                    tbClienteDataGridView.DataSource = cliente;
                }
                if (cbmFiltrar.Text == "CPF")
                {
                    //define a instrução SQL
                    string sql = "SELECT * FROM tbCliente WHERE CPF ='" + txtPesquisar.Text + "'";
                    cmd = new SqlCommand(sql, cn);
                    cn.Open();
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable cliente = new DataTable();
                    da.Fill(cliente);
                    tbClienteDataGridView.DataSource = cliente;
                }
                if (cbmFiltrar.Text == "")
                {
                    //define a instrução SQL
                    string sql = "SELECT * FROM tbCliente";
                    cmd = new SqlCommand(sql, cn);
                    cn.Open();
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable cliente = new DataTable();
                    da.Fill(cliente);
                    tbClienteDataGridView.DataSource = cliente;

                }

            }
            catch (Exception ex)
            {
                //Caso haja uma exceção
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //Fechar a conexão
                cn.Close();

            }
        }

            private void tbClienteDataGridView_Click(object sender, EventArgs e)
        {
            
        }
            private void LimparCampo()
            {
                foreach (Control c in Controls)
                {
                    if (c is GroupBox)
                    {
                        for (int i = 0; i < c.Controls.Count; i++)
                        {
                            if (c.Controls[i] is TextBox)
                            {
                                (c.Controls[i] as TextBox).Text = "";
                            }
                            if (c.Controls[i] is MaskedTextBox)
                            {
                                (c.Controls[i] as MaskedTextBox).Text = "";

                            }
                            if (c.Controls[i] is ComboBox)
                            {
                                (c.Controls[i] as ComboBox).Text = "";

                            }
                        }
                    }
                }
            }

            private void tbClienteDataGridView_Click(object sender, EventArgs e)
            {
                //Limpar a GroupBox
                LimparCampo();

                //O Campo codFuncionario recebe da datagrid tbCliente a celula número 0 e assim por diante
                codFuncionarioTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[0].Value.ToString();
                nomeTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[1].Value.ToString();
                cpfMaskedTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[2].Value.ToString();
                telephoneMaskedTextBox.Text = tbClienteDataGridView.CurrentRow.Cells[3].Value.ToString();
            }
            private void txtPesquisar_TextChanged(object sender, EventArgs e)
            {

            }

            private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
            {
                if (cbmFiltrar.Text == "Código")
                {
                    txtPesquisar.Mask = "";
                }
                if (cbmFiltrar.Text == "Nome")
                {
                    txtPesquisar.Mask = "";

                }
                if (cbmFiltrar.Text == "CPF")
                {
                    txtPesquisar.Mask = "000,000,000-00";
                }
                if (cbmFiltrar.Text == "")
                {
                    txtPesquisar.Mask = "";
                }
            }
        }
    }

A dizer:

O cn não existe no contexto

O cmd não existe no contexto

Podem-me ajudar novamente?

obrigado

Visual Studio 2010 C#

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.