Custom caption next to CheckEdit Editor in XtraGrid - winforms

I have a XtraGrid bound to a Datasource, where I want a single column to have a Check Box in it, and have a caption next to the Check Box in the same cell. I can get the Checked True or False from another column in the Datasource, and also I need to get the caption text from another column in the Datasource...That part is easy, once I get a handle on implementing the caption changes.
My question is:
How do I programmatically change the text that is displayed next to the CheckEdit in the same cell?
Similar to (Tick and Untick represent the check box state):
Tick Apples
Untick Bananas
However, my attempts only display the text 'Check' - obviously the default caption in the CheckEdit editor:
Tick Check
Untick Check
I have searched the DevExpress support centre and they only have instructions for the CheckEdit Repository item when used in the TreeList (not the XtraGrid). I understand it will involve managing the CustomDrawCell event and some others - which is fine.

I think, your instructions for XtraTreeList can be also applied to XtraGrid. But I can suggest to you the other way.
You can use GridView.CustomRowCellEdit event and two ResositoryItemCheckEdit objects:
private RepositoryItemCheckEdit editTick;
private RepositoryItemCheckEdit editUntick;
Just set your Tick text to editTick.Caption property and Untick text to editUntick.Caption property.
Here is example:
//Initialize repository items:
editTick = new RepositoryItemCheckEdit() { GlyphAlignment = HorzAlignment.Near, Caption = "Apples" };
editUntick = new RepositoryItemCheckEdit() { GlyphAlignment = HorzAlignment.Near, Caption = "Bananas" };
//Add handler for CheckedChanged event:
Action<object, EventArgs> action = (s, e) =>
{
var ownerEdit = s as CheckEdit;
if (ownerEdit == null)
return;
ownerEdit.Text = ownerEdit.Checked ? editTick.Caption : editUntick.Caption;
};
editTick.CheckedChanged += new EventHandler(action);
editUntick.CheckedChanged += new EventHandler(action);
//Some sample DataSource:
var table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Bool", typeof(bool));
table.Rows.Add(0, false);
table.Rows.Add(1, false);
table.Rows.Add(2, true);
table.Rows.Add(3, false);
table.Rows.Add(4, true);
table.Rows.Add(5, true);
table.Rows.Add(6, false);
table.Rows.Add(7, true);
gridControl.DataSource = table;
//Here comes the CustomRowCellEdit:
gridView1.CustomRowCellEdit += (s, e) =>
{
if (e.Column.FieldName != "Bool" || e.CellValue == null) //Put your own field name here instead of "Bool".
return;
e.RepositoryItem = (bool)e.CellValue ? editTick : editUntick;
};
//Add CustomColumnDisplayText event, so you can see your "Apples" in group rows and filters:
gridView1.CustomColumnDisplayText += (s, e) =>
{
if (e.Column.FieldName != "Bool")
return;
e.DisplayText = (bool)e.Value ? editTick.Caption : editUntick.Caption;
};

Related

Script to Change selected item to a Swatch Color

I have a basic JavaScript that should change the selected item from CMYK Black to a Swatch Color Named Bronze but it's not working. Any advice on haw the code should look?
// Get the active document
var doc = app.activeDocument;
// Check if there is a selection
if (doc.selection.length > 0) {
// Get the first selected object
var obj = doc.selection[0];
// Check if the object has a fill
if (obj.fillColor > 0) {
// Set the object's fill color to the "Bronze" swatch
obj.fillColor = 5;
}
}
Change your code to this, this works, I have tested it.
// Get the active document
var doc = app.activeDocument;
// Check if there is a selection
if (doc.selection.length > 0) {
// Get the first selected object
var obj = doc.selection[0];
//change your code to lines below ---v
if (obj.filled)
obj.fillColor = activeDocument.swatches["Bronze"].color
}

Binding to List element when element number changes

private List<Report> _reports = new List<Report>();
public Report CurrentReport
{
get { return _reports[_componentIterator]; }
set { _reports[_componentIterator] = value; }
}
I have a _reports field and it's a list of Report objects.
I use a CurrentReport property to access current Report object based on _componentIterator.
How do I bind to some of Report properties so that changing _componentIterator won't break my binding?
If I bind like this, every _componentIterator change breaks binding.
Binding designatorTextBlockBinding = new Binding(nameof(CurrentReport.Designator));
designatorTextBlockBinding.Source = CurrentReport;
_artifactControl.DesignatorTextBlock.SetBinding(Controls.TextBlock.TextProperty, designatorTextBlockBinding);
Declare the Binding like shown below, and make sure the CurrentReport property fires a change notification.
var designatorTextBlockBinding = new Binding
{
Path = new PropertyPath("CurrentReport.Designator"),
Source = this
};
or
var designatorTextBlockBinding = new Binding
{
Path = new PropertyPath(
nameof(CurrentReport) + "." + nameof(CurrentReport.Designator)),
Source = this
};
or
var designatorTextBlockBinding = new Binding(
nameof(CurrentReport) + "." + nameof(CurrentReport.Designator))
{
Source = this
};

Google Sheets Script to Hide Row if Checkbox Checked

I am trying to find a working code that will automatically hide a row if the checkbox in column F of that row is checked.
I have tried every script I have found and nothing seems to work. Unfortunately I am not code savvy and I am unable to find the issue.
This is what I currently have:
function onOpen() {
var s = SpreadsheetApp.getActive().getSheetByName("Checklists");
s.showRows(1, s.getMaxRows());
s.getRange('F2:F200')
.getValues()
.forEach( function (r, i) {
if (r[0] == "TRUE")
s.hideRows(i + 1);
});
}
The sheet I am working on is "Checklists" and the column that contains the checkbox is F. The value of the checkbox is either TRUE or FALSE. If the value is TRUE, I want that row to be hidden.
Can someone please help!!!
The quick test I was able to run was to set up a column of checkboxes in column F, then to create a function that catches each edit event on the sheet. This will immediately catch when the user checks a box and will then hide that row.
The trick with using the onEdit event is with determining which cell was actually changed. In your case, you only want to fully follow your logic if the change happens to a checkbox in column F. In my code, I've been using a function to make sure the change is in the desired range. The function looks like this:
function isInRange(checkRange, targetCell) {
//--- check the target cell's row and column against the given
// checkrange area and return True if the target cell is
// inside that range
var targetRow = targetCell.getRow();
if (targetRow < checkRange.getRow() || targetRow > checkRange.getLastRow()) return false;
var targetColumn = targetCell.getColumn();
if (targetColumn < checkRange.getColumn() || targetColumn > checkRange.getLastColumn()) return false;
//--- the target cell is in the range!
return true;
}
So then all your onEdit function has to do is to make a quick call when the edit event is fired to see if the change falls within the range you're looking for. In this case, I set up a variable with my range to check:
var thisSheet = SpreadsheetApp.getActiveSheet();
var checkRange = thisSheet.getRange("F2:F200");
if (isInRange(checkRange, eventObj.range)) {
After that, it's just a matter of picking the row number and hiding or showing. Here's the full example solution:
function isInRange(checkRange, targetCell) {
//--- check the target cell's row and column against the given
// checkrange area and return True if the target cell is
// inside that range
var targetRow = targetCell.getRow();
if (targetRow < checkRange.getRow() || targetRow > checkRange.getLastRow()) return false;
var targetColumn = targetCell.getColumn();
if (targetColumn < checkRange.getColumn() || targetColumn > checkRange.getLastColumn()) return false;
//--- the target cell is in the range!
return true;
}
function onEdit(eventObj) {
//--- you could set up a dynamic named range for this area to make it easier
var thisSheet = SpreadsheetApp.getActiveSheet();
var checkRange = thisSheet.getRange("F2:F200");
if (isInRange(checkRange, eventObj.range)) {
//--- so one of the checkboxes has changed its value, so hide or show
// that row
var checkbox = eventObj.range;
var rowIndex = checkbox.getRow();
Logger.log('detected change in checkbox at ' + checkbox.getA1Notation() + ', value is now ' + checkbox.getValue());
if (checkbox.getValue() == true) {
Logger.log('hiding the row');
thisSheet.hideRows(rowIndex, 1);
} else {
Logger.log('showing the row');
thisSheet.showRows(rowIndex, 1);
}
}
}

How can I delete a hyperlink in a Silverlight RichTextBox?

I have a RichTextBox that the user can edit to create a hyperlink (in my case to another page with the document rather than an external URL). Having successfully created the link I now need to be able to remove it.
I have code that identifies that I've got a hyperlink in the current selection:
TextSelection linkText = richTextBox.Selection;
if (linkText != null && !string.IsNullOrWhiteSpace(linkText.Text))
{
XElement root = XElement.Parse(linkText.Xaml);
XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XElement linkElement = root.Element(ns + "Paragraph").Element(ns + "Hyperlink");
if (linkElement != null)
{
// Get here if have a Hyperlink. How do I remove or update?
}
}
However, I'm now stuck on the bit that goes inside the if test. How do I find the hyperlink so I can remove it completely?
My code for setting up the hyperlink is:
TextSelection linkText = richTextBox.Selection;
var hyperlink = new Hyperlink();
hyperlink.Inlines.Add(linkText.Text);
if (!String.IsNullOrEmpty(selectedTopic)) // A string holding the link target
{
// Setup hyperlink here
}
linkText.Insert(hyperlink);
I've managed to work out how to update the hyperlink:
foreach (var block in richTextBox.Blocks)
{
Paragraph p = block as Paragraph;
foreach (var inline in p.Inlines)
{
var hyperlink = inline as Hyperlink;
if (hyperlink != null && hyperlink.NavigateUri.AbsoluteUri.Contains(currentLink))
{
hyperlink.NavigateUri = new Uri(newLink);
}
}
}
I could use the same approach to delete the hyperlink, but how do I convert the Hyperlink to a normal Inline?
Looks like you are getting close you just need to hold a reference to the link then use Remove. Something like the following (I like to use Linq to make things a little more succinct):-
foreach (var p in richTextBox.Blocks.OfType<Paragraph>())
{
var hyperlink = p.Inlines.OfType<HyperLink>()
.FirstOrDefault(hl => hl.NavigateUri.AbsoluteUri.Contains(currentLink));
if (hyperlink != null)
{
p.Inlines.Remove(hyperlink);
break;
}
}
Edit: Want to leave the content of hyperlink in place? (i.e., just remove the wrapping hyperlink),
foreach (var p in richTextBox.Blocks.OfType<Paragraph>())
{
var hyperlink = p.Inlines.OfType<HyperLink>()
.FirstOrDefault(hl => hl.NavigateUri.AbsoluteUri.Contains(currentLink));
if (hyperlink != null)
{
int index = p.Inlines.IndexOf(hyperlink);
Span span = new Span();
foreach (var inline in hyperlink.Inlines.ToArray())
{
hyperlink.Inlines.Remove(inline);
span.Inlines.Add(inline);
}
// You may need code here to preserve the Font properties etc from hyperlink to span.
p.Inlines[index] = span;
break;
}
}

Formatting text in RichtextBox WPF

I try formatting text in richTextBox, something like in skype chat.
1.column-"Nick" 2.column-"Text of Messange" 3.column-"DateTime"
I want alling 1. column max left and 3. column max right.
What is the best way how can I do it? I am using WPF.
My solution:
Simple solution is create Table object and add to blocks of richtextbox, somothing like this:
var tab = new Table();
var gridLenghtConvertor = new GridLengthConverter();
tab.Columns.Add(new TableColumn() { Name = "colNick", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });
tab.Columns.Add(new TableColumn { Name = "colMsg", Width = (GridLength)gridLenghtConvertor.ConvertFromString("5*") });
tab.Columns.Add(new TableColumn() { Name = "colDt", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });
tab.RowGroups.Add(new TableRowGroup());
tab.RowGroups[0].Rows.Add(new TableRow());
var tabRow = tab.RowGroups[0].Rows[0];
tabRow.Cells.Add(new TableCell(new Paragraph(new Run(rpMsg.Nick))) { TextAlignment = TextAlignment.Left });
tabRow.Cells.Add(new TableCell(ConvertToRpWithEmoticons(rpMsg.RpText)));
tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Cas"))) { TextAlignment = TextAlignment.Right });
RtbConversation.Document.Blocks.Add(tab);

Resources