ErrorProvider does not display error message - c#

In the following code I used errorProvider.SetError(control, message) to display the message, but only the Icon is shown, the message is not shown, what is wrong?
Is there a way to adjust the left margin of the error message only? (I know you can SetIconPadding, but I only want left margin to be changed)
public static DialogResult ShowDialog()
{
var inputBox = new Form { ClientSize = new Size(520, 225), FormBorderStyle = FormBorderStyle.FixedDialog };
var panel = new TableLayoutPanel { Size = new Size(460, 100), Location = new System.Drawing.Point(45, 15) };
var errorProvider = new ErrorProvider { Icon = SystemIcons.Exclamation, BlinkStyle = ErrorBlinkStyle.NeverBlink };
errorProvider.SetIconAlignment(panel, ErrorIconAlignment.BottomLeft);
var okButton = new Button
{
Size = new System.Drawing.Size(70, 30),
Location = new Point(330, 180),
Text = "OK"
};
okButton.Click += new EventHandler((sender, e) => { errorProvider.SetError(panel, "Test Error"); });
inputBox.Controls.Add(panel);
inputBox.Controls.Add(okButton);
return inputBox.ShowDialog();
}

Let me explain about ErrorProvider.
ErrorProvider in Windows Application has following behaviour.
It will display error icon as per configuration.
It will display error message that you have set once you put your mouse cursor on it.
The behaviour you want is too display error message along with icon.
There is one solution build your own control just like ErrorProvider.

Related

Why does the stackpanel ignore Orientation property?

I'd like to use a stackpanel with two textblock that have two uris, inside a ContentDialog. The problem is that despite I set the property to Vertical, it has no effect and that's the visual result
private async void AboutButton_Click(object sender, RoutedEventArgs e)
{
TextBlock gHRepoTB = new TextBlock();
Hyperlink hyperlink1 = new Hyperlink();
Run run1 = new Run();
run1.Text = "View GitHub repository";
hyperlink1.NavigateUri = new Uri("https://www.google.com/");
hyperlink1.Inlines.Add(run1);
gHRepoTB.Inlines.Add(hyperlink1);
TextBlock privacyPolicyTB = new TextBlock();
Hyperlink hyperlink2 = new Hyperlink();
Run run2 = new Run();
run2.Text = "Privacy Policy";
hyperlink2.NavigateUri = new Uri("https://www.bing.com/");
hyperlink2.Inlines.Add(run2);
gHRepoTB.Inlines.Add(hyperlink2);
StackPanel aboutPanel = new StackPanel();
aboutPanel.Orientation = Orientation.Vertical;
aboutPanel.Children.Add(gHRepoTB);
aboutPanel.Children.Add(privacyPolicyTB);
ContentDialog aboutDialog = new ContentDialog();
aboutDialog.Title = "About";
aboutDialog.Content = aboutPanel;
aboutDialog.PrimaryButtonText = "Report a bug";
aboutDialog.PrimaryButtonClick += ReportBug_Click;
aboutDialog.PrimaryButtonStyle = App.Current.Resources["AccentButtonStyle"] as Style;
aboutDialog.CloseButtonText = "Close";
await aboutDialog.ShowAsync();
}
#mm8 showed how to fix your problem, however this is the result of unnecessary and unclear code.
Here are the things I changed:
I've changed a button with hyperlink layout to a hyperlinkbutton.
I've set the contents to just a string since in this case no more is needed.
Instead of manually setting a style to the primarybutton, I've specified the DefaultButton
By creating the dialog this way, you are less prone to making mistakes, others and yourself later on will be able to tell faster what's going on, and less temporary variables are introduced.
The code (You should be able to copy this code directly over your current eventhandler):
private async void AboutButton_Click(object sender, RoutedEventArgs e)
{
StackPanel aboutPanel = new StackPanel();
aboutPanel.Children.Add(
new HyperlinkButton
{
Content = "View GitHub repository",
NavigateUri = new Uri("https://www.google.com/")
});
aboutPanel.Children.Add(
new HyperlinkButton
{
Content = "Privacy Policy",
NavigateUri = new Uri("https://www.bing.com/")
});
var dlg = new ContentDialog
{
Title = "About",
Content = aboutPanel,
PrimaryButtonText = "Report a bug",
DefaultButton = ContentDialogButton.Primary,
CloseButtonText = "Close"
};
dlg.PrimaryButtonClick += ReportBug_Click;
await dlg.ShowAsync();
}
See the result below:
You add the second Hyperlink to the wrong TextBlock. It should be privacyPolicyTB.Inlines.Add(hyperlink2);:
TextBlock privacyPolicyTB = new TextBlock();
Hyperlink hyperlink2 = new Hyperlink();
Run run2 = new Run();
run2.Text = "Privacy Policy";
hyperlink2.NavigateUri = new Uri("https://www.bing.com/");
hyperlink2.Inlines.Add(run2);
privacyPolicyTB.Inlines.Add(hyperlink2); //<-- here

Winform: Clean way to toggle visibility of a control on TextChanged in TextBox

I have a winform with many TextBox and a single message, I want the message to disappear when any of the TextBox are changed. Is there a clean way to achieve this without adding TextChanged EventHander to all TextBox ?
My messy way of doing it is as follows:
public static DialogResult ShowDialog()
{
var inputBox = new Form { ClientSize = new Size(520, 225), FormBorderStyle = FormBorderStyle.FixedDialog };
var input1 = new TextBox { Location = new Point(25, 25)};
var input2 = new TextBox { Location = new Point(25, 60) };
// Many more text boxes...
var label = new Label { Text = "Text", Location = new Point(25, 90), Visible = true };
input1.TextChanged += new EventHandler((sender, e) => label.Visible = false);
input2.TextChanged += new EventHandler((sender, e) => label.Visible = false);
// Add handler for all TextBoxes...
inputBox.Controls.Add(input1);
inputBox.Controls.Add(input2);
inputBox.Controls.Add(label);
return inputBox.ShowDialog();
}
You can try to write a function to create TextBox.
let TextBox initial setting and event binding code in the function.
private static TextBox CreateTextBox(int xPos,int yPos,Label label){
var input1 = new TextBox { Location = new Point(xPos, yPos)};
input1.TextChanged += new EventHandler((sender, e) => label.Visible = false);
return input1;
}
You just need to call the function in inputBox.Controls.Add method,and pass parameters which you need.
public static DialogResult ShowDialog()
{
var inputBox = new Form { ClientSize = new Size(520, 225), FormBorderStyle = FormBorderStyle.FixedDialog };
var label = new Label { Text = "Text", Location = new Point(25, 90), Visible = true };
inputBox.Controls.Add(CreateTextBox(25, 25,label));
inputBox.Controls.Add(CreateTextBox(25, 60,label));
inputBox.Controls.Add(label);
return inputBox.ShowDialog();
}
NOTE
if there are too many parameters you can try to use a class to carry those and pass.

Simple document using FastReport.Net does not show text

I tried to make a simple document using FastReport. So I started with placing a button on the form and writing the following code in order to run it when the button is clicked:
private void button1_Click(object sender, EventArgs e)
{
Report report = new Report();
ReportPage page1 = new ReportPage();
page1.Name = "Page1";
report.Pages.Add(page1);
page1.ReportTitle = new ReportTitleBand();
page1.ReportTitle.Name = "ReportTitle1";
TextObject text1 = new TextObject();
text1.Name = "Text1";
text1.Text = "REPORT TITLE TEXT";
text1.HorzAlign = HorzAlign.Center;
text1.Font = new Font("Tahoma", 14, FontStyle.Bold);
page1.ReportTitle.Objects.Add(text1);
report.Show();
}
Unfortunately, when I ran the application and I pressed the button, a blank page was shown, without any text. What is wrong with the code? Does it lack some element?
You should set width and height for text object:
text1.AutoWidth = true;
text1.Height = 100;

Creating balloon tooltip in C#

Can i know how can i make a popup bubble message in my application coded in C#.
Like example, when i start my application, it'll popup saying "Welcome to UbuntuSE App".
And yea, The popup is not the message box popup, it's the popup in the traymenu.
Something similar to this:
PS,
If i'm not wrong, this is called Balloon Tooltips. But how can i use this in my codes.
If you're using Winforms you have the NotifyIcon class. This object has a ShowBalloonTip method which will show a balloon tip:
var icon = new NotifyIcon();
icon.ShowBalloonTip(1000, "Balloon title", "Balloon text", ToolTipIcon.None)
you must be looking for the Notify Icon Control
another CodeProject Example
here is a full example in MSDN
You can use the NotifyIcon control that's part of .NET 2.0 System.Windows.Forms.
Check : Using the NotifyIcon control
From the msdn,
NotifyIcon : Specifies a component that creates an
icon in the notification area. This
class cannot be inherited.
NotifyIcon.BalloonTipIcon
You have to set the property "icon" or it won't pop up
NotifyIcon ballon = new NotifyIcon();
ballon.Icon = SystemIcons.Application;//or any icon you like
ballon.ShowBalloonTip(1000, "Balloon title", "Balloon text", ToolTipIcon.None)
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace ShowToolTip
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btBallonToolTip_Click(object sender, EventArgs e)
{
ShowBalloonTip();
this.Hide();
}
private void ShowBalloonTip()
{
Container bpcomponents = new Container();
ContextMenu contextMenu1 = new ContextMenu();
MenuItem runMenu = new MenuItem();
runMenu.Index = 1;
runMenu.Text = "Run...";
runMenu.Click += new EventHandler(runMenu_Click);
MenuItem breakMenu = new MenuItem();
breakMenu.Index = 2;
breakMenu.Text = "-------------";
MenuItem exitMenu = new MenuItem();
exitMenu.Index = 3;
exitMenu.Text = "E&xit";
exitMenu.Click += new EventHandler(exitMenu_Click);
// Initialize contextMenu1
contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] { runMenu, breakMenu, exitMenu });
// Initialize menuItem1
this.ClientSize = new System.Drawing.Size(0, 0);
this.Text = "Ballon Tootip Example";
// Create the NotifyIcon.
NotifyIcon notifyIcon = new NotifyIcon(bpcomponents);
// The Icon property sets the icon that will appear
// in the systray for this application.
string iconPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\setup-icon.ico";
notifyIcon.Icon = new Icon(iconPath);
// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon.ContextMenu = contextMenu1;
notifyIcon.Visible = true;
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon.Text = "Morgan Tech Space BallonTip Running...";
notifyIcon.BalloonTipText = "Morgan Tech Space BallonTip Running...";
notifyIcon.BalloonTipTitle = "Morgan Tech Space";
notifyIcon.ShowBalloonTip(1000);
}
void exitMenu_Click(object sender, EventArgs e)
{
this.Close();
}
void runMenu_Click(object sender, EventArgs e)
{
MessageBox.Show("BallonTip is Running....");
}
}
}
Thanks for the info!
Made something like this and worked!
private void NotifyBaloon(string text, string tooltip, string title, bool show)
{
notifyIconMain.Text = text;
notifyIconMain.BalloonTipText = tooltip;
notifyIconMain.BalloonTipTitle = title;
if (show)
notifyIconMain.ShowBalloonTip(1000);
}

Programmatically closing a WP7 Coding4Fun toolkit MessagePrompt Control

I am using the MessagePrompt control from the Coding4Fun toolkit to show a "Rate and Review" dialog in my app.
I have replaced the default buttons with custom buttons as well as clearing the existing buttons (to get rid of the ticks and crosses) but cannot figure out which method to call to close the prompt.
This is the code I am using, Id like to close the prompt in the click method of the cancel button
var messagePrompt = new MessagePrompt
{
Title = "Rate and Review",
IsAppBarVisible = false,
Body = new TextBlock
{
Text = "PLS REVIEW MY APP K THNX",
TextWrapping = TextWrapping.Wrap
}
};
var rateButton = new Button() { Content = "Rate and Review" };
rateButton.Click += (sender, e) =>
{
var m = new MarketplaceDetailTask
{
ContentIdentifier = PhoneState.AppID,
ContentType = MarketplaceContentType.Applications
};
m.Show();
};
var cancelButton = new Button() { Content = "Dismiss" };
cancelButton.Click += (sender, e) =>
{
//todo close messagePrompt here
};
messagePrompt.ActionPopUpButtons.Clear();
messagePrompt.ActionPopUpButtons.Add(rateButton);
messagePrompt.ActionPopUpButtons.Add(cancelButton);
messagePrompt.Show();
The newest check in of the toolkit exposes the Hide() method to solve this.
cancelButton.Click += (sender, e) =>
{
messagePrompt.Hide();
};

Categories

Resources