Doughts and crosses in windows form application - c#

can someone tell me why I can't run my program? I'm trying to create doughts and crosses in windows form applications I tried changing the code and stuff but I've tried everything but I think something is wrong with my function at the bottom. By now, I wanted to program to run, generate 9 buttons and when I click on them an "X" or "O" would appear depending whose turn is it.
PS. I haven't added the win condition function yet I wanted to test if the program is working as it should.
Thanks in advance.
public partial class Form1 : Form
{
Button[] gameButtons = new Button[9]; //array of buttons for markers(X's and O's)
bool cross = true; //cross is set to true if the next marker is to be a cross
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "More Complex Version of Noughts and Crosses";
this.BackColor = Color.BlanchedAlmond;
this.Width = 400;
this.Height = 400;
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 3) * 50;
int y = 50 + (i / 3) * 50;
this.gameButtons[i].Location = new System.Drawing.Point(x, y);
this.gameButtons[i].Name = "btn" + (index + 1);
this.gameButtons[i].Size = new System.Drawing.Size(50, 50);
this.gameButtons[i].TabIndex = i;
//this.gameButtons[i].Text = Convert.ToString(index);
this.gameButtons[i].UseVisualStyleBackColor = true;
this.gameButtons[i].Visible = true;
gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender,index);
this.Controls.Add(gameButtons[i]);
}
}
private void buttonHasBeenPressed(object sender,int i)
{
if (((Button)sender).Text == "")
{
if (cross == true)
{
((Button)sender).Text = "X";
gameButtons[i] = 'X';
}
else
{
((Button)sender).Text = "O";
gameButtons[i] = 'O';
}
cross = !cross;
}
}
}
Edit: The first problem was solved, thanks a lot to everyone :) But now I'm struggling to find the win condition. I've used this code but I'm getting a compiler error that I don't seem to understand how to fix it. This is the code I've made up:
private void threeInARow(int a, int b, int c, object sender)
{
if (gameButtons[a]==gameButtons[b] && gameButtons[a]==gameButtons[c])
{
if (gameButtons[a]='X')
{
MessageBox.Show("the winner is crosses");
}
else
{
MessageBox.Show("the winner is noughts");
}
}
The error is on my my first if it says "cannot implicitly convert type char to system.windows.forms.button"

Firstly, you have some compile errors:
// These two lines both throw the error:
// Cannot implicitly convert type 'char' to 'System.Windows.Forms.Button'
gameButtons[i] = 'X';
gameButtons[i] = 'O';
This is because you're trying to set a button to a character. This is not necessary, since you've already changed the text of the button, and you can remove these lines.
Next, you get a runtime exception when clicking on a button, on this line:
// The following line fails with the error:
// Unable to cast object of type 'WinFormTest.Form1' to type 'System.Windows.Forms.Button'.
if (((Button)sender).Text == "")
This is because the sender is the Form1 object, and not the button. The reason for this is that, in your assignment of the event to the button Click, you are passing sender instead of sender1 to the event, and, since the assignment of this event is happening in the Form.Load event, sender is the Form1. So you need to change the assignment to pass sender1 instead:
gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender1, index);
The next problem you're having (since you've modified the code in the original question) is in the threeInARow method here:
if (gameButtons[a] = 'X') // Error: Cannot implicitly convert type 'char' to 'Button'
The reason for this is that gameButtons is an array of Button objects, so gameButtons[a] represents a Button, and you can't assign the character 'X' to a Button (they are two different types). Since you've already assigned a value to the Text property of each button (which is of string type), you can just use that instead.
Also, you are using a single = sign, which is an assignment. You want to do a comparison, which uses a double == sign. So, putting these together, you will get:
if (gameButtons[a].Text == "X")
You have added similar problematic code to your buttonHasBeenPressed method, which you should just remove since we can compare the Text properties and don't need this additional assignement:
gameButtons[i] = 'X'; // Remove these invalid assignments
That takes care of the compile error, but you still have another problem in your comparisons with this line:
if (gameButtons[a] == gameButtons[b] && . . .
This line is asking if the Button reference in the array at index a is pointing to the exact same object as the Button reference in the array at index b. This will never be the case because you (correctly) initialized your array with 9 unique buttons.
What you really want to do is compare the Text property of each button, like so:
if (gameButtons[a].Text == gameButtons[b].Text && . . .
Lastly, you have included an Object parameter named sender to your method that you aren't using, so you might as well remove that (or do something with it in your method).
So, putting this all together you have:
private void threeInARow(int a, int b, int c)
{
if (gameButtons[a].Text == gameButtons[b].Text && gameButtons[a].Text == gameButtons[c].Text)
{
if (gameButtons[a].Text == "X")
{
MessageBox.Show("the winner is crosses");
}
else
{
MessageBox.Show("the winner is noughts");
}
}
}

See code below :
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
Button[] gameButtons = new Button[9]; //array of buttons for markers(X's and O's)
bool cross = true; //cross is set to true if the next marker is to be a cross
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "More Complex Version of Noughts and Crosses";
this.BackColor = Color.BlanchedAlmond;
this.Width = 400;
this.Height = 400;
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 3) * 50;
int y = 50 + (i / 3) * 50;
this.gameButtons[i].Location = new System.Drawing.Point(x, y);
this.gameButtons[i].Name = "btn" + (index + 1);
this.gameButtons[i].Size = new System.Drawing.Size(50, 50);
this.gameButtons[i].TabIndex = i;
//this.gameButtons[i].Text = Convert.ToString(index);
this.gameButtons[i].UseVisualStyleBackColor = true;
this.gameButtons[i].Visible = true;
gameButtons[i].Click += new EventHandler(buttonHasBeenPressed);
this.Controls.Add(gameButtons[i]);
}
}
private void buttonHasBeenPressed(object sender, EventArgs e)
{
if (((Button)sender).Text == "")
{
if (cross == true)
{
((Button)sender).Text = "X";
//gameButtons[i].Text = "X";
}
else
{
((Button)sender).Text = "O";
//gameButtons[i].Text = 'O';
}
cross = !cross;
}
}
}
}

Related

Label text not updating

public void ItemGot()
{
number = number + 1; //Increment by 1
quantity.Text = ("x" + number); //Overwrite Text
quantity.Refresh(); //Updates the text
}
Hello, I have this code above. When this method runs, the text of a label I set up earlier should change the text to the one i set below. However, its not doing it. Furthermore, by setting breakpoints in Visual Studio, I have determined that:
The method is being called,
Number is being incremented properly.
There should be NO reason why this not working, because the program is recognizing that number is increasing by one. My friend said a similar question here. Still no help, and now that code is outdated. Please help!
EDIT: how I added the quantity label
First, I initialized it in the constructor: public Label quantity;
Then I did this: quantity = new Label();
Lastly, in another method, I gave the quantity the following properties:
quantity.Size = new Size(24, 24);
quantity.Text = ("x" + number);
quantity.Left = 48;
Controls.Add(quantity);
number is also in the constructor and is set to 0.
EDIT 2 : I'll Post my whole method
public InventoryScreen()
{
btnItems = new Button();
quantity = new Label();
//call the methods for spawning the buttons
ButtonGenItems(cNumber, btnItems, quantity);
InitializeComponent();
}
public void InventoryScreen_Load(object sender, EventArgs e)
{
}
#region ButtonGenItems Method
public void ButtonGenItems(int cNumber, Button btnItems,Label quantity)
{
int xPos = 126;
int yPos = 25;
for (int n = 0; n < 1; n++)
{
btnItems.Tag = n;
btnItems.Size = new Size(48, 52); //Button size X and Y
btnItems.BackColor = Color.CornflowerBlue;
quantity.Size = new Size(24, 24);
quantity.Text = ("x" + number);
if (yPos > 60) // Five Buttons in one column
{
yPos = 25; //spawn position Y
xPos = xPos + btnItems.Width + 10; //spacing X
}
btnItems.Left = xPos; //Start Button spawn at the Left side
btnItems.Top = yPos; //Start spawn at the top side
quantity.Left = 48;
quantity.Top = 60;
yPos = yPos + btnItems.Height + 10;
btnItems.Text = "Use";
Controls.Add(btnItems); //place Buttons
Controls.Add(quantity);
// the Event of click Button
//btnItems.Click += new System.EventHandler(ItemUse); //to be implimented
}
}
#endregion
public void ItemGot()
{
//*Interestingly, the program recognizes that 'number' is increasing by 1, but label won't update the text
//Furthermore, pressing the actual button will trigger the text update, but simulating a buttonclick WONT DO ANYTHING
Console.WriteLine("Text should now increment by 1"); //Debugging to test method
number = number + 1; //Increment by 1
quantity.Text = ("x" + number); //Overwrite Text
}
}
2.5 This is how the method is being called. This method is located in another class
public void Update(Vector2 pos)
{
this.position = pos; //get char position
Inv = new InventoryScreen(); //create instance of object
charRange = new Rectangle((int)position.X, (int)position.Y, 64, 57); //create rectangle
//Intersection Code, If the character intersects with the item while the item is showing, run below
if (alive && charRange.Intersects(itemRect))
{
alive = false; //stop showing the item
Inv.ItemGot(); //Call the ItemGot class, which adds the item to the inventory screen
}
}
As I understand you have already open/showed form(instance of class InventoryScreen) with your label when you calling Update method, But...
Inside of method Update you creating a new instance of InventoryScreen, and calling function ItemGot with this new instance of form.
I think you need to pass reference of your current instance of InventoryScreen in method Update, then use that reference for calling ItemGot method
public void Update(Vector2 pos, InventoryScreen invscreen)
{
this.position = pos;
charRange = new Rectangle((int)position.X, (int)position.Y, 64, 57);
if (alive && charRange.Intersects(itemRect))
{
alive = false;
invscreen.ItemGot();
}
}

storing multiple controls and update radio button values?

What am doing here is; making a UI to update values visually, will add more support for other types too. Possibly all types.
updateIcons this function is called everytime the controller is loaded, and has new values,names everytime.
countControls to keep track of controllers, so if can update values on clicks.
myP is the object that holds the values taken at runtime, user shuffles values by pressing tab from another screen
created radiobuttons groupboxes to allow radiobutton group to be managed.
properties all belong to one object. each property has few possible values like in my example, the enums.
now am kinda lost, not sure how to best do this, as now my rb_CheckedChanged is returning some kind of mess.
How do i do this the right way ? all together, i feel its somewhat the right approach At least.
I thought of making a dictionary of ? to use it at the checked event. not exactly sure how
private void updateIcons(List<Props> prop) {
countControls++;
locationY = 10;
int gbHeight;
foreach (var p in prop) {
radioButtonY = 10;
IType pType = p.Type;
if (pType is Enum) {
var myP = new MyProp(p, this);
GroupBox gb = new GroupBox();
gb.Location = new Point(nextLocationX,locationY);
nextLocationX += rbWidth+10;
gb.Name = "groupBox" + countControls;
gb.Text = "smthn";
var TypesArray = set here;
gbHeight = TypesArray.Length;
foreach (var type in TypesArray) {
getimagesPath(TypesArray);
RadioButton rb = new RadioButton();
rb.Appearance = Appearance.Button;
rb.Width = rbWidth;
rb.Height = rbHeight;
rb.Name = type.Name + countControls;
rb.Text = type.Name;
string path = imagePaths[type.Name];
Bitmap rbImage = new Bitmap(path);
rb.BackgroundImage = rbImage;
countControls++;
rb.Location = new Point(radioButtonX, radioButtonY);
if (myP.Value != null && type.Name.SafeEquals(myP.Value.ToString())) {
rb.Checked = true;
}
radioButtonY += rbHeight;
gb.Controls.Add(rb);
rb.CheckedChanged += rb_CheckedChanged;
}
gb.Height = rbHeight * gbHeight + 20;
gb.Width = rbWidth + 10;
Controls.Add(gb);
}
}
}
void rb_CheckedChanged(object sender, EventArgs e) {
RadioButton rb = (RadioButton)sender;
Control control = (Control)sender;
if (rb.Checked) {
MessageBox.Show("You have just checked: " + rb.Text);
MessageBox.Show("You have just called Controller: " + control.Name);
var t = PropSeq;
}
else {
MessageBox.Show("you have just unchecked: " + rb.Text);
MessageBox.Show("You have just called Controller: " + control.Name);
}
}
I think your code might be a little messed up and not the easiest to read. It looks plain invalid with not all closing braces present? Try the code below which will create two group boxes, each with five radio buttons. This should help you achieve what you are trying to do (full listing for a basic Form):
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CreateButton();
}
private void CreateButton()
{
// Add two group boxes
for (int groupCount = 1; groupCount < 3; groupCount++)
{
var groupBox = new GroupBox();
groupBox.Location = new Point(220 * (groupCount - 1), 10);
groupBox.Name = string.Format("groupBox{0}", groupCount);
groupBox.Text = string.Format("Group Box {0}", groupCount);
// Add some radio buttons to each
for (int buttonCount = 1; buttonCount < 6; buttonCount++)
{
var radioButton = new RadioButton();
radioButton.Width = 150;
radioButton.Location = new Point(10, 30 * buttonCount);
radioButton.Appearance = Appearance.Button;
radioButton.Name = string.Format("radioButton{0}", buttonCount);
radioButton.Text = string.Format("Dynamic Radio Button {0} - {1}", groupCount, buttonCount);
radioButton.CheckedChanged += radioButton_CheckedChanged;
// Add radio button to the group box
groupBox.Controls.Add(radioButton);
groupBox.Height += 20;
}
// Add group box to form
Controls.Add(groupBox);
}
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
// Get button and only show the selected (not now de-selected item)
var radioButton = (RadioButton)sender;
if (radioButton.Checked)
{
MessageBox.Show("You have just checked: " + radioButton.Text);
}
}
}
}

Interchange positions of two buttons

I want to replace button location (Interchange location )by black button when i click it and it is next to black button (b9=black button and lable1 is a temp for saving location).
I made this method :
void checkLocation()
{
if (ActiveControl.Location == new Point(5, 7))//-----------for button 1
{
if (b9.Location == new Point(83, 7) || b9.Location == new Point(5, 71))
{
label1.Location = ActiveControl.Location;
ActiveControl.Location = b9.Location;
b9.Location = label1.Location;
}
}// it continue for every button
and I write this code for every button_click
private void button1_Click(object sender, EventArgs e)
{
checkLocation();
}
now,some button don't work currently . what is wrong ?
by thanks from p.s.w.g
I think it is shorter and fit :
void swapLocation()
{
var tmp = ActiveControl.Location;
if((ActiveControl.Location.X==b9.Location.X)&&(Math.Abs(b9.Location.Y-ActiveControl.Location.Y)<=60))
{
ActiveControl.Location = b9.Location;
b9.Location = tmp;
}
if ((ActiveControl.Location.Y == b9.Location.Y) && (Math.Abs(b9.Location.X-ActiveControl.Location.X) <= 70))
{
ActiveControl.Location = b9.Location;
b9.Location = tmp;
}
}
Just do this to swap the locations of two controls:
void swapLocation()
{
var tmp = ActiveControl.Location;
ActiveControl.Location = b9.Location;
b9.Location = tmp;
}
Or more generally
void swapLocation(Control x, Control y)
{
var tmp = x.Location;
x.Location = y.Location;
y.Location = tmp;
}
...
swapLocation(ActiveControl, b9);
Update
It looks like you're trying to implement a version of the 15-puzzle. There are numerous ways to solve this, but to avoid a radical rewrite of your program I'd recommend this:
private int buttonWidth = 82;
private int buttonHeight = 82; // adjust these values as needed
private void button_Click(object sender, EventArgs e)
{
if ((Math.Abs(ActiveControl.Location.X - b9.Location.X) == 0 &&
Math.Abs(ActiveControl.Location.Y - b9.Location.Y) == buttonHeight) ||
(Math.Abs(ActiveControl.Location.X - b9.Location.X) == buttonWidth &&
Math.Abs(ActiveControl.Location.Y - b9.Location.Y) == 0))
{
swapLocation(ActiveControl, b9);
}
}
This basically checks to see if the ActiveControl is either directly above, below, to the left, or the right of b9, and if it is, swaps them. You can use this click handler for all buttons 1 through 8. Note this method only works with the buttons are a fixed width and height.

change local function

I have problem with my C# WinForms project. I have a function that should change the place of buttons if they touch each other. For example, if I have btn1 at oldloction = (4,2) and btn2 at oldlocaction (2,6), then if I will move the buttons and they will touch bt1 new location = (2,6) and bt2 new location = (4,2)
now i did that with 2 buttons and it works.
locationx - means the x location on the button and its orgenize firat place of the location feat to the first buttons[0], the second feat to locationx[1] = buttons[1].location.x;
location - works the same ass locationx but uts the y locaion.
private void myText_MouseUp(object sender, MouseEventArgs e)
{
Point oldlocation = new Point(locationx[0], locationy[0]);
Point oldlocation2 = new Point(locationx[1], locationy[1]);
if (buttons[0].Location.Y == buttons[1].Location.Y)
{
buttons[1].Location = oldlocation;
buttons[0].Location = oldlocation2;
}
}
When I tried to make that as a global function it doesn't work and I don't know why.
This is the code of the global function that doesn't work:
private void myText_MouseUp(object sender, MouseEventArgs e)
{
for (int i = 0; i < counter; i++)
{
Point oldlocation = new Point(locationx[i], locationy[i]);
for (int j = 0; j < counter; j++)
{
if (i != j)
{
Point oldlocation2 = new Point(locationx[j], locationy[j]);
if (buttons[i].Location.Y != buttons[j].Location.Y)
{
buttons[j].Location = oldlocation2;
buttons[i].Location = oldlocation;
}
else if (buttons[i].Location.Y == buttons[j].Location.Y)
{
buttons[j].Location = oldlocation;
buttons[i].Location = oldlocation2;
}
}
}
}
}
Try using the button event for when it is pressed to call the function, rather than creating your own.
If the second function is not part of the control or form containing the buttons, it won't have a way to access the buttons array or locationx and locationy. You may need to pass these values as arguments to your function or ensure that they are provided as members of the class containing the second function. Note that generally a utility function would not take in "sender" and "MouseEventArgs" - pass only the specific data that the utility function needs to do its job.

DataGridView: Change Edit Control size while editing

in the DataGridView I want the cell size to expand according to the string length when I edit the cell. Excel does the same.
In the DataGridView, when entering edit mode, a DataGridViewTextBoxEditingControl is placed at the cell position. I tried to change the bounds/size of this control, but result is just a short flicker of my desired size. It gets directly overpainted the original, truncated way.
Any ideas on how to get this working?
Thanks,
Timo
You need to start by overriding the DataGridViewCell.PositionEditingPanel Method. You need to redefine your own type of column and your own type of cell to access this method.
Here is an example on how to do it, that multiply the size of the editing panel (the one that owns the editing control) by 2:
dataGridView1.AutoGenerateColumns = false; // disable columns auto generation
... add all columns
// add your special column
col = new MyColumn();
col.DataPropertyName = "Text"; // bind with the corresponding property
dataGridView1.Columns.Add(col); // add the custom column
... add other columns
public class MyCell : DataGridViewTextBoxCell
{
public override Rectangle PositionEditingPanel(Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
{
cellBounds.Width *= 2;
cellClip.Width = cellBounds.Width;
return base.PositionEditingPanel(cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
}
}
public class MyColumn : DataGridViewTextBoxColumn
{
public MyColumn()
{
CellTemplate = new MyCell();
}
}
This question is quite old but hopefully my answer helps somebody down the road. I ran across the same problem and was able to use a process similar to the following to make the column width update dynamically as the user typed, in order to ensure the text fit in the column.
Events used:
CellBeginEdit
CellEndEdit
EditingControlShowing
TextBoxKeyPressEvent (i.e. KeyPress)
NOTE: The following code assumes that AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
// ---------------------------------------------------------------------------
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
// Copies the original column width because switching to DataGridViewAutoSizeColumnMode.None
// will automatically make the column a default width.
int origColumnWidth = dataGridView1.Columns[e.ColumnIndex].Width;
dataGridView1.Columns[e.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
// Reverts back to the original width.
dataGridView1.Columns[e.ColumnIndex].Width = origColumnWidth;
}
// ---------------------------------------------------------------------------
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Columns[e.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
// ---------------------------------------------------------------------------
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
{
var tbox = (e.Control as TextBox);
// De-register the event FIRST so as to avoid multiple assignments (necessary to do this or the event
// will be called +1 more time each time it's called).
tbox.KeyPress -= TextBoxKeyPressEvent;
tbox.KeyPress += TextBoxKeyPressEvent;
}
}
// ---------------------------------------------------------------------------
private void TextBoxKeyPressEvent(object sender, KeyPressEventArgs e)
{
// Gets the text prior to the new character being added. Appending an arbitrary "0" to the value
// to account for the missing character when determining appropriate measurements.
string prevText = dataGridView1.CurrentCell.EditedFormattedValue.ToString() + "0";
Graphics editControlGraphics = dataGridView1.EditingControl.CreateGraphics();
// Gets the length of the current text value.
SizeF stringSize = editControlGraphics.MeasureString(prevText, dataGridView1.EditingControl.Font);
int widthForString = (int)Math.Round(stringSize.Width, 0);
// Makes the column width big enough if it's not already.
if (dataGridView1.CurrentCell.OwningColumn.Width < widthForString)
{
dataGridView1.CurrentCell.OwningColumn.Width = widthForString;
}
}
EDIT: Update to the TextBoxKeyPressEvent logic to account for Backspace:
private void TextBoxKeyPressEvent(object sender, KeyPressEventArgs e)
{
string prevText;
bool wasBackspaced = false;
// The following logic will either add or remove a character to/from the text string depending if the user typed
// an additional character or pressed the Backspace key. At the end of the day, the cell will (at least) be
// sized to the configured minimum column width or the largest row width in the column because we're using
// AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells.
if (e.KeyChar == Convert.ToChar(Keys.Back))
{
prevText = dataGridView1.CurrentCell.EditedFormattedValue.ToString();
if (prevText.Length == 0)
{
// Don't try to make it any smaller...
return;
}
// Remove an arbitrary character for determining appropriate measurements.
prevText = prevText.Remove(prevText.Length - 1);
wasBackspaced = true;
}
else
{
// Gets the text prior to the new character being added. Appending an arbitrary "0" to the value
// to account for the missing character when determining appropriate measurements.
prevText = dataGridView1.CurrentCell.EditedFormattedValue.ToString() + "0";
}
Graphics editControlGraphics = dataGridView1.EditingControl.CreateGraphics();
// Gets the length of the current text value.
SizeF stringSize = editControlGraphics.MeasureString(prevText, dataGridView1.EditingControl.Font);
int widthForString = (int)Math.Round(stringSize.Width, 0);
// Makes the column width big, or small, enough if it's not already.
if (dataGridView1.CurrentCell.OwningColumn.Width < widthForString || // 1. Applies when adding text
(dataGridView1.CurrentCell.OwningColumn.Width > widthForString && // ---
dataGridView1.CurrentCell.OwningColumn.MinimumWidth < widthForString && // 2. Applies when backspacing
wasBackspaced)) // ---
{
dataGridView1.CurrentCell.OwningColumn.Width = widthForString;
}
}
This was work for me:
Enable KeyPreview Property of the form and change the body of KeyPress Event of the form to this:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar!='\b') //discard backspace
{
dataGridView1.Columns[0].Width += 5; //the column's index or name
}
else
{
dataGridView1.Columns[0].Width -= 5; //for backspase pressing
}
}
you can limit the pressed keys with e.KeyChar ;
I mentioned in my other answer that I had two solutions, this one is the MeasureString solution (as opposed to the second datagridview solution)
any mention of textbox1-5.text e.t.c. has been commented it was just for debugging.
this, and the other solution, doesn't just resize the column you are editting in, it also resizes datagridview's width, and the form width, though you can easily comment that if you don't want that behaviour.
I should probably have used the 'uses' keyword for creating the graphics object, but anyhow.
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 datagridviewexpandcelldynamically
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
int origColumnWidth = dataGridView1.Columns[e.ColumnIndex].Width;
dataGridView1.Columns[e.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView1.Columns[e.ColumnIndex].Width = origColumnWidth;
if (dataGridView1.CurrentCell == null) dataGridView1.CurrentCell.Value = "";
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Columns[e.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
{
var tbox = (e.Control as TextBox);
// De-register the event FIRST so as to avoid multiple assignments (necessary to do this or the event
// will be called +1 more time each time it's called).
tbox.TextChanged -= TextBoxChanged;
tbox.TextChanged += TextBoxChanged;
}
}
private void TextBoxChanged(object sender, EventArgs e)
{
// try catch is helpful in a winforms program 'cos otherwise program might just stop.
// http://stackoverflow.com/questions/1583351/silent-failures-in-c-seemingly-unhandled-exceptions-that-does-not-crash-the-pr
try
{
int colindex = dataGridView1.CurrentCell.ColumnIndex;
Graphics agraphics = this.CreateGraphics();
SizeF headerTextSize = agraphics.MeasureString(dataGridView1.Columns[colindex].HeaderText, dataGridView1.EditingControl.Font);
// sometimes it goes black and this link here says to use editing control http://stackoverflow.com/questions/3207777/datagridview-cell-turns-black-when-accessing-editedformattedvalue
// string stredit=dataGridView1.CurrentCell.EditedFormattedValue.ToString();
string stredit=myDataGridView.EditingControl.Text;
SizeF curCellTextSize = agraphics.MeasureString(stredit, dataGridView1.EditingControl.Font);
//SizeF curCellTextSize = agraphics.MeasureString(dataGridView1.CurrentCell.GetEditedFormattedValue.ToString(), dataGridView1.EditingControl.Font);
int curCellTextSize_i = (int)Math.Round(curCellTextSize.Width, 0);
int headerCellSize = dataGridView1.Columns[colindex].Width;
textBox2.Text = headerTextSize.Width.ToString();
textBox3.Text = headerCellSize.ToString();
// find biggest existing one
int maxcelltextincol = (int)Math.Round(headerTextSize.Width,0);
// the max size, at least for the header, includes a bit of padding..
maxcelltextincol += 20;
int tempcelllength=0;
for(int i=0; i<dataGridView1.Rows.Count;i++) {
if (dataGridView1.Rows[i].Cells[colindex].Value == null) dataGridView1.Rows[i].Cells[colindex].Value = "";
tempcelllength = (int)Math.Round(agraphics.MeasureString(dataGridView1.Rows[i].Cells[colindex].Value.ToString(), dataGridView1.EditingControl.Font).Width, 0);
if (tempcelllength > maxcelltextincol) maxcelltextincol = tempcelllength;
}
// textBox2.Text = "PRE curCellTextSize_i=" + curCellTextSize_i + " " + "dgvw=" + dataGridView1.Columns[colindex].Width.ToString() + " max=" + maxcelltextincol.ToString() + " prevstringlength=";
string txtinwhatiamediting = stredit;
SizeF sizelengthoftxtinwhatiamediting = agraphics.MeasureString(txtinwhatiamediting, dataGridView1.Font); //intermediate
int lengthoftxtinwhatiamediting=(int)Math.Round(sizelengthoftxtinwhatiamediting.Width,0);
//if(lengthoftxtinwhatiamediting>maxcelltextincol)
int amountovermax = lengthoftxtinwhatiamediting - maxcelltextincol;
int oldcolwidth = dataGridView1.Columns[colindex].Width;
if (amountovermax < 0) { dataGridView1.Columns[colindex].Width = maxcelltextincol; return; }
dataGridView1.Columns[colindex].Width = maxcelltextincol + amountovermax;
int newcolwidth = dataGridView1.Columns[colindex].Width;
//dataGridView1.Width += (int)Math.Round((double)amountovermax,0);
dataGridView1.Width += newcolwidth - oldcolwidth;
this.Width += newcolwidth - oldcolwidth;
// textBox2.Text = "curCellTextSize_i=" + curCellTextSize_i + " " + "dgvw=" + dataGridView1.Columns[colindex].Width.ToString() + " max=" + maxcellincol.ToString();
if (curCellTextSize_i > maxcelltextincol) maxcelltextincol = curCellTextSize_i;
// textBox5.Text= "POST curCellTextSize_i=" + curCellTextSize_i + " " + "dgvw=" + dataGridView1.Columns[colindex].Width.ToString() + " max=" + maxcelltextincol.ToString() + "prevstring=" + prevString + " prevstringlength=" + prevtextsize + " diff=" + diff;
// textBox5.Text = "POST curCellTextSize_i=" + curCellTextSize_i + " " + "dgvw=" + dataGridView1.Columns[colindex].Width.ToString() + " max=" + maxcelltextincol.ToString() + " diff=" + amountovermax;
}
catch (Exception ee) { MessageBox.Show(ee.ToString()); }
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
//dataGridView1.AllowUserToAddRows = false;
dataGridView1.Font = new System.Drawing.Font("David", 30.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridView1.Rows.Add(1);
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
Graphics g = this.CreateGraphics(); // should be in a using.
Font fontA = new System.Drawing.Font("David", 30.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
SizeF headerSize = g.MeasureString(dataGridView1.Columns[0].HeaderText, fontA);
int totalcolwidth = dataGridView1.RowHeadersWidth + 40; // about 40+70
//MessageBox.Show(totalcolwidth.ToString());
for (int i = 0; i < dataGridView1.Columns.Count; i++)
totalcolwidth += dataGridView1.Columns[i].Width;
// MessageBox.Show(totalcolwidth.ToString());
// MessageBox.Show(dataGridView1.Width.ToString());
int diff = totalcolwidth - dataGridView1.Width;
dataGridView1.Width = totalcolwidth;
// MessageBox.Show(dataGridView1.Width.ToString());
this.Width += diff;
}
catch (Exception exc)
{
MessageBox.Show("exception ");
MessageBox.Show(exc.ToString());
}
}
}
}
I have found a few solutions for this
One uses MeasureString and one datagridview , another creates another datagridview for the purposes of figuring out the correct width of a cell if the cell were to have that content. Another(my latest one) manages it with one datagridview and adding and removing a row.
This is the one that uses a second datagridview
Draw a datagridview on a form i've given mine two columns no data.
The code will create a second datagridview also with two columns no data.
Of course the issue that the questioner ran into was that without the editing automatically autosizing, it's not clear what width to set the column. This solution creates another datagridview (call it DGVb), this one not added to the form. And it writes that data to a cell in DGVb, sees what width the cell took, and uses that figure as the figure to set the cell in the proper DGV.
Another issue covered by vine, is that with the cell set to autosize, you can't set the column's width programmatically, so you can put code on when the cellbeginedit event is triggered, to set autosize to none, and put it back on when cellendedit is triggered, and another thing is that because setting to none might immediately change the column size a bit e.g. column1 with autosize on might be 73 and then when you turn autosize off it goes to 100, so you can store the size before you put autosize to none, then put autosize to none and set the size to what it was, that way preserving that unwanted size change. That is what is done here, as covered by Vine.
this code expands and shrinks the column and doesn't have weaknesses with backdelete, forward delete or arrow keys, though as of writing, vine's answer has some weaknesses with those keys. I have used TextChanged to avoid those problems. (as opposed to keydown e.t.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 datagridviewexpandcelldynamically_with_second_dgv
{
public partial class Form1 : Form
{
DataGridView dgvtest = new DataGridView();
// DataGridView dgvtest;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
dgvtest.AllowUserToAddRows = false;
dataGridView1.CellBeginEdit += (object ssender, DataGridViewCellCancelEventArgs ee) =>
{
//keep column width as it is for now but just change autosize to none so will be able to manually increase it
int origColumnWidth = dataGridView1.Columns[ee.ColumnIndex].Width;
dataGridView1.Columns[ee.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView1.Columns[ee.ColumnIndex].Width = origColumnWidth;
};
dataGridView1.CellEndEdit += (object sssender, DataGridViewCellEventArgs eee) =>
{
dataGridView1.Columns[eee.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
};
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dgvtest.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgvtest.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dgvtest.Columns.Add("Column1", "Column1");
dgvtest.Columns.Add("Column2", "Column2");
dgvtest.Rows.Add(1);
dataGridView1.Rows.Add(1);
/*
Form newfrm = new Form();
newfrm.Show();
newfrm.Controls.Add(dgvtest);
dgvtest.Show();
*/
//dgvtest.Rows[0].Cells[0].Value = "abc";
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
{
var tbox = (e.Control as TextBox);
// De-register the event FIRST so as to avoid multiple assignments (necessary to do this or the event
// will be called +1 more time each time it's called).
tbox.TextChanged -= TextBoxChanged;
tbox.TextChanged += TextBoxChanged;
//not KeyDown 'cos the character has not appeared yet in the box. and one would have to check what it was as a parameter, and if it's a backdelete then go back one.. and also forward delete isn't coutned as a keydown.
//not KeyUp 'cos while yeah the character has at least appeared, there's a delay so if you hold backdelete then only after releasing it will it trigger the procedure, and updating the width of the cell then is a bit late.
//not KeyPress 'cos has issues of keyup.
}
}
private void TextBoxChanged(object sender, EventArgs e)
{
int colindex = dataGridView1.CurrentCell.ColumnIndex;
int oldcolwidth = dataGridView1.CurrentCell.Size.Width;
//string stredit=dataGridView1.CurrentCell.EditedFormattedValue.ToString();
string stredit=dataGridView1.EditingControl.Text;
dgvtest.Rows[0].Cells[0].Value = stredit;
int newcolwidth = dgvtest.Rows[0].Cells[0].Size.Width;
int headercellsize = dataGridView1.Columns[colindex].HeaderCell.Size.Width;
// find biggest existing one
int maxcellincol = headercellsize;
int tempcelllength = 0;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[colindex].Value == null) dataGridView1.Rows[i].Cells[colindex].Value = "";
//length of all others but not current.
tempcelllength = dataGridView1.Rows[i].Cells[colindex].Size.Width;
if (tempcelllength > maxcellincol) maxcellincol = tempcelllength;
}
int diffcol = newcolwidth - oldcolwidth;
// new isn't an ideal name.. 'cos it's not made new yet.. and 'cos if it's smaller than the max one then we won't make it the new one.. but it will be the new one if it's bigger than the max.
// txtdesc.Text = "";
txtdesc.Text += "newcolwidth=" + newcolwidth + "\r\n";
txtdesc.Text += "maxcellincol=" + maxcellincol + "\r\n";
//if (newcolwidth < maxcellincol) != even if = then fine.
dataGridView1.Columns[colindex].Width = newcolwidth;
dataGridView1.Width += diffcol;
}
}
}
The idea of expanding the cell as text is being typed is quite a hack.. but seems visually preferable to this alternative.. which is less tricky to do but doesn't look as nice, that is, to just have the cell expand in size on cellbeginedit, (so set to autosize to none, and set col width to some size like =50), and have it shrink back to size - autosize - on cellendedit) and then i suppose on cellendedit to increase datagridview width so it doesn't get a scroll bar. But then the datagridview col jumps in size and it's not nice to use.
This solution(my latest) uses a similar technique to the answer that uses 2 datagridviews, but it manages it with just one datagridview.
What it does is when text is typed into a cell, it creates a new row, and enters that text into that row at the corresponding column within that row. Then it sees what the new width should be and it expands the column to that width and removes that row.
It's an edit of what I had, that improves it.. Since it turned out that I could comment out the cellbeginedit and cellendedit method. And it also found that while the previous one was fine, a slight amendment would cause the bug of a black cell, and that is mentioned here. As long as i'm autosizing all columns I avoid the black cell. (that and using EditingControl.Text as I have)
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 dgveditresize
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//DGVprocs.dgv = dataGridView1;
dataGridView1.AllowUserToAddRows = false;
autoshrinkwholedgv();
//DGVprocs.autoshrink_off_wholedgv__preservewidths(); not necessary
dataGridView1.Rows.Add(5);
//dataGridView1.CellBeginEdit += OnCellBeginEditExpandCol;
dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;
// MessageBox.Show(dataGridView1.Columns[1].Width.ToString());
}
private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
// MessageBox.Show(dataGridView1.Columns[1].Width.ToString());
// http://stackoverflow.com/questions/37505883/how-can-i-dynamically-detect-the-characters-in-a-datagridview-cell-execute-co
//if(DGVprocs.isshrinkon()==false) { MessageBox.Show("err ")}
if (e.Control is TextBox)
{
var tbox = (e.Control as TextBox);
// De-register the event FIRST so as to avoid multiple assignments (necessary to do this or the event
// will be called +1 more time each time it's called).
tbox.TextChanged -= A_Cell_TextChanged;
tbox.TextChanged += A_Cell_TextChanged;
}
}
private void A_Cell_TextChanged(object sender, EventArgs e)
{
dataGridView1.Rows.Add(1);
//MessageBox.Show(dataGridView1.Rows.Count+" rows");
int colindex = dataGridView1.CurrentCell.ColumnIndex;
int oldcolwidth = dataGridView1.CurrentCell.Size.Width;
//string stredit=dataGridView1.CurrentCell.EditedFormattedValue.ToString();
string stredit = dataGridView1.EditingControl.Text;
//dgvtest.Rows[0].Cells[0].Value = stredit;
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[dataGridView1.CurrentCell.ColumnIndex].Value = stredit;
//MessageBox.Show(dataGridView1.Rows.Count + " rows");
//int newcolwidth = dgvtest.Rows[0].Cells[0].Size.Width;
//autoshrinkcurrentcol(); // WORSE (1) WW
autoshrinkwholedgv(); //added BETTER (2) XX
int newcolwidth = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[dataGridView1.CurrentCell.ColumnIndex].Size.Width;
autoshrinkoff_wholedgv_preservewidths(); //added BETTER (3) YY
// autoshrink_off_currentcol_preservewidth(); // WORSE (4) ZZ
/*
WAS ERROR WITH THIS ONE..
IF YOU TYPE IN THE FIRST CELL THEN HIT DOWN ARROW TWICE
THEN TYPE THEN IT GOES BLACK
BUT PROBLEM RESOLVED SINCE USING 2,3 RATHER THAN 1,4
*/
// doing either 1,4 or 2,3
// no comparison
// 1,4 causes blackness.
// 2,3 and it works
// all of them is just same as 2,3 not surprising.
// but funny that 1,4 causes blackness.
//MessageBox.Show("removing row");
if(dataGridView1.AllowUserToAddRows) { MessageBox.Show("programmer msg- issue in 'cell's textchanged method', allowusertoaddrows must be false otherwise an exception is thrown by the next line dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);"); Application.Exit(); }
// requires user not add row set to true.
dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
//MessageBox.Show(dataGridView1.Rows.Count + " rows");
int headercellsize = dataGridView1.Columns[colindex].HeaderCell.Size.Width;
// find biggest existing one
int maxcellincol = headercellsize;
int tempcelllength = 0;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[colindex].Value == null) dataGridView1.Rows[i].Cells[colindex].Value = "";
//length of all others but not current.
tempcelllength = dataGridView1.Rows[i].Cells[colindex].Size.Width;
if (tempcelllength > maxcellincol) maxcellincol = tempcelllength;
}
int diffcol = newcolwidth - oldcolwidth;
// new isn't an ideal name.. 'cos it's not made new yet.. and 'cos if it's smaller than the max one then we won't make it the new one.. but it will be the new one if it's bigger than the max.
txtdesc.Text = "";
txtdesc.Text += "newcolwidth=" + newcolwidth + "\r\n";
txtdesc.Text += "maxcellincol=" + maxcellincol + "\r\n";
//if (newcolwidth < maxcellincol) != even if = then fine.
// say we move that earlier
//dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
//DGVprocs.autoshrinkoff_preservecurrentcolwidth();
//if (dataGridView1.Columns[colindex].Width == newcolwidth)
if (oldcolwidth == newcolwidth)
txtwidthcomp.Text="old width is equal to cur width diff="+diffcol;
else
txtwidthcomp.Text="old width is not equal to cur width diff="+diffcol;
//shrink should never be on while there's an editbox showing.
//if (diffcol>0) if (DGVprocs.isshrinkon() == true) MessageBox.Show("shrink is on this may be why it's not resizing");
// when turning autoshrink off a)it should be done after the editbox it will freeze the editbox to the size that it was. b)when it is done it should be done in a preservational way. getting all col sizes beforehand and turning shrink off and setting all cols to that size that they were
// DGVprocs.autoshrinkoff();
// shrink has to be off for the current column.. doesn't matter about the rest of it.
// if(diffcol>0) if(DGVprocs.isshrinkoncurrentcol()==true) MessageBox.Show("shrink is on(cur col) this may be why it's not resizing");
dataGridView1.Columns[colindex].Width = newcolwidth;
dataGridView1.Width += diffcol;
// i think autoshrink while the editbox is showing is wrong.
// you need to autoshrink it to size of editbox.
// DGVprocs.autoshrink();
}
public void autoshrinkwholedgv()
{
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
return;
}
public void autoshrinkcurrentcol()
{
dataGridView1.Columns[getcurrentcol()].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
//this may be optional.
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
// DGVprocs.dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
return;
}
public int getcurrentcol()
{
if (dataGridView1.CurrentCell == null) { MessageBox.Show("Programmer msg - getcurrentcol() error, current cell not selected"); Application.Exit(); }
if (dataGridView1.CurrentCell.Value == null) dataGridView1.CurrentCell.Value = "";
return dataGridView1.CurrentCell.ColumnIndex;
}
public void autoshrink_off_currentcol_preservewidth()
{
int w = dataGridView1.Columns[getcurrentcol()].Width;
dataGridView1.Columns[getcurrentcol()].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView1.Columns[getcurrentcol()].Width = w;
}
public void autoshrinkoff_wholedgv_preservewidths()
{
// deal with the 73,100 bug.. whereby if you ave autoresize on immediately, then a DGV with Column1 Colum2, Column3 e.t.c. has width of 73. But then when turning autoresize off it goes to 100.
int[] colsizes = new int[dataGridView1.Columns.Count];
for (int i = 0; i < dataGridView1.Columns.Count; i++)
colsizes[i] = dataGridView1.Columns[i].Width;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
dataGridView1.Columns[i].Width = colsizes[i];
return;
}
}
}
Would you like the cell to resize as you type? Or would you like it to resize once the text is entered and enter is hit? The second option is by fair the easiest.
Let me know.
Thanks

Categories