I have a button that activate a service but before that service is activate i need to force the user to enter a text, for that task i create a user control that allow the user to enter text and validate it, on that control i have 2 buttons OK and cancel, OK button is working fine but the cancel is supposed to hide the popup, but the event don't get triggered, i need to hit the button several times before that happen, i am also getting a similar problem if the person hits back i cant get rid of the popup without closing the app. What is the best approach to work with popup and in this case why is the event is not getting trigger on the first hit
public partial class TicketView
{
Controls.TextInputBox textInputControl;
private void InitCloseTxtInput()
{
textInputControl = new Controls.TextInputBox(8, 0);
textInputControl.CancelBtn.Click += new RoutedEventHandler(CancelBtn_Click);
textInputControlForCamara.CancelBtn.Click += new RoutedEventHandler(CancelBtn_Click);
}
private void AppBarBtn_Click(object sender, EventArgs e)
{
InitCloseTxtInput();
PivotContainer.IsEnabled = false;
this.popup = new Popup();
this.popup.Margin = new Thickness(0, 150, 0, 0);
this.popup.Child = textInputControl;
this.popup.IsOpen = true;
}
void OkBtnOnPopUP_Click(object sender, RoutedEventArgs e)
{
//Call Service
}
void CancelBtn_Click(object sender, RoutedEventArgs e)
{
this.popup.IsOpen = false;
this.PivotContainer.IsEnabled = true;
}
}
If anyone have any out of question recommendation for this piece of code, Please tell me.
This line was the problem don’t exactly know why but when trying to re do de control i comment this line and all start to work just fine
this.popup.Margin = new Thickness(0, 150, 0, 0);
At least in the code, it does not seem like you are hooking CancelBtnPopUP_Click anywhere.
Is that the entirety of the code?
Related
I have an app where when there is a change to a label it creates a clickable notification. It all works perfectly however the notification icon stays in the system tray and you end up with loads of them. I know I can use the notification.dispose but I don't know where to use it. Here is the code:
private void label1_TextChanged(object sender, EventArgs e)
{
string strNumber = label1.Text;
if (strNumber.StartsWith("0") || strNumber.StartsWith("+44") & strNumber.Length < 17 & strNumber.Length > 8)
{
var notification = new System.Windows.Forms.NotifyIcon()
{
Visible = true,
Icon = System.Drawing.SystemIcons.Question,
BalloonTipTitle = "Click here to dial",
BalloonTipText = Clipboard.GetText(TextDataFormat.Text),
};
notification.ShowBalloonTip(5000);
notification.BalloonTipClicked += new System.EventHandler(notification_BalloonTipClicked);
// notification.Dispose();
}
}
I have commented out the notification.Dispose at the bottom because if I do that it disposes of it straight away and I can't click it. So I don't know where else I am supposed to add this code so that it removes the icon for it in the tray either when you click the notification or if you just wait and let it time out. I can't place it outside of this block of text as it's a local variable.
I'm a complete novice at coding so any help would be appreciated.
You need to subscribe to the events that the NotifyIcon presents.
Create functions for handling timeout and click then
notification.BallotTipClicked += MyClickFunction;
notification.BallotTipClosed += MyCloseFunction;
private void MyClickFunction(Object sender, EventArgs e) {
System.Windows.Forms.NotifyIcon notification = sender as System.Windows.Forms.NotifyIcon;
notification.Dispose()
}
Let's take an example from android. Assume you send a message to someone and after message was send you can see (for a few seconds) kind of notification on the screen like Your message was send.
That is exactly what I would like to find in Winform. In my Winform app user click on the button and I would like to make kind of UI response, show him a message for a few sec, like Button clicked.
How to do it?
P.S. Actually I tried to find out how to do it, but everything that I found is kind of notification on the screen at the right bottom corner. It is not actually what I am looking for. I need something like you can see on screenshot. This text should appear in the form, not on the corner of the screen.
P.S 2 Tooltip also not what I am looking for. Tooltip is something that binded close to the button(view). I need kind of general UI response. User click on buttons and instead to show him a dialog that force user to move his mouse and close the dialog, I need kind of softy message that disappear after a few sec.
I need kind of softy message that disappear after a few sec.
I think the tooltips are what you are looking for. The idea is you can programmatically control where to show a tooltip and when to hide it.
Please start a new WinForms project. Add three buttons, ToolTip and Timer to the form. Write the next event handlers (and bind them to the corresponding components):
private void button_Click(object sender, EventArgs e)
{
toolTip1.Show(((Button)sender).Text + " is pressed", this, 300, 300);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
toolTip1.Hide(this);
}
After demo starting you'll see a tooltip with certain text appearing at the same position for the 1 second.
#Miamy's solution not a bad one, however you have to center text in notification box, and you don't need to use timer. Here the custom tooltip class which centers the tooltip text location and you can see the output below:
Moreover I added some coloring features along with default timer implementation.
class CustomToolTip : ToolTip
{
public int SIZE_X = 500;
public int SIZE_Y = 50;
public CustomToolTip()
{
this.OwnerDraw = true;
this.Popup += new PopupEventHandler(this.OnPopup);
this.Draw += new DrawToolTipEventHandler(this.OnDraw);
}
string m_EndSpecialText;
Color m_EndSpecialTextColor = Color.Black;
public Color EndSpecialTextColor
{
get { return m_EndSpecialTextColor; }
set { m_EndSpecialTextColor = value; }
}
public string EndSpecialText
{
get { return m_EndSpecialText; }
set { m_EndSpecialText = value; }
}
private void OnPopup(object sender, PopupEventArgs e) // use this event to set the size of the tool tip
{
e.ToolTipSize = new Size(SIZE_X, SIZE_Y);
}
private void OnDraw(object sender, DrawToolTipEventArgs e) // use this event to customise the tool tip
{
Graphics g = e.Graphics;
LinearGradientBrush b = new LinearGradientBrush(e.Bounds,
Color.AntiqueWhite, Color.LightCyan, 45f);
g.FillRectangle(b, e.Bounds);
g.DrawRectangle(new Pen(Brushes.Black, 1), new Rectangle(e.Bounds.X, e.Bounds.Y,
e.Bounds.Width - 1, e.Bounds.Height - 1));
System.Drawing.Size toolTipTextSize = TextRenderer.MeasureText(e.ToolTipText, e.Font);
g.DrawString(e.ToolTipText, new Font(e.Font, FontStyle.Bold), Brushes.Black,
new PointF((SIZE_X - toolTipTextSize.Width)/2, (SIZE_Y - toolTipTextSize.Height) / 2));
b.Dispose();
}
}
You can call this tooltip as below code:
CustomToolTip notifyError = new CustomToolTip();
notifyError.Show("Please enter a number.", this, 100, 100, 2000);
Above code creates notification box at 100, 100 location for 2 seconds.
For two days, I have tried various methods for making it so that on loadButton click, it opens a secondary window and disables the loadButton; Then, when that secondary window has been closed, the loadButton will be re-enabled. Although, obviously all of my attempts have been unsucessful.
I have been reading about using the isClosing event, although, I haven't figured out how to properly implement it. So I decided to go with this route.
private void loadButton_Click(object sender, RoutedEventArgs e)
{
var richWindow = RichTextWindow.GetWindow(new RichTextWindow());
if (richWindow.IsActive != true)
{
loadButton.IsEnabled = false;
richWindow.Show();
}
else
{
loadButton.IsEnabled = true;
}
}
Issue here is, the first half is executed. Once I click the loadButton, it does disable. However, on closing the new Window, the loadButton is still disabled.
Could anyone point me in the right direction on where I need to go with this?
I think what you want is something like this:
private void loadButton_Click(object sender, RoutedEventArgs e)
{
var richWindow = RichTextWindow.GetWindow(new RichTextWindow());
richWindow.Closed += (s, e) => loadButton.IsEnabled = true;
loadButton.IsEnabled = false;
richWindow.Show();
}
Basically, disable the button before opening the window. Then, listen for the window to close and enable the button again.
richWindow.Loaded +=
{
loadedButton.IsEnabled = false;
};
richWindow.Closing +=
{
loadedButton.IsEnabled = true;
}
I'm trying to make a button on my windows form create a new page when clicked, something like when you are going through an installation of a program and you would click "Next" and it would take you to a new page but not bring up an entirely seperate window. I would also have another button that would be pressed to bring up the original form.
I've looked everywhere for a answer to this so any help on how I would create this would be greatly appreciated.
private void Cleaning_Click(object sender, EventArgs e)
{
// executes new page
}
What you want to do is to create a wizard. There are lot of samples on internet regarding that. Take a look;
http://www.codeproject.com/Articles/31770/Wizard-Form-Implementation
Or
You can see that SO question
Creating Wizards for Windows Forms in C#
One simple way is to use panels in a single form. The only problem with panels is that it is hard to edit the layout and also your code would become very messy.
it IS simple BUT has a very bad downside
Simply we can add one groupbox and add your controls, in form load set groupbox visible to false and button click event visible to true something like....
private void Form3_Load(object sender, EventArgs e)
{
groupBox1.Visible = false;
}
private void button1_Click_1(object sender, EventArgs e)
{
if (button1.Text == "&Show")
{
button1.Text = "&Hide";
groupBox1.Visible = true;
}
else if (button1.Text == "&Hide")
{
button1.Text = "&Show";
groupBox1.Visible = false;
}
}
I have seen some topics about this subject in Objective-C. I read a lot of them, spent 2 days on it on trying to find a solution and none of them worked for me. I am mainly coding in C#. Since my problem behaviour (fire only when leaving/re-enter button) and context (C#) is a bit different. So, I will try my chance by asking my question here.
I will try to keep it simple.
Here is a sample of code:
private UIButton _buttonTest;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
_buttonTest = new UIButton(new RectangleF(10, 70, 50, 50));
_buttonTest.SetTitle("0", UIControlState.Normal);
_buttonTest.TouchUpInside += HandleButtonTestTouchUpInside;
_buttonTest.BackgroundColor = UIColor.Red;
this.View.AddSubview(_buttonTest);
}
void HandleButtonTestTouchUpInside (object sender, EventArgs e)
{
string textNumber = _buttonTest.Title(UIControlState.Normal);
// Increment Number
_buttonTest.SetTitle((int.Parse(textNumber)+1).ToString(), UIControlState.Normal);
}
There is a button and the title text is set to "0".
When the user click on it, it increments the number (title) by 1.
This code usually works very well!
However, it does not work in some of my classes for some unknown reasons...
Here is the problem:
TouchUpInside does not fire. ... However, if I hold the button with a finger and keep holding the finger while leaving the button and then re-entering the button then release the button, then.... it will fire the TouchUpInside, .... so the finger need to leave and re-renter the button while holding the button to make the TouchUpInside fires. Usually, this code works very well.
Things Checked:
if I replace TouchUpInside by TouchDown, then TouchDown works.
'User Interaction Enabled' is set to True for all superviews.
All views frames (among the superviews) seem to be inside their superview frames.
I moved the _buttonTest around and I noticed that the _buttonTest TouchUpInside does not fire correctly in the View, the SuperView, the SuperSuperView.... but it fires correctly in the SuperSuperSuperView.
Any suggestion?
In the SuperSuperView, I had this tap gesture that was entering in conflict with the Button event.
// Tap Gesture
UITapGestureRecognizer tapPageGestureRecognizer = new UITapGestureRecognizer();
tapPageGestureRecognizer.AddTarget(this, new Selector ("HandleTapPageGestureRecognizer:"));
this.View.AddGestureRecognizer(tapPageGestureRecognizer);
The idea is to disable the gesture SuperSuperView gesture when the button event, TouchDown was fired, ... and to re-enable it when the TouchUpInside is fired.
So here is one solution for the problem:
private void SetGrandParentViewGestureEnabled(bool enabled)
{
foreach(UIGestureRecognizer g in this.View.Superview.Superview.GestureRecognizers)
{
g.Enabled = enabled;
}
}
void HandleButtonSubmitTouchDown (object sender, EventArgs e)
{
SetGrandParentViewGestureEnabled(false);
}
void HandleButtonSubmitTouchUpInside (object sender, EventArgs e)
{
// => Do treatments here!
SetGrandParentViewGestureEnabled(true);
}
However, I could have used EventHandler or Action to enable/disable the tap gesture.
EDIT: Here is another function that need to be added as well to re-enable the gesture.
void HandleButtonSubmitTouchUpOutside (object sender, EventArgs e)
{
SetGrandParentViewGestureEnabled(true);
}
I had a similar problem, I ended up using a tap recognizer together with the long press recognizer like this for the button TopRightButton.
var longPressGesture = new UILongPressGestureRecognizer(Action);
TopRightButton.AddGestureRecognizer(longPressGesture);
var tapRecognizer = new UITapGestureRecognizer();
tapRecognizer.AddTarget(() =>
{
//method
});
tapRecognizer.NumberOfTapsRequired = 1;
this.TopRightButton.AddGestureRecognizer(tapRecognizer);
private void Action(){ };