C# Button Text Condition - c#

Using a WinApp form in c#, and many buttons here...
I want to create a condition, that if a button has text in it, then the background color of that button changes. That sounds easy enough to do. But what I have is a common set of buttons, that have text in them dependant of values in a XML document.
Example: Week 1 - Buttons 1, 3 and 5 have text in them. Week 2 - Buttons 2 and 3 have text.
How can I setup a seperate condition to check if the button has text in it or not, and then change the color if there is a text value in the button.
Thank you.

I would extend button and override the label setter such that it also changes the color when setting the contents of the label to some non-empty value.

Do you want something like this?
foreach (var btn in this.Controls.OfType<Button>()) {
btn.BackColor = (string.IsNullOrEmpty(btn.Text))
? SystemColors.ButtonFace : Color.AliceBlue;
}
I would put it in a method, and call it on form load, or whenever the buttons' texts changes.

Related

How to change the color of one cell in ObjectListView?

THIS IS NOT A PLAIN LISTVIEW!
I want to create a Windows Forms Application in C#. But i have some problems with the ObjectListView.
I want to change the color of one specific cell in the objectlistview but I can only change the color of one row.
this is my objectlistview:
screenshot
there you can see the column "Queue" and there is a value "Allgemein" and I want to color all these values with "Allgemein" in these column in red but not the whole row.
I changed with this code the color of the row:
objectListView1.Items[0].BackColor = Color.Red;
I think that what you are trying to do is well explained in this ObjectListView cookbook.
Just In case someone needs this, if you want to change the color of the whole column (not header), use decoration.
There is a class TintColumnDecoration, used by default in OLV to tint sorted column, but you can customize it:
Dim MyTint= New TintedColumnDecoration(YourColumn)
MyTint.Tint = Color.FromArgb(50, Color.LightGreen)
OLV.AddDecoration(MyTint)
From http://objectlistview.sourceforge.net/
CellEditStarting event
The CellEditStarting event is triggered after the user has requested to edit a cell but before the cell editor is placed on the screen.
This event passes a CellEditEventArgs object to the event handlers. In the handler for this event, if you set e.Cancel to True , the cell editing operation will not begin. If you don’t cancel the edit operation, you will almost certainly want to play with the Control property of CellEditEventArgs.
You can use this to customise the default editor, or to replace it entirely.
For example, if your ObjectListView is showing a Color in a cell, there is no default editor to handle a Color. You could make your own ColorCellEditor, set it up correctly, and then set the Control property to be your color cell editor. The ObjectListView would then use that control rather than the default one. If you do this, you must fully configure your control, since the ObjectListView will not do any further configuration of the editor. So, to listen for the event, you would do something like this:
this.myObjectListView.CellEditStarting += new CellEditEventHandler(this.HandleCellEditStarting);
And your handler method might look something like this:
private void HandleCellEditStarting(object sender, CellEditEventArgs e) {
if (e.Value is Color) {
ColorCellEditor cce = new ColorCellEditor();
cce.Bounds = e.CellBounds;
cce.Value = e.Value;
e.Control = cce;
}
}
With this code in place, your spiffy ColorCellEditor will be shown whenever the user tries to edit a color in your ObjectListView.

Text in textbox is selected - how to change?

I created a simple form in C# with only a textbox and a button.
The form contains a function to set the text in the textbox and another function that closes the form if the button is clicked.
Now I execute the form, set the text and display the form.
Everything is fine, but: The text in the textbox is "selected / marked".
What can I do that the text is not selected and the focus is on the button?
(button.focus is not working)
Thanks for help
You can manually set the selection after changing the content, for example like this:
this.textBox1.SelectionStart = this.textBox1.Text.Length;
Where textBox1 is the textbox you're working with. This clears the previous selection and makes a new one, effectively setting the cursor inside the textbox to the last element of it's content.
Alternatively, select the button instead of focussing it, like this:
this.button1.Select();
Where button1 is the button you want to select after changing the textbox' content.
Solution:
I changed the TabIndex from textbox from 0 to 1 and
have tabindex 0 to the button.
Then text is no longer marked.

select listViewItems in listView as text

In ListView there are 3 option of SelectionMode
1.Single - only one item can be selected.
2.Multiple - you can select muliple items, one after the other.
3.Extended - You can select multiple items and used Ctrl or Shift key.
I'm need to select some items in ListView as text in TextBox.
i.e. press with the left button of mouse, until the mouse is up.
and mark all between items as Selected.
How can I make it?
Thanks
1.Single: SelectionMode="Single"?
2 Mutiple : i think use binding
enter link description here
3
enter link description here
Or You try this line SelectionMode="Muti..."
Firstly, the porpose of this problem is to display listView of TextBlocks for allow use ItemsSource of ListView for display text for several reasone.
behind each textBlock that contains a word, there are in the ViewModel class named Word. that contains the text of the word and property of IsSelected.
I solve this problem, by adding 3 EventSetter event to the ListViewItem,
1.PreviewMouseLeftDown
2.MouseEnter
3.PreviewMouseLeftUp
and adding a flag of IsInSelection, and two object Word that present the control in the view,1.firstSelectionWord, 2.lastSelectionWord.
and, when the first event raise, i update the current control to be Selected.
and set a flag IsInSelection to true. and set firstSelectionWord = lastSelectionWord = current word pressed.
in the MouseEnter event i checked if IsInSelection is true, and them mark also the current control to Selected=true. set the lastSelectionWord = current word pressed.
and call a method that mark all the Word between them as selected.
in the PreviewMouseLeftUp function, i set the IsInSelection = false.

How to show tool tip when cursor is on ListView cell?

I have listview object, and several columns in it. One of the columns does not fit all text info, and the text has been cutted. I need to implement something like this: when user move mouse cursor to this column's cell, all the text is shown. Is it possible to do that ?
Now i have:
protected ListViewItem GetItem(ListView listView, Point mousePosition)
{
Point localPoint = listView.PointToClient(mousePosition);
return listView.GetItemAt(localPoint.X, localPoint.Y);
}
private void myListView_MouseMove(object sender, MouseEventArgs e)
{
ListViewItem item = GetItem(myListView, Cursor.Position);
// or should I use e.Location instead of Cursor.Position?
item.ToolTipText = "my info"//Now I need to show "my info" on the cell
//that user move cursor on
}
One of the old ways to show decent tooltip with lists is to hide a label and make it visible at the desired location, with the desired text for the desired duration. Benefit of this could be multi-line, multi-color tip.
So it works like this:
the label sits on the listview or form and it is invisible.
on mouse move you get item data using mouse X and Y. Position,
size and show your label filled with data.
label itself can have a timer, which will start when label
is visible and make it invisible in 5 seconds, for example.
I remember doing just that this year. Although my "tooltip" was done over the listbox.
Replace label with a textbox and suddenly you can make your data in the cell editable :o)
Try this code
ListViewItem iListView = new ListViewItem("add");
iListView.ToolTipText = "Add Expanded";
myListView.Items.Add(iListView);
Do this by setting the ShowItemToolTips property of ListView to true

Read a dynamically created textbox in a Gridview

I am dynamically adding a textbox to certain rows (one column only) of a gridview. I add the controls with this insdie of a test condition (works fine):
TextBox txtASIN = new TextBox();
txtASIN.ID = "TxtASIN" + e.Row.RowIndex;
e.Row.Cells[4].Controls.Add(txtASIN);
int i = e.Row.Cells[4].Controls.Count; //TEST: This returns 1 correctly
I want the user to be able to enter values into one or more of these textboxes and then update the database with a single button click (not one click for each row). The problem I'm having is how to access those values on a button click event. I did a simple test this way to try to see the value in the second row but get null in temp1 (I am certain there is a value entered in that textbox):
protected void btnUpdate1_Click(object sender, EventArgs e)
{
TextBox temp = (TextBox)GridView2.Rows[1].FindControl("txt1");
string temp1 = temp.Text;
int i = GridView2.Row.Cells[4].Controls.Count; //TEST: This returns 0 incorrectly }
Once I can make this work, I can iterate through the rows and do what I need to do with the values. I don't know if the text entered in the textbox is actually readable without a postback but I'm otherwise stumped. Open to better suggestions on how to do this.
Thanks.
EDIT: Here is where I am now. I can see the textboxes in my column fine. I put a break in on a button that attempts to read them and this is what I'm seeing. If I check GridView2.Rows[0].Controls.Count, I get 8, which is the correect number of columns. If I check GridVeiw2.Rows[0].Cells[4].Controls.Count, I get 0, which is wrong because the textbox is there. I can get a Count of 1 right after I dynamically create it but not when I perform a subsequent button click.
Can anyone explain this? I feel if I can get past this holdup, I can get the rest done.
Thanks again.
You need to assign an ID to the TextBox controls and then access them by that ID in FindControl(). Also, make sure you're adding the controls in the Page's Init() method of the life-cycle. That way it gets added to ViewState.
TextBox txt1 = new TextBox();
txt1.ID = "txt1";
e.Row.Cells[4].Controls.Add(txt1);
EDIT: I just remembered another possible solution. Instead of programatically creating the TextBox controls in the code-behind just create a TemplateField in the GridView.
Add Textbox TemplateField Column To GridView Programmatically
I would try a different approach, putting the text box in the html markup of the page and then control the visible or readonly property of it on the ItemDataBound event. That way, the control will always be there and you don't have to worry about the lifecycle stuff.

Categories