I'm developing a calculator in visual studio 2017. Everything is working fine, but input from keyboard isn't working properly.
I use "&" in text property of a button, and it works, but problem is that it's printing on the screen like "&1 + &2". I attach a code and images so you guys can see what's happening.
1 - result picture
2 - usage of "&" symbol
Thanks in advance,
Best regards,
Ram
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;
namespace Calculator
{
public partial class Form1 : Form
{
Double resultado_value = 0; // result is zero in the beginning
String operationPerformed = "";
bool is_pressed = false;
public Form1()
{
InitializeComponent();
}
private void button_click(object sender, EventArgs e)
{
if ((textBox_Result.Text == "0") || (is_pressed))
textBox_Result.Clear();
is_pressed = false;
Button button = (Button)sender;
if (button.Text == ".") //to avoid repetitive dots
{
if(!textBox_Result.Text.Contains("."))
textBox_Result.Text = textBox_Result.Text + button.Text;
}else
textBox_Result.Text = textBox_Result.Text + button.Text;
}
private void operator_click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultado_value != 0) //if result value not equal to zero
{
button15.PerformClick();
operationPerformed = button.Text;
labelCurrentOperation.Text = resultado_value + " " + operationPerformed;
is_pressed = true;
}
else
{
operationPerformed = button.Text;
resultado_value = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = resultado_value + " " + operationPerformed;
is_pressed = true;
}
}
//Clear entry
private void button4_Click(object sender, EventArgs e)
{
textBox_Result.Text = "0";
}
//button Clear
private void button5_Click(object sender, EventArgs e)
{
// this.BackColor = System.Drawing.Color.White;//can't find color "control"
textBox_Result.Text = "0";
resultado_value = 0;
}
// equal button
private void button15_Click(object sender, EventArgs e)
{
switch (operationPerformed)
{
case "+":
// this.BackColor = System.Drawing.Color.Red;//form change color to red
textBox_Result.Text = (resultado_value + Double.Parse(textBox_Result.Text)).ToString();
break;
case "-":
// this.BackColor = System.Drawing.Color.Aqua;
textBox_Result.Text = (resultado_value - Double.Parse(textBox_Result.Text)).ToString();
break;
case "X":
// this.BackColor = System.Drawing.Color.AliceBlue;
textBox_Result.Text = (resultado_value * Double.Parse(textBox_Result.Text)).ToString();
break;
case "รท":
// this.BackColor = System.Drawing.Color.BlueViolet;
textBox_Result.Text = (resultado_value / Double.Parse(textBox_Result.Text)).ToString();
break;
default:
break;
}
resultado_value = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void labelCurrentOperation_Click(object sender, EventArgs e)
{
}
}
}
If I understand what you are trying to do correctly then what you want to do is catch keypresses at the Form level. If this is what you want you should set the KeyPreview Property of your Form to true and override the OnKeyPress Method of the Form or add an Event Handler of KeyPressed and assign this to the Forms KeyPressed Event and do your thing there.
If you want me to provide an example let me know.
Ram Pawar I have written a quick example for you.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Formkeypress
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyPreview = true;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (e.KeyChar == 'r') BackColor = Color.Red;
if (e.KeyChar == 'b') BackColor = Color.Blue;
if (e.KeyChar == 'g') BackColor = Color.Green;
}
}
}
Basicly if you type 'r' here the Form will change its Background color to Red. Typing 'b' will change it to Blue, typing 'g' will change it to Green.
Please note that you have to set KeyPreview to true in the constructor for this to work.
I override the OnKeyPress event here as this is the prefered way to add logic to an event when deriving from a Control or Form. You can however just attach a KeyPress event handler to the Form if you wish with the same code block as the OnKeyPress method.
Also rermove the '&'s from your Text Properties.
Hope this help
Danny
Related
We're doing C# this week. It's the Lunch Order assignment, and the groupBox for my Add-ons menu does not populate until I check another main course. The Add-ons are all Checkboxes inside the groupBox.
I thought I would overcome this by setting the first main course item (hamburger) as checked by default. Did not fix it, and so I thought maybe I should have one of the add-on menu check-boxes checked by default... This also did not improve anything.
This is what it looks like when I run the app:
LunchOrderLaunch No add-ons
And when I check another main course option:
LunchOrderMainCourseChange Add-ons show
Here's the code (yes things are out of order because I am new to this and trying to work around things. Apologies for that in advance:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LunchAppLAB1_V2
{
public partial class frmLunchOrder : Form
{
// Global declaration
decimal Subtotal;
decimal Tax = 0.05m;
decimal OrderTotal;
public frmLunchOrder()
{
InitializeComponent();
}
private void grbxAddOns_CheckChanged(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
string message = "Hey! Thanks for dining with us today!\n Will you be coming
back?";
DialogResult button =
MessageBox.Show(message, "Dear Customer",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (button == DialogResult.No)
{
this.Close();
}
if (button == DialogResult.Yes)
{
MessageBox.Show("Hey! Thanks dude! Hope to see you soon!", "Greeting
Message");
this.Close();
}
}
private void btnPlaceOrder_Click(object sender, EventArgs e)
{
decimal add = 0m;
if (chkbx1.Checked)
{
add++;
}
if (chkbx2.Checked)
{
add++;
}
if (chkbx3.Checked)
{
add++;
}
if (rdoHamburger.Checked)
{
decimal Hamburger = Convert.ToDecimal(rdoHamburger.Checked);
Hamburger = 6.95m;
Subtotal = Hamburger + (add * .75m);
Tax = Subtotal * 0.05m;
OrderTotal = Tax + Subtotal;
txtSubtotal.Text = Subtotal.ToString("c");
txtTax.Text = Tax.ToString("c");
txtOrderTotal.Text = OrderTotal.ToString("c");
}
else if (rdoPizza.Checked)
{
decimal Pizza = Convert.ToDecimal(rdoPizza.Checked);
Pizza = 5.95m;
Subtotal = Pizza + (add * .50m);
Tax = Subtotal * 0.05m;
OrderTotal = Tax + Subtotal;
txtSubtotal.Text = Subtotal.ToString("c");
txtTax.Text = Tax.ToString("c");
txtOrderTotal.Text = OrderTotal.ToString("c");
}
else if (rdoSalad.Checked)
{
decimal Salad = Convert.ToDecimal(rdoSalad.Checked);
Salad = 4.95m;
Subtotal = Salad + (add * .25m);
Tax = Subtotal * 0.05m;
OrderTotal = Tax + Subtotal;
txtSubtotal.Text = Subtotal.ToString("c");
txtTax.Text = Tax.ToString("c");
txtOrderTotal.Text = OrderTotal.ToString("c");
}
}
private void rdoHamburger_CheckedChanged(object sender, EventArgs e)
{
grbxAddOns.Text = "Add-on items($.75/each)";
chkbx1.Text = "Lettuce, Tomato, and Onions";
chkbx2.Text = "Ketchup, Mustard, and Mayonnaise";
chkbx3.Text = "French Fries";
}
private void rdoPizza_CheckedChanged(object sender, EventArgs e)
{
grbxAddOns.Text = "Add-on items($.50/each)";
chkbx1.Text = "Pepperoni";
chkbx2.Text = "Sausage";
chkbx3.Text = "Olives";
}
private void rdoSalad_CheckedChanged(object sender, EventArgs e)
{
grbxAddOns.Text = "Add-on items($.25/each)";
chkbx1.Text = "Croutons";
chkbx2.Text = "Bacon Bits";
chkbx3.Text = "Bread Sticks";
}
private void btnReset_Click(object sender, EventArgs e)
{
RecursiveClearTextBoxes(this.Controls);
}
private void RecursiveClearTextBoxes(Control.ControlCollection cc)
{
foreach (Control ctrl in cc)
{
TextBox tb = ctrl as TextBox;
if (tb != null)
tb.Clear();
else
RecursiveClearTextBoxes(ctrl.Controls);
}
}
}
}
All of the radio buttons for the main course option have been coded with a CheckedChanged instruction. So I understand that they don't fire until the button is physically checked (and the checked button changes) So I am wondering how to do this and have the add-ons populate to begin with. I've been trying to find a solution since yesterday, and now I'm pretty much out of time. I'm a C# noob with this assignment due tomorrow...
In the constructor, add this line after InitializeComponent();
public frmLunchOrder()
{
InitializeComponent();
rdoHamburger_CheckedChanged(null, null); // <-- this line
}
Your problem is that, even though you're setting one to be clicked by default, it's still not firing the event that sets the check box text.
So you can just call this event manually in the constructor.
I've been creating the game Simon in a windows form using C#. I'm having a problem in the labels that blink to show the pattern. When one label is required to blink twice (because it appears in the pattern twice) it will only blink once. Also, in general the labels will sometimes not blink in the correct order they are meant to (i.e the second in the pattern blinks before the first). Any assistance in how to fix this or in general how to improve on my code would be great. I have only been using C# for the last few weeks and it's part of a university project.
Have attached the code and a picture of what the windows form looks like.
Windows Form
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;
namespace Simon2
{
public partial class Form1 : Form
{
List<int> sequence = new List<int>();
Random rnd = new Random();
int number = 0;
public Form1()
{
InitializeComponent();
sequence.Add(rnd.Next(0, 4));
hey();
}
void hey()
{
foreach (int colour in sequence)
{
switch (colour)
{
case 0: {
timer1.Enabled = true;
break;
}
case 1: {
timer2.Enabled = true;
break;
}
case 2: {
timer3.Enabled = true;
break;
}
case 3: {
timer4.Enabled = true;
break;
}
}
}
}
void pattern(int colour)
{
if (sequence[number] == colour)
{
label1.Text = ("Score: " + sequence.Count);
sequence.Add(rnd.Next(0, 4));
number = 0;
hey();
}
else
{
MessageBox.Show("Fail!");
Application.Exit();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Red1.BackColor == Color.Transparent)
{
Red1.BackColor = Color.Red;
timer1.Interval = 300;
}
else
{
Red1.BackColor = Color.Transparent;
timer1.Interval = 300;
timer1.Stop();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Blue1.BackColor == Color.Transparent)
{
Blue1.BackColor = Color.Blue;
timer2.Interval = 300;
}
else
{
Blue1.BackColor = Color.Transparent;
timer2.Interval = 300;
timer2.Stop();
}
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Yellow1.BackColor == Color.Transparent)
{
Yellow1.BackColor = Color.Yellow;
timer3.Interval = 300;
}
else
{
Yellow1.BackColor = Color.Transparent;
timer3.Interval = 300;
timer3.Stop();
}
}
private void timer4_Tick(object sender, EventArgs e)
{
if (Green1.BackColor == Color.Transparent)
{
Green1.BackColor = Color.Lime;
timer4.Interval = 300;
}
else
{
Green1.BackColor = Color.Transparent;
timer4.Interval = 300;
timer4.Stop();
}
}
private void Red_Click(object sender, EventArgs e)
{
pattern(0);
}
private void Blue_Click(object sender, EventArgs e)
{
pattern(1);
}
private void Yellow_Click(object sender, EventArgs e)
{
pattern(2);
}
private void Green_Click(object sender, EventArgs e)
{
pattern(3);
}
}
}
I am not familiar with the game itself, my understanding is that one light after the other has to light up.
My suggestion: Use Thread.sleep (UI will not be responsive while this does it's thing), instead of timers, directly in the switch:
switch (colour){
case 0: {
Red1.BackColor = Color.Red;
Thread.Sleep(500);
Red1.BackColor = Color.Transparent;
break;
}
edit:
a better way would be to use a while loop which checks if a certain amount of ms elapsed and put Application.DoEvents(); in there
Here's the problem:
I'm using a MaskedTextBox for phones masks. But, the Mask must accept two kinds of mask, like, the default mask is like this (00) 0000-0000, but sometimes the mask need to have one more slot, like this (00) 0000-00000.
This process must be dynamic. If the user type more than 10 chars, the MaskedTextBox will change his own mask.
Some time ago, I made it using VB.Net, but now, I need to do this using C#.
Here's just a example using VB.NET that I made some time ago.
Private Sub MaskedTextValidacao_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
Me.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
If Me.Text.Contains(" ") Or Me.Text.EndsWith(" ") Or Me.Text.StartsWith(" ") Then
Me.Text.Replace(" ", "")
End If
If Me.Text.Count.Equals(10) Or Me.MaskCompleted.Equals(True) Then
Me.BackColor = Color.LightGreen
Me.Text = Me.Text.TrimEnd
ElseIf Me.Text = "" Then
Me.BackColor = Color.White
Else
Me.BackColor = Color.LightCyan
End If
End Sub
Now, I'm trying to make something better and using C#.
Can someone help me? I mean, just give me a light, because I'm stuck!
You can make it like this :
public class CustomMaskedBox : MaskedTextBox
{
public CustomMaskedBox()
{
this.MaskInputRejected += CustomMaskedBox_MaskInputRejected;
this.Enter += CustomMaskedBox_Enter;
this.Leave += CustomMaskedBox_Leave;
}
void CustomMaskedBox_Leave(object sender, EventArgs e)
{
if (this.MaskFull)
{
this.BackColor = Color.LightGreen;
}
else
{
this.Mask = "(00) 0000-0000";
this.BackColor = Color.LightGreen;
}
if (!this.MaskCompleted)
{
this.BackColor = Color.LightCoral;
}
}
void CustomMaskedBox_Enter(object sender, EventArgs e)
{
this.BackColor = Color.LightBlue;
}
void CustomMaskedBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
if (this.MaskFull)
{
this.Mask = "(00) 0000-00000";
this.BackColor = Color.LightYellow;
}
}
}
You are looking for this I think( where maskedTextBox1 is control on form): So when the form starts it defaults to the Mask of
this.maskedTextBox1.Mask = "(00) 0000-0000";
When the user enters the values and the Mask is full then you can change the mask again:
private void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (this.maskedTextBox1.MaskFull)
{
this.maskedTextBox1.Mask = "(00) 0000-00000";
}
}
You can customise the key press/key down events to pick up the values dynamically.
thanks for the answers. I tried something and it's working.
It's use colors to identify if the field are Right or not. It also modify the mask.
Most part of it are in the Leave Event.
Think:
User type a number with 10 digits (00) 0000-0000
User leave the field
Then, the User enter again in the field and add a new digit (00)
0000-00000
User leave the field
Again, the user enter in the field and remove some digit (00)
0000-0000
The EVENT LEAVE can handle with this. I just like to know how can I do it in a "Professional Way",
I mean, this MaskedTextBox will be a part of a Class with others Custom Controls, so...there's a better way to do something like I did?
Here's the code that I made, and again, thanks guys!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MaskedTextBox
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
if (maskedTextBox1.MaskFull)
{
maskedTextBox1.Mask = "(00) 0000-00000";
maskedTextBox1.BackColor = Color.LightYellow;
}
}
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
maskedTextBox1.BackColor = Color.LightBlue;
}
private void maskedTextBox1_Leave(object sender, EventArgs e)
{
if (maskedTextBox1.MaskFull)
{
maskedTextBox1.BackColor = Color.LightGreen;
}
else
{
maskedTextBox1.Mask = "(00) 0000-0000";
maskedTextBox1.BackColor = Color.LightGreen;
}
if (!maskedTextBox1.MaskCompleted)
{
maskedTextBox1.BackColor = Color.LightCoral;
}
}
}
}
I have a WinForm text editor.
I would like to be able to allow the user to undo and redo changes in the Rich Text Box, like they can in Microsoft Word.
I have spent the past week or so researching how to do this, and most results seem to be regarding graphics applications.
The standard richTextBox1.Undo(); gives disappointing results, as it undoes everything that the user has written.
Does anybody have any idea how I could implement effective undo/redo? Preferably one which undoes/redoes the action word-by-word as opposed to character-by-character.
This is a very basic idea, and I'm sure that many improvements could be made.
I would create a String Array and incrementally store the value of the RichTextBox (In the TextChanged event, under your own conditions) in the array. As you store the value, increment the value of a counter, say stackcount. When the user undoes, decrement the stackcount and set the RichTextBox.Text = array(stackcount). If they redo, then increment the value of the counter and set the value again. If they undo and then change the text, then clear all values onwards.
I am sure that many other people may have better suggestions/changes for this, so please post in comments and I will update, or edit it yourself!
Example in C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RedoUndoApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string[] RTBRedoUndo;
public int StackCount = 0;
public int OldLength = 0;
public int ChangeToSave = 5;
public bool IsRedoUndo = false;
private void Form1_Load(object sender, EventArgs e)
{
RTBRedoUndo = new string[10000];
RTBRedoUndo[0] = "";
}
private void undo_Click(object sender, EventArgs e)
{
IsRedoUndo = true;
if (StackCount > 0 && RTBRedoUndo[StackCount - 1] != null)
{
StackCount = StackCount - 1;
richTextBox1.Text = RTBRedoUndo[StackCount];
}
}
private void redo_Click(object sender, EventArgs e)
{
IsRedoUndo = true;
if (StackCount > 0 && RTBRedoUndo[StackCount + 1] != null)
{
StackCount = StackCount + 1;
richTextBox1.Text = RTBRedoUndo[StackCount];
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (IsRedoUndo == false && richTextBox1.Text.Substring(richTextBox1.Text.Length - 1, 1) == " ")//(Math.Abs(richTextBox1.Text.Length - OldLength) >= ChangeToSave && IsRedoUndo == false)
{
StackCount = StackCount + 1;
RTBRedoUndo[StackCount] = richTextBox1.Text;
OldLength = richTextBox1.Text.Length;
}
}
private void undo_MouseUp(object sender, MouseEventArgs e)
{
IsRedoUndo = false;
}
private void redo_MouseUp(object sender, MouseEventArgs e)
{
IsRedoUndo = false;
}
}
}
One way to do this is use the TextChanged event to periodically store the contents of richtextbox.text in an array or list, as a stack. When you undo, "pop the stack" and copy the most recent version on the stack into richtextbox.text.
TextChange can determine whether a change should be saved onto the stack, whether a new word, line, or character.
How can I retrieve text from DataGridView Cell and displayit on the button.
Basically I have two forms on my project (Form1 & Form2).
-In Form1 I have two buttons (Starter & Main). Both these buttons on click event, they call database sql-query and genereate into form the records as buttons.
-In Form2 I have a button (Starter). Also this button on click event calls database sql-query and generates records in DatagridView.
Now in Form2 when I double_click inside the cell under the Quantity In Stock column, a dialog-box pops up and allows me to enter the number in to that particular cell. Lets say Row-1:
Soup Starter 10 <Allways On Stock>
So based on this, how can I take the value of that cell = 10 and dispalyit on the bottom-right corner of the Button (in this case button Soup)
Like So:
##############
# #
# Soup #
# 10 #
##############
Could someone help me please and solve this problem....
Thanks in advance...
Kind regards
lapeci
here is the code of cellclick event of datagridview
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// make sure that the click was in the right column
if (e.ColumnIndex == 2) // I used 1 here because I didn't put a column for FoodType, you should use 2.
{
// Give it a value in case the cell is empty
string cellContent = "0";
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
{
cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
{
if (ib.ShowDialog() == DialogResult.OK)
{
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
cellContent = ib.Result;
}
}
}
}
And this is the InputBox dialog to enter quantity in to the cell...
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;
namespace DtposApplication
{
public partial class InputBox : Form
{
public InputBox(string text, string caption, string defaultValue)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
this.Text = caption; //.Clone().ToString();
Size size;
using (Graphics g = this.CreateGraphics())
{
Rectangle screen = Screen.PrimaryScreen.WorkingArea;
SizeF sizeF = g.MeasureString(text, lblPrompt.Font, screen.Width - 20);
size = sizeF.ToSize();
size.Width += 4;
}
if (size.Width < 310)
{
size.Width = 310;
}
Size clientSize = this.ClientSize;
clientSize.Width += size.Width - lblPrompt.Width;
clientSize.Height += size.Height - lblPrompt.Height;
this.ClientSize = clientSize;
lblPrompt.Text = text;
txtResult.Text = defaultValue;
this.DialogResult = DialogResult.Cancel;
}
void CancelButtonClick(object sender, System.EventArgs e)
{
result = null;
this.Close();
}
void AcceptButtonClick(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.OK;
result = txtResult.Text;
this.Close();
}
string result;
public string Result
{
get
{
return result;
}
}
private void btnSeven_Click(object sender, EventArgs e)
{
txtResult.Text += btnSeven.Text + "7";
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtResult.Text += btnTwo.Text + "2";
}
private void btnOne_Click(object sender, EventArgs e)
{
txtResult.Text += btnOne.Text + "1";
}
private void btnSix_Click(object sender, EventArgs e)
{
txtResult.Text += btnSix.Text + "6";
}
private void btnFive_Click(object sender, EventArgs e)
{
txtResult.Text += btnFive.Text + "5";
}
private void btnFour_Click(object sender, EventArgs e)
{
txtResult.Text += btnFour.Text + "4";
}
private void btnNine_Click(object sender, EventArgs e)
{
txtResult.Text += btnNine.Text + "9";
}
private void btnEight_Click(object sender, EventArgs e)
{
txtResult.Text += btnEight.Text + "8";
}
private void btnThree_Click(object sender, EventArgs e)
{
txtResult.Text += btnThree.Text + "3";
}
private void btnZero_Click(object sender, EventArgs e)
{
txtResult.Text += btnZero.Text + "0";
}
private void btnClear_Click(object sender, EventArgs e)
{
txtResult.Clear();
txtResult.Focus();
}
}
}
is the code how im creating buttons on form1 and then take the database records and asign values to these buttons
private void FoodAddButtons(DataTable table)
{
int xpos = 5;
int ypos = 5;
int space = 2;
VistaButtonTest.VistaButton newButton = null;
DtposMenuBS.Sort = "FoodPrice";
try
{
foreach (DataRowView dr in DtposMenuBS.List)
{
newButton = new VistaButtonTest.VistaButton();
newButton.ButtonText = dr["FoodName"].ToString();
newButton.AutoEllipsis = true;
newButton.Width = 152;
newButton.Height = 70;
newButton.CornerRadius = 4;
newButton.Font = new System.Drawing.Font("Arial Narrow", 15.00F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
newButton.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
newButton.ForeColor = System.Drawing.Color.Black;
newButton.HighlightColor = System.Drawing.Color.DarkGray;
newButton.GlowColor = System.Drawing.Color.DimGray;
if (xpos + newButton.Width > this.FoodMenuPanel.ClientSize.Width)
{
ypos += newButton.Height + space;
xpos = 5;
}
newButton.Location = new Point(xpos, ypos);
xpos += newButton.Width + space;
newButton.Click += ItemSelection1;
this.FoodMenuPanel.Controls.Add(newButton);
}
}
finally
{
DtposMenuBS.Sort = "";
}
}
You could put a label on top of the button exactly where you want it
and then update the label
Check this out:
Multiline Text as the button label - Windows Forms
At the time you get the value 10, try to set the button text property with this value. You should do some formatting (adding spaces accordingly) to fit the text in the bottom-right part of the button.
More detailed explanation:
When the dialog box pops up you enter the value 10. When you set this number in the datagrid cell (I think you have some kind of event handler doing this job) you should also set the button's text property btnSoup.Text = 10 within the same event handler. To align the text in the button, use the link I provided you above.