Programmatically changing button label in Monotouch - c#

I made a .xib with a button, and referenced it with an outlet to "btnActies"
when i do the following (at any given time) in the back-end C# code
btnActies.TitleLabel.Text = "This is a
new label!";
When i build and run the app, the label on the button changes to "This is a new label",
but then, if I touch the button, the label reverts to the 'default' text i set in the .xib file.
How do i change the label on a Monotouch UIButton and keep this from happening?

When you want to set some text on a UIButton, you do not do it by altering the text of its TextLabel property. You do it by calling its SetTitle method, passing as the second argument, the button state for which the title will be set at runtime. Chetan Bhalara's answer is correct, here is the C#/MonoTouch equivalent:
btnActies.SetTitle ("title", UIControlState.Normal);
They way you are doing it right now doesn't work, because the label's text is changing internally whenever needed, to the title set in Interface Builder (if you have set it), in this case when you tap the button.

//Improved Formatting
Hello
You can use this code for change the button label for iPhone.
[btnActies.setTitle:#"Title value1" forState:UIButtonStateNormal];
[btnActies.setTitle:#"Title value2" forState:UIButtonStateSelected];
[btnActies.setTitle:#"Title value3" forState:UIButtonStateHighlighted];

Related

UWP NavigationView not lining up with titlebar back button

I'm trying to line up my NavigationView with the back button that comes built into the window like so:
[]
but when i try to line up my NavigationView it ends up looking like
[]
How should i go about changing the width of the NavigationView so that it lines up with the back button?
The reason why the back button isn't aligned for you is that you're using the embedded back button in the title bar, which is deprecated now (as in, the MS guidelines suggest not to use it anymore, and to replace it with a dedicated back button in the app UI).
What you want to do is to set the IsBackButtonVisible property of the NavigationView to Visible, and use that back button instead to handle the navigation in your frame, or in other parts of your app.
To handle the back navigation request, you can just register to the BackRequested event in the NavigationView control.
Also, to hide the back button in the title bar, you just need to remove these couple lines in your code:
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;

Hide button text behind button image

Is there any method to hide a button text behind button image like bring to front or send to back option?
I only need to hide or show button image only as I have a code that coverts the original text CloseButton.text = "&Close"; to CloseButton.Text = "&Cancel";
to perform another command so I can't use CloseButton.Text = "";.
Tried this link - WinForms button with image and text but my button size is too small that it would only show the text and not the image no matter how I mix and match TextAlign and ImageAlign.
Any help is much appreciated. Thanks in advance.
Sample Button Size below:
Check this
Place Textbox in Button and set textbox.visible=false method
Is there any method to hide a button text behind button image like
bring to front or send to back option?
There is no such built in but you can simply clear out the text on click event of the controls. Example: if you have radio buttons for send to back then on click of that clear out the control text saying controlId.Text = string.Empty
As #Rotem posted in the comments.
Have your code behind use the Tag property rather than Text. Easiest way out is using properties for what they were made for.
Instead of using CloseButton.text = "&Close"; I changed it to CloseButton.Tag = "&Close"; and made my code worked around it to have the same function without placing an actual Text in my Buttons. Credit this asnwer to #Rotem. Thanks.

How to change the properties of a textbox in C#?

I am a beginning C# noob and I am making my own wordpad/document creator, and I want to change the property of a textbox via the code. What I am trying to do is have three buttons: Left, Center, and Right, and I am trying to make them so that they change the "TextAlign" property, when they are clicked on. Does anyone have some suggestions? I hove done some research and turned up with nothing.
textBox1.TextAlign = HorizontalAlignment.Center;
textBox1.TextAlign = HorizontalAlignment.Right;
textBox1.TextAlign = HorizontalAlignment.Left;
Give a name to your TextBox such as tbContent and then add three buttons, again name them appropriately such as btnAlignRight, btnAlignCenter and btnAlignLeft.
Now, go to your .cs file and then Add event handlers for your three buttons through the designer. Shortcut: Just double click on the buttons one by one when in the designer, it will automatically generate and register the Click EventHandler for that button.
And then add the code provided by badkip in the appropriate EventHandler

How to check if WPF control is hidden/collapsed using MS coded UI tests (CUIT)

I want to write a coded UI test like "Some WPF control when some condition should not be visible". How do I assert "is not visible"?
To reproduce the issue:
create new WPF app
add nothing but one big named button into the main window
go to CUIT editor and recognize the button
without closing the CUIT editor close the WPF app
add Visibility="Hidden" to the button
restart the app
select the button in the CUIT editor and press "refresh" button
NOTE: the properties of the hidden button are exactly the same as properties of visible button!
There's no way to assert that the button is hidden!
Additionally:
I would be glad to hear about workarounds you're using. After all what I need is to write the test, not figure out CUITs
I am aware that I can compare screenshots
Interestingly if you try to do stuff with the hidden button the CUIT will throw. It implies that the CUIT knows when a button is hidden.
Interestingly if Visibility="Collapsed" instead of "Hidden" CUIT will recognize it by reporting Width = Height = -1. That doesn't help with collapsed buttons though :(
I've found the best way to work around the IsVisible limitation is to use the TryGetClickablePoint(out System.Drawing.Point) method of the UITestControl object. This method will return a Boolean value. So, for example, if you have a WpfButton:
WpfButton mine = new WpfButton(parent);
mine.SearchProperties["id"] = "id";
Point toString;
bool result = mine.TryGetClickablePoint(out toString);
Assert.IsTrue(result, "My Assertion here.");
That has worked more often than not. To handle collapsed or expanded, though, is there some property of the object that changes based on its state? For example, if the class is class="myobject expanded", you could easily assert based onmine.GetProperty("Class").ToString().Contains("expanded"); as a Boolean value.
Try to use GetProperty method:
WpfButton myButton = new WpfButton();
if(myButton.GetProperty("Enabled").Equals(true))
{
... CODE
}

How to change the background image of a button when clicked in code?

There is a similar question like mine here in Stackoverflow but it only explains how to change it in XAML. I want to know how can I change it in code.
Here is a image that shows how I do it in XAML using Blend:
Link for full size: https://snag.gy/4Skk4.jpg
Basically I want to change the background of a button's pressed state in C# but I can't seem to find any examples on the Internet. It must be in code because sometimes the image of the button will change therefore the button's pressed image must change as well.
The following code is just to change the image of the button and it's just the start.
image.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(#"images/Button-warning-icon.png", UriKind.Relative));
image.Stretch = Stretch.Uniform;
buttonWarnings.Background = image;
If I understand you correctly, you are trying to change the appearance of the Button control in a "pressed" visual state.
I'm not near my dev computer to try it out, but to "unblock you" I'll give a direction.
First, as you noticed in your Blend screenshot, each visual state is represented with a Storyboard, which defines how various properties change. In your case, you're looking to change Background property.
The VisualStateGroups and their states are defined by the control. You can override them when you re-template the control. So, retemplate the button control using Blend with "Edit Template"->"Edit Copy".
Then, in code, you should be able to do the following:
1) Get visual states (this would not work unless you re-template the control, AFAIK)
var visualStateGroups = VisualStateManager.GetVisualStateGroups(buttonWarnings);
2) Get the VisualStateGroup of "CommonStates" from the visualStateGroups
collection
var commonStatesGroup = visualStateGroups.Find((g) => ((VisualStateGroup)g).Name == "CommonStates") as VisualStateGroup;
3) Get the "Pressed" VisualState:
var pressedVisualState = commonStatesGroup.Find((vs) => ((VisualState)vs).Name == "Pressed") as VisualState;
4) Change the storyboard of that state
pressedVisualState.Storyboard = newStoryboardWithCustomImageBackgroundProperty;
(Disclaimer: I'm not near in a computer to try it now - it's all in theory)
There are many examples to be found on the internet!
Take a look at some:
http://mobile.dzone.com/articles/windows-phone-buttonimage
http://loekvandenouweland.com/index.php/2011/01/windows-phone-image-button/
Actually its quite simple,
While in button pressed state....see part 3 in the image you uploaded above.
Above all the colors there is a row containing 5 icons.
Click on 4th icon.
it will show you option to choose image as background.

Categories

Resources