Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textbox and at least 5 buttons, I want user to click any of 5 buttons then insert its value to the textbox.

If there is no value exists in the textbox then insert the clicked button value to the textbox, but if there are any existing value in textbox then add the "-" to separate the second input value.

Example: The first value should be 12 after click to insert the second button then textbox format should be 12-13.

I do something like this but still no luck:

C#
private void txtFixedTypeTwo_TextChanged(object sender, EventArgs e)
        {
            string sVal = txtFixedTypeTwo.Text;
            if (!string.IsNullOrEmpty(sVal))
            {
                txtGameTwoRandom.Enabled = true;
                txtCountGameTwo.Enabled = true;
                if (txtFixedTypeTwo.Text.Length > 11)
                {
                    MessageBox.Show("Only allow 4 animals", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtFixedTypeTwo.Text = txtFixedTypeTwo.Text.Substring(0, txtFixedTypeTwo.Text.Length - 3);
                }
                else
                {
                    sVal = sVal.Replace("-", "");
                    string newst = Regex.Replace(sVal, ".{2}", "$0-");
                    var nos = newst.Split('-');
                    List<string> tmp = new List<string>();
                    foreach (var item in nos)
                    {
                        if (!tmp.Contains(item))
                        {
                            tmp.Add(item);
                        }
                        else
                        {
                            MessageBox.Show("Duplicate entry detected", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            txtFixedTypeTwo.Text = txtFixedTypeTwo.Text.Substring(0, txtFixedTypeTwo.Text.Length - 3);
                        }
                    }
                    txtFixedTypeTwo.SelectionStart = txtFixedTypeTwo.Text.Length;
                    txtFixedTypeTwo.Focus();
                }
            }
        }


EDIT:
I tried another solution from Stackoverflow it works great;
C#
public void Button1_Click(object sender, EventArgs e)
{
    if(textBox.Text.Length > 0)
         textBox.Text += "-";

    Button butt = (Button)sender;

    textBox.Text += butt.Text;
}
Posted
Updated 27-Jan-15 22:06pm
v5
Comments
BillWoodruff 27-Jan-15 3:15am    
What exactly does each button insert ?
CeebLaj Thoj 27-Jan-15 3:28am    
Example: btn1 = "01", btn2 = "02", btn3 = "03", btn4 = "04", btn5 = "05"

This could be what you wanted to have:

MainWindow.xaml:
XML
<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="75"></Setter>
            <Setter Property="Height" Value="25"></Setter>
            <Setter Property="Margin" Value="5"></Setter>
        </Style>    
    </Window.Resources>
    
    <Grid>
        <StackPanel x:Name="stackPanelButtons" Button.Click="CommonClickHandler">
            <Button Content="01"/>
            <Button Content="02"/>
            <Button Content="03"/>
            <Button Content="04"/>
            <Button Content="05"/>
            <TextBlock x:Name="textBlock"></TextBlock>
        </StackPanel>
    </Grid>
</Window>


MainWindow.xaml.cs:
C#
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication10
{
    public partial class MainWindow : Window
    {
        List<string> values;

        public MainWindow()
        {
            values = new List<string>();

            InitializeComponent();
        }

        private void CommonClickHandler(object sender, RoutedEventArgs e)
        {
            string value = ((Button)e.Source).Content.ToString();

            if(values.Contains(value))
            {
                MessageBox.Show(this, string.Format("'{0}' already exists!", value), "Info", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                values.Add(value);
            }

            textBlock.Text = string.Join("-", values);

            e.Handled = true;
        }
    }
}
 
Share this answer
 
v5
Comments
Sinisa Hajnal 28-Jan-15 3:37am    
Good solution - my 5. How do you know its WPF? Other then that, it is well written...but you're not taking into consideration possibility of really having duplicate values on two buttons.
TheRealSteveJudge 28-Jan-15 3:45am    
Thank you for the vote and your appreciation!
Actually I had no clue whether it was Windows Forms or WPF.
I decided to use WPF as it allows event bubbling.
Duplicate values are actually handled.
A value is only added to the values list if it does not already exist.
Otherwise a MessageBox is shown.
Sinisa Hajnal 28-Jan-15 4:01am    
Indeed. And what if two buttons have the same value (duplicate, but legal?) - note, I have no idea if this is possible, but there is nothing in the description that prevents it.
TheRealSteveJudge 28-Jan-15 4:08am    
I just added another button with content "05" in my Visual Studio
in order to have two buttons with the same value "05".
When I press the first button with content "05" and then
the other button with content "05", it is handled and a MessageBox is shown
saying that Value "05" already exists.
Sinisa Hajnal 28-Jan-15 4:30am    
:) I know, that is exactly what I'm saying. You're not allowing two buttons to have same value. It could be that it is a legal situation - two buttons with same values...lets say you have day, month and year buttons...it would be legal to do 01-01-2015, but not pressing day button twice...regardless of the same outcome.
When you click a button, there is button click event in which you can add the hyphen

Something like this:
C#
protected void button_click(object sender, EventArgs e) {
    Button btn = CType(sender, Button)
    if (btn.Text != string.empty) {
        txtFixedTypeTwo.text += "-" + btn.Text;
    else
        txtFixedTypeTwo.Text = btn.Text;
    }
}


You should probably add same event to all buttons and add the check before the cast to see that the sender really is a button.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
v2
Comments
CeebLaj Thoj 27-Jan-15 2:30am    
My problem is each button apply to different textbox format, so I can't do that with button click event.
BillWoodruff 27-Jan-15 3:14am    
You need to explain what each button inserts.
Sinisa Hajnal 27-Jan-15 3:16am    
Of course you can, you just cannot have it all in a single button event. Assign in each button event whatever format is needed.
CeebLaj Thoj 27-Jan-15 21:38pm    
I have problem CType error, not exist in the current....
CeebLaj Thoj 27-Jan-15 3:32am    
The reason is because in some textbox condition I don't need to insert the "-" to separate the enter value, because they need to replace the actual textbox with the button value instead of put the "-" to separate the entered value.
write a method as

C#
private void AppendText(string text)
{
  if(!string.IsNullOrEmpty())
  {
     txtFixedTypeTwo.Text += "-" +  text;
  }
  else
  {
     txtFixedTypeTwo.Text = text;
  }
}


Call this method on click of each button and pass the value of button.Text. e.g

C#
protected void button_click(object sender, EventArgs e) 
{
    Button btn = CType(sender, Button)
    AppendText(btn.Text);
}
 
Share this answer
 
Comments
Sinisa Hajnal 28-Jan-15 3:35am    
This would fail for CType just like mine did (you just reorganized it).
Atish K. Singh 28-Jan-15 4:36am    
Don't use CType. Just pass the button text on the click event of button like:

protected void button1_click(object sender, EventArgs e)
{
AppendText(button1.Text);
}
Sinisa Hajnal 28-Jan-15 5:54am    
You can't if the same handler handles all five buttons...
Atish K. Singh 28-Jan-15 6:15am    
There is no need to have same handler for each button. Instead create separate handler for each button and call the method and pass the button's text in it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900