DataGridView_CellValidating - sql

I have a datagridview that I would like to validate using the cellvalidating event. however as the user doesnt navigate between cells or rows in the datagridview. just enters data in a cell in the datagridview and then clicks a save button the cellvalidating event doesnt get fired.
any help would be greatly appreciated.

Before you save data, you can call ValidateChildren on the form to force all controls to validate themselves. the method will return false if there was a validation error. You use it like this:
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
If Me.ValidateChildren Then
...Save
End If
End Sub

It is obviously in the Win Forms environment.
Here is the answer to the problem.
I am accepting a datatable as my datasource for the gridview
DataTable dtSource = new DataTable();
#region Data Table Creation
dtSource.Columns.Add("NumericColumn1");
dtSource.Columns.Add("NumericColumn2");
dtSource.Columns.Add("NumericColumn3");
#endregion
#region Add Rows
dtSource.Rows.Add("1", "2", "3");
dtSource.Rows.Add("4", "5", "6");
dtSource.Rows.Add("7", "8", "9");
#endregion
dataGridView1.DataSource = dtSource;
My objective is to check if a user has entered anything in any of the cell apart from a numeric value, then upon clicking on the SAVE button an error message should be populated.
In the cell validating event , I have written the following
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "";
if (!(IsNumeric(e.FormattedValue.ToString(), System.Globalization.NumberStyles.Integer)))
{
flag = !flag;
dataGridView1.Rows[e.RowIndex].ErrorText = "Only numeric values are accepted";
}
else
{
flag = true;
}
}
The IsNumeric function is as under
public bool IsNumeric(string Val, System.Globalization.NumberStyles NumberStyle)
{
Double result;
return Double.TryParse(Val, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result);
}
And in the SAVE button event , I am checking the status of the validation
private void SAVE_Click(object sender, EventArgs e)
{
if (flag == true)
{
MessageBox.Show("Every thing is ok");
}
}
Hope this helps

private void dgv_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
if (dgv[e.ColumnIndex, e.RowIndex].IsInEditMode)
{
dgv.EndEdit();
}
}
}

Related

Adding quantity if the product is exist in the DataGridView

Can someone help me with my problem?
The picture below shows the same item I inputted. What I want is I don't like to show duplicate items in DataGridView. If the same product record adds, then the new will not be showed, it just add the quantity when clicking the "Save" button. And I don't know how to code it I'm just new to vb.net. Can somebody help me how to do it?? It would be a big help for me if you do, thank you so much!
Below is my code for Save button:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
initializeCon()
Dim found As Boolean = False
If (DataGridView1.Rows.Count > 0) Then
For Each row As DataGridViewRow In DataGridView1.Rows
If (Convert.ToString(row.Cells(1).Value) = dtDate.Text) And (Convert.ToString(row.Cells(2).Value) = txtProductCode.Text) AndAlso
(Convert.ToString(row.Cells(3).Value) = txtProductName.Text) Then
row.Cells(4).Value = Convert.ToString(txtQuantity.Text + Convert.ToInt16(row.Cells(4).Value))
found = True
End If
Next
If Not found Then
cmd = New SqlCommand("INSERT INTO tbl_productOrders VALUES('" & txtID.Text & "','" & dtDate.Text & "','" & txtProductCode.Text & "','" & txtProductName.Text & "'," & txtQuantity.Text & ");", con)
cmd.ExecuteNonQuery()
clrtxt()
SaveMsg()
Getdata()
End If
End If
End Sub
this is an axample to avoid duplicates while adding rows
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim exist As Boolean = False, numrow As Integer = 0, numtext As Integer
For Each itm As DataGridViewRow In DataGridView1.Rows
If itm.Cells(0).Value IsNot Nothing Then
If itm.Cells(0).Value.ToString = TextBox1.Text Then
exist = True
numrow = itm.Index
numtext = CInt(itm.Cells(1).Value)
Exit For
End If
End If
Next
If exist = False Then
DataGridView1.Rows.Add(New String() {TextBox1.Text, TextBox2.Text})
Else
DataGridView1.Rows(numrow).Cells(1).Value = CInt(TextBox2.Text) + numtext
End If
End Sub
There are two aspects to improve your program.
1. Make comparing products better
Since you need to check individual properties to find equal product, the better way is to overload == operator along with implementing IEquatable interface. You can read more here. When you do this, you can compare products with == operator: if (product1 == product2) { }. In this case three properties are being compared. If they all are the same, then two products are equal.
2. Make adding products to DataGridView easier
In order to make adding products to DataGridView easier, you need to leverage the handy binding mechanism in Windows Forms, which appeared in .NET Framework 2.0 - BindingSource. It acts like a medium between your data and controls. In order to make this class usable, you need to use another handy class - BindingList. You feed BindingList to BindingSource and assign BindingSource to DataSource property of DataGridView. After that you work only with BindingList collection (adding/removing/editing) - and all the propagation is done by BindinSource.
To summarize, here is the explanation and full code. Here you can download the project itself.
When you add product, the code first checks whether such product already exists in DataGridView (Add product button). Do note that we don't work with DataGridView directly - we work only with the collection. Thanks to enhancements, we can use LINQ here. If the product is not found, we add it to collection. If it's found, we just update the quantity.
If you need to update selected product data (Change quantity button), you only need to retrieve it from selected rows and use BoundDataItem - this is where our product is located. Then just update properties of product you need.
If you need to remove product (Delete product button), retrieve it from selected row and delete it from the collection.
Important! Do not forget to call Refresh() method on DataGridView after all taken actions in order to see the changes.
namespace WinFormsApp
{
public partial class Root : Form
{
private BindingList<Product> list = null;
private BindingSource bindingSource = null;
public Root() => InitializeComponent();
private void OnFormLoaded(object sender, EventArgs e)
{
// Our collection holding products
list = new BindingList<Product>();
// I've set up columns manually (with applied binding),
// so switch generating columns off.
dataGrid.AutoGenerateColumns = false;
// Disable adding new row
dataGrid.AllowUserToAddRows = false;
// Create our medium between grid and collection
bindingSource = new BindingSource { DataSource = list };
// Set binding
dataGrid.DataSource = bindingSource;
}
private void OnAddRecord(object sender, EventArgs e)
{
// Create new product
var new_product = new Product
{
Date = dtPicker.Value.ToShortDateString(),
Code = txtCode.Text,
Name = txtName.Text,
Quantity = npQuantity.Value
};
// No need to check individual properties here
// as == and IEquatable will do all the work.
// We can safely use LINQ here.
var p = list.FirstOrDefault(x => x == new_product);
if (p == null)
{
// Product is not found. Add it to list.
list.Add(new_product);
}
else
{
// Product is found. Update quantity.
p.Quantity += new_product.Quantity;
}
dataGrid.Refresh(); //Required to reflect changes
}
private void OnChangeQuantity(object sender, EventArgs e)
{
// Change quantity here.
var product = GetProduct();
if (product != null)
{
// Update product's quantity.
product.Quantity *= 2;
// Do not forget to refresh grid.
dataGrid.Refresh();
}
}
private void OnDeleteProduct(object sender, EventArgs e)
{
// Delete product here.
var product = GetProduct();
if (product != null)
{
// We need to delete product only from collection
list.Remove(product);
// Do not forget to refresh grid
dataGrid.Refresh();
}
}
// Retrieve product from selected row.
private Product GetProduct() =>
dataGrid.SelectedCells.Count == 0 ?
null :
dataGrid.SelectedCells[0].OwningRow.DataBoundItem as Product;
}
class Product : IEquatable<Product>
{
public string Date { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public decimal Quantity { get; set; }
// Overload == operator
public static bool operator ==(Product firstProduct, Product secondProduct)
{
// Check for null on left side.
if (Object.ReferenceEquals(firstProduct, null))
{
if (Object.ReferenceEquals(secondProduct, null))
{
// null == null = true.
return true;
}
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return firstProduct.Equals(secondProduct);
}
// Overload != operator (required when overloading == operator)
public static bool operator !=(Product firstProduct, Product secondProduct) =>
!(firstProduct == secondProduct);
// Implementing IEquatable<T> interface
public bool Equals(Product other)
{
// If 'other' is null, return false.
if (Object.ReferenceEquals(other, null))
{
return false;
}
// Optimization for a common success case.
if (Object.ReferenceEquals(this, other))
{
return true;
}
// If run-time types are not exactly the same, return false.
if (this.GetType() != other.GetType())
{
return false;
}
// Return true if the fields match.
return
Date == other.Date &&
Code == other.Code &&
Name == other.Name;
}
public override bool Equals(object obj) => this.Equals(obj as Product);
public override int GetHashCode() =>
Date.GetHashCode() + Code.GetHashCode() + Name.GetHashCode();
// Optional. For debugging purposes.
public override string ToString() =>
$"Date: {Date}, Code: {Code}, Name: {Name}, Quantity: {Quantity}";
}
}

Incorrect value using LINQ on DataGridView [duplicate]

I have a winforms app and want to trigger some code when a checkbox embedded in a DataGridView control is checked / unchecked. Every event I have tried either
Triggers as soon as the CheckBox is clicked but before its checked state changes, or
Triggers only once the CheckBox looses its focus
I can't seem to find event that triggers immediately after the checked state changes.
Edit:
What I am trying to achieve is that when the checked state of a CheckBox in one DataGridView changes, the data in two other DataGridViews changes. Yet all the events I have used, the data in the other grids only changes after the CheckBox in the first DataGridView looses focus.
To handle the DatGridViews CheckedChanged event you must first get the CellContentClick to fire (which does not have the CheckBoxes current state!) then call CommitEdit. This will in turn fire the CellValueChanged event which you can use to do your work. This is an oversight by Microsoft. Do some thing like the following...
private void dataGridViewSites_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
dataGridViewSites.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
/// <summary>
/// Works with the above.
/// </summary>
private void dataGridViewSites_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
UpdateDataGridViewSite();
}
P.S. Check this article https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx
I found #Killercam's solution to work but was a bit dodgy if the user double clicked too fast. Not sure if other's found that the case either. I found a another solution here.
It uses the datagrid's CellValueChanged and CellMouseUp. Changhong explains that
"The reason for that is OnCellvalueChanged event won’t fire until the DataGridView thinks you have completed editing. This makes senses for a TextBox Column, as OnCellvalueChanged wouldn’t [bother] to fire for each key strike, but it doesn’t [make sense] for a CheckBox."
Here it is in action from his example:
private void myDataGrid_OnCellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)
{
// Handle checkbox state change here
}
}
And the code to tell the checkbox it is done editing when it is clicked, instead of waiting till the user leaves the field:
private void myDataGrid_OnCellMouseUp(object sender,DataGridViewCellMouseEventArgs e)
{
// End of edition on each click on column of checkbox
if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)
{
myDataGrid.EndEdit();
}
}
Edit: A DoubleClick event is treated separate from a MouseUp event. If a DoubleClick event is detected, the application will ignore the first MouseUp event entirely. This logic needs to be added to the CellDoubleClick event in addition to the MouseUp event:
private void myDataGrid_OnCellDoubleClick(object sender,DataGridViewCellEventArgs e)
{
// End of edition on each click on column of checkbox
if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)
{
myDataGrid.EndEdit();
}
}
jsturtevants's solution worked great. However, I opted to do the processing in the EndEdit event. I prefer this approach (in my application) because, unlike the CellValueChanged event, the EndEdit event does not fire while you are populating the grid.
Here is my code (part of which is stolen from jsturtevant:
private void gridCategories_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == gridCategories.Columns["AddCategory"].Index)
{
//do some stuff
}
}
private void gridCategories_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == gridCategories.Columns["AddCategory"].Index)
{
gridCategories.EndEdit();
}
}
Here is some code:
private void dgvStandingOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvStandingOrder.Columns[e.ColumnIndex].Name == "IsSelected" && dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
{
bool isChecked = (bool)dgvStandingOrder[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
if (isChecked == false)
{
dgvStandingOrder.Rows[e.RowIndex].Cells["Status"].Value = "";
}
dgvStandingOrder.EndEdit();
}
}
private void dgvStandingOrder_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dgvStandingOrder_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
{
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
following Killercam'answer, My code
private void dgvProducts_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dgvProducts.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
and :
private void dgvProducts_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dgvProducts.DataSource != null)
{
if (dgvProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True")
{
//do something
}
else
{
//do something
}
}
}
This also handles the keyboard activation.
private void dgvApps_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(dgvApps.CurrentCell.GetType() == typeof(DataGridViewCheckBoxCell))
{
if (dgvApps.CurrentCell.IsInEditMode)
{
if (dgvApps.IsCurrentCellDirty)
{
dgvApps.EndEdit();
}
}
}
}
private void dgvApps_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// handle value changed.....
}
Ben Voigt found the best solution in a comment-reply above:
private void dgvStandingOrder_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
Seriously, that's ALL you need.
What worked for me was CurrentCellDirtyStateChanged in combination with datagridView1.EndEdit()
private void dataGridView1_CurrentCellDirtyStateChanged( object sender, EventArgs e ) {
if ( dataGridView1.CurrentCell is DataGridViewCheckBoxCell ) {
DataGridViewCheckBoxCell cb = (DataGridViewCheckBoxCell)dataGridView1.CurrentCell;
if ( (byte)cb.Value == 1 ) {
dataGridView1.CurrentRow.Cells["time_loadedCol"].Value = DateTime.Now.ToString();
}
}
dataGridView1.EndEdit();
}
It's all about editing the cell, the problem that is the cell didn't edited actually, so you need to save The changes of the cell or the row to get the event when you click the check box so you can use this function:
datagridview.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
with this you can use it even with a different event.
I have found a simpler answer to this problem. I simply use reverse logic. The code is in VB but it is not much different than C#.
Private Sub DataGridView1_CellContentClick(sender As Object, e As
DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim _ColumnIndex As Integer = e.ColumnIndex
Dim _RowIndex As Integer = e.RowIndex
'Uses reverse logic for current cell because checkbox checked occures
'after click
'If you know current state is False then logic dictates that a click
'event will set it true
'With these 2 check boxes only one can be true while both can be off
If DataGridView1.Rows(_RowIndex).Cells("Column2").Value = False And
DataGridView1.Rows(_RowIndex).Cells("Column3").Value = True Then
DataGridView1.Rows(_RowIndex).Cells("Column3").Value = False
End If
If DataGridView1.Rows(_RowIndex).Cells("Column3").Value = False And
DataGridView1.Rows(_RowIndex).Cells("Column2").Value = True Then
DataGridView1.Rows(_RowIndex).Cells("Column2").Value = False
End If
End Sub
One of the best things about this is no need for multiple events.
I've tried some answers from here, but I've always had some kind of problem (like double clicking or using the keyboard). So, I combined some of them and got a consistent behavior (it's not perfect, but works properly).
void gridView_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if(gridView.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell))
return;
if(!gridView.CurrentCell.IsInEditMode)
return;
if(!gridView.IsCurrentCellDirty)
return;
gridView.EndEdit();
}
void gridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) {
if(e.ColumnIndex == gridView.Columns["cFlag"].Index && e.RowIndex >= 0)
gridView.EndEdit();
}
void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
if(e.ColumnIndex != gridView.Columns["cFlag"].Index || e.RowIndex < 0)
return;
// Do your stuff here.
}
The Code will loop in DataGridView and Will check if CheckBox Column is Checked
private void dgv1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1)
{
dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit);
var i = 0;
foreach (DataGridViewRow row in dgv1.Rows)
{
if (Convert.ToBoolean(row.Cells[0].Value))
{
i++;
}
}
//Enable Button1 if Checkbox is Checked
if (i > 0)
{
Button1.Enabled = true;
}
else
{
Button1.Enabled = false;
}
}
}
In the event CellContentClick you can use this strategy:
private void myDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)//set your checkbox column index instead of 2
{ //When you check
if (Convert.ToBoolean(myDataGrid.Rows[e.RowIndex].Cells[2].EditedFormattedValue) == true)
{
//EXAMPLE OF OTHER CODE
myDataGrid.Rows[e.RowIndex].Cells[5].Value = DateTime.Now.ToShortDateString();
//SET BY CODE THE CHECK BOX
myDataGrid.Rows[e.RowIndex].Cells[2].Value = 1;
}
else //When you decheck
{
myDataGrid.Rows[e.RowIndex].Cells[5].Value = String.Empty;
//SET BY CODE THE CHECK BOX
myDataGrid.Rows[e.RowIndex].Cells[2].Value = 0;
}
}
}
The best way that I found (which also doesn't use multiple events) is by handling the CurrentCellDirtyStateChanged event.
private void dataGrid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridMatten.CurrentCell.OwningColumn == dataGridMatten.Columns["checkBoxColumn"] && dataGridMatten.IsCurrentCellDirty)
{
dataGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
//your code goes here
}
}
To do this when using the devexpress xtragrid, it is necessary to handle the EditValueChanged event of a corresponding repository item as described here. It is also important to call the gridView1.PostEditor() method to ensure the changed value has been posted. Here is an implementation:
private void RepositoryItemCheckEdit1_EditValueChanged(object sender, System.EventArgs e)
{
gridView3.PostEditor();
var isNoneOfTheAboveChecked = false;
for (int i = 0; i < gridView3.DataRowCount; i++)
{
if ((bool) (gridView3.GetRowCellValue(i, "NoneOfTheAbove")) && (bool) (gridView3.GetRowCellValue(i, "Answer")))
{
isNoneOfTheAboveChecked = true;
break;
}
}
if (isNoneOfTheAboveChecked)
{
for (int i = 0; i < gridView3.DataRowCount; i++)
{
if (!((bool)(gridView3.GetRowCellValue(i, "NoneOfTheAbove"))))
{
gridView3.SetRowCellValue(i, "Answer", false);
}
}
}
}
Note that because the xtragrid doesnt provide an enumerator it is necessary to use a for loop to iterate over rows.
Removing the focus after the cell value changes allow the values to update in the DataGridView. Remove the focus by setting the CurrentCell to null.
private void DataGridView1OnCellValueChanged(object sender, DataGridViewCellEventArgs dataGridViewCellEventArgs)
{
// Remove focus
dataGridView1.CurrentCell = null;
// Put in updates
Update();
}
private void DataGridView1OnCurrentCellDirtyStateChanged(object sender, EventArgs eventArgs)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
You can force the cell to commit the value as soon as you click the checkbox and then catch the CellValueChanged event. The CurrentCellDirtyStateChanged fires as soon as you click the checkbox.
The following code works for me:
private void grid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
SendKeys.Send("{tab}");
}
You can then insert your code in the CellValueChanged event.
I use DataGridView with VirtualMode=true and only this option worked for me
(when both the mouse and the space bar are working, including repeated space clicks):
private void doublesGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
var data_grid = (DataGridView)sender;
if (data_grid.CurrentCell.IsInEditMode && data_grid.IsCurrentCellDirty) {
data_grid.EndEdit();
}
}
private void doublesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == CHECKED_COLUMN_NUM && e.RowIndex >= 0 && e.RowIndex < view_objects.Count) { // view_objects - pseudocode
view_objects[e.RowIndex].marked = !view_objects[e.RowIndex].marked; // Invert the state of the displayed object
}
}
this worked for me
private void employeeDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == employeeDataGridView.Columns["employeeStatusColumn"].Index)
{
bool isChecked = (bool)employeeDataGridView.CurrentCell.Value;
if (isChecked)
{
MessageBox.Show("Checked " + isChecked); //out true;
}
else
{
MessageBox.Show("unChecked " + isChecked);
}
}
}
private void employeeDataGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (employeeDataGridView.DataSource != null)
{
if (e.ColumnIndex == employeeDataGridView.Columns["employeeStatusColumn"].Index && e.RowIndex != -1)
{
employeeDataGridView.EndEdit();
}
}
}
private void dataGridViewPendingBill_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
bool isChecked = (bool) dataGridViewPendingBill[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
if (isChecked)
{
totalAmount += int.Parse(dataGridViewPendingBill.Rows[e.RowIndex].Cells["Amount"].Value.ToString());
textBoxAmount.Text = totalAmount.ToString();
}
else
{
totalAmount -= int.Parse(dataGridViewPendingBill.Rows[e.RowIndex].Cells["Amount"].Value.ToString());
textBoxAmount.Text = totalAmount.ToString();
}
dataGridViewPendingBill.EndEdit();
}

change the Label of a button in ribbon word add in

I'm trying to create a simple Word add-in.
I the Ribbon a have create a button and I want to change its label depending on the type of word document.
the document is a mail merge that has two types of models: Demand and Response.
I want to have button label: CREATE DEMAND for Demmand and CREATE RESPONSE for Response.
I tried something like this, but it does not work:
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
//Word.MailMerge mailMerge = doc.MailMerge;
if (doc.MailMerge.DataSource.FieldNames.Count!=0)
{
foreach (Microsoft.Office.Interop.Word.MailMergeFieldName f in doc.MailMerge.DataSource.FieldNames)
{
if (f.Name.Equals("Response"))
{
this.button1.Label = "Create a response";
break;
}
else if (f.Name.Equals("Demand"))
{
this.button1.Label = "Create a demand";
break;
}
}
}
}
the value of doc.MailMerge.DataSource.FieldNames.Count is always equal to 0
Do you have any idea how i can do it?
The visual designer for ribbons in word is a little limited, you can get much more functionality by using an XML ribbon, although it is a little more work.
You could do handle this by creating two buttons and then creating a GetVisible method in your ribbon code. The XML would be the following:
<button id="ButtonCreateDemand" label="Create Demand" size="normal"getVisible="GetVisible" onAction="Call_CreateDemand"/>
<button id="ButtonCreateResponse" label="Create Response" size="normal"getVisible="GetVisible" onAction="Call_CreateResponse"/>
The code would be something like this
public bool GetVisible(Office.IRibbonControl control)
{
//Check for demand\response
switch(control.Id.ToLower())
{
case "buttoncreatedemand":
return GetDemand();
case "buttoncreateresponse":
return !GetDemand();
}
}
private bool GetDemand()
{
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
//Word.MailMerge mailMerge = doc.MailMerge;
if (doc.MailMerge.DataSource.FieldNames.Count!=0)
{
foreach (Microsoft.Office.Interop.Word.MailMergeFieldName f in doc.MailMerge.DataSource.FieldNames)
{
if (f.Name.Equals("Response"))
{
return false;
}
}
}
return true;
}
Alternatively you could use a GetLabel() method to return the label for the button.
You add the DocumentChange() event to the ThisAddIn class
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Globals.ThisAddIn.Application.DocumentChange += new Word.ApplicationEvents4_DocumentChangeEventHandler(DocumentChange);
}

Textbox input range of numbers

I'm trying to handle my textbox input values. I want the user to only be able to input numbers within a range using KeyPress. Ex. (0 - 1000). I have the code to prevent any input thats not a number. I can't quite figure out how to prevent the user from inputting a value thats not within a certain range.
Private Sub txt2x6LumberQuanity_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt2x6LumberQuanity.KeyPress
If Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If
End Sub
Does anybody have any suggestions. I've spent a couple hours searching but can't seem to find the right solution.
I would use the text changed and ErrorProvider component for this effect:
Valid Entry
Invalid Entry
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int User2x6LumberQuantity
{
get
{
int x;
if (int.TryParse(txt2x6LumberQuantity.Text, out x))
{
return x;
}
return 0;
}
}
private void txt2x6LumberQuantity_TextChanged(object sender, EventArgs e)
{
errorProvider1.SetError(txt2x6LumberQuantity, null);
int x=User2x6LumberQuantity;
if (x<0||x>1000)
{
errorProvider1.SetError(txt2x6LumberQuantity, "Value Must Be (0-1000)");
continueButton.Enabled=false;
}
else
{
continueButton.Enabled=true;
}
}
}
You could add this to the Keypress event handler
If Char.IsNumber(e.KeyChar) Then
Dim newtext As String = TextBox1.Text.Insert(TextBox1.SelectionStart, e.KeyChar.ToString)
If Not IsNumeric(newtext) OrElse CInt(newtext) > 1000 OrElse CInt(newtext) < 0 Then e.Handled = True
End If

VSTO Outlook 2007 Add-in Context Menu CommandBarButton click event

I need to add a button to the context menu of the inbox. I have this working fine. What I need to figure out is in the implementation of the event handler how do I determine which item/items was clicked?
private void AddIn_Startup(object sender, EventArgs e)
{
Application.ItemContextMenuDisplay += Application_ItemContextMenuDisplay;
}
private void Application_ItemContextMenuDisplay(CommandBar commandBar, Selection selection)
{
commandBar.Controls[1].BeginGroup = true; // add seperator before first menu
var cmdButtonCopy = (CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, 1, Missing.Value, 1, Missing.Value);
cmdButtonCopy.Caption = "&Copy Message";
cmdButtonCopy.Click += cmdButtonCopy_Click;
}
private void cmdButtonCopy_Click(CommandBarButton ctrl, ref bool canceldefault)
{
// no sender/event args to determine which item was clicked ...
}
In the cmdButtonCopy_Click event handler I need to copy the specific item that was right-clicked but I can't figure out how to tell which item was clicked.
You can use tag property inside the CommandBarButton
I have written the code to solve your issue have a look at it:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemContextMenuDisplay += new ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
}
void Application_ItemContextMenuDisplay(CommandBar CommandBar, Selection Selection)
{
CommandBarButton mycmdbarbtn = (CommandBarButton)CommandBar.Controls.Add(MsoControlType.msoControlButton,missing, missing, 1,true);
mycmdbarbtn.Caption = "Test Button";
mycmdbarbtn.Click += new _CommandBarButtonEvents_ClickEventHandler(mycmdbarbtn_Click);
mailitm=Selection.Application.ActiveExplorer().Selection[1]; // to get the currently selected mailitem.
}
void mycmdbarbtn_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
MessageBox.Show("The subject of the clicked mail is " + mailitm.Subject);
}