I created some Hyperlinks using the code below:
public Class_List()
{
InitializeComponent();
StackPanel myStackPanel = new StackPanel();
TextBlock txt = new TextBlock();
txt.Foreground = Brushes.Black;
txt.FontFamily = new FontFamily("03SmartFontUI");
txt.FontSize = 25;
txt.Margin = new Thickness(0, 5, 0, 5);
Run run = new Run(className);
Hyperlink link = new Hyperlink(run);
link.Click += Link_Click;
txt.Inlines.Add(link);
}
Now, I want to get the text of the hyperlink and store it on string s:
private void Link_Click(object sender, RoutedEventArgs e)
{
string s = (sender as Hyperlink).Inlines.ToString();
Class_Page class_page = new Class_Page();
NavigationService.Navigate(class_page);
}
However instead of the hyperlink text, I got
System.Windows.Documents.InlineCollection
You're getting that type because you are actually accessing the entire collection of Inlines rather than the Inline you're looking for. The fastest way to access the Run's text you're using as the first Inline in the Hyperlink's InlineCollection is to do this:
((sender as Hyperlink).Inlines.FirstInline as Run).Text;
Related
How to create a validator from the textbox class or create new textbox, rather than dragging the textbox from the toolbox
private void Form1_Load(object sender, EventArgs e)
{
createTextpass();
// Creating and setting the properties of TextBox1
TextBox textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size (300,30);
textboxUsername.Name = "text_user";
this.Controls.Add(textboxUsername);
TextBox textboxPassword = new TextBox();
textboxPassword.Location = new Point(420, 80);
textboxPassword.Size = new Size(300, 30);
textboxPassword.Name = "text_pass";
this.Controls.Add(textboxPassword);
TextBox textboxMail = new TextBox();
textboxMail.Location = new Point(420, 110);
textboxMail.Size = new Size(300, 30);
textboxMail.Name = "text_mail";
this.Controls.Add(textboxMail);
}
Add the texbox width and height
If I understand your question correctly. You want to create a textbox and check whether valid text is entered in this textbox or not. For example, the following code can be used for textboxUsername:
public TextBox textboxUsername;
private void Form1_Load(object sender, EventArgs e)
{
createTextpass();
// Creating and setting the properties of TextBox1
textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size(300, 30);
textboxUsername.Name = "text_user";
this.Controls.Add(textboxUsername);
textboxUsername.TextChanged += new EventHandler(usernameTextbox_TextChanged);
....
}
//If textboxUsername contains the # character, an alarm will be displayed
protected void usernameTextbox_TextChanged(object sender, EventArgs e)
{
if (textboxUsername.Text.Contains('#'))
MessageBox.Show("inavlid char in userName");
}
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
I have a dynamically created Label, RichTextBox via a button that is constructed via array.
Label dateLabel = new Label();
dateLabel.Text = dateArray[i];
dateLabel.Name = "date" + i;
dateLabel.Location = new Point(154, 5 + (50 * i));
dateLabel.Tag = dateLabel;
dateLabel.Size = new System.Drawing.Size(91, 20);
panel1.Controls.Add(dateLabel);
RichTextBox placeTravelLabel = new RichTextBox();
placeTravelLabel.Text = placeTravelArray[i];
placeTravelLabel.Name = "placeTravel" + i;
placeTravelLabel.Location = new Point(272, 5 + (50 * i));
placeTravelLabel.Tag = placeTravelLabel;
placeTravelLabel.Size = new System.Drawing.Size(148, 45);
placeTravelLabel.ReadOnly = true;
panel1.Controls.Add(placeTravelLabel);
Button clearButton = new Button();
clearButton.Name = "clearButton" + i;
clearButton.Text = "Remove";
clearButton.Location = new Point(1200, 5 + (30 * i));
clearButton.Click += new EventHandler(this.clearButton_Click);
panel1.Controls.Add(clearButton);
Now I want them to be remove something like this.
public void clearButton_Click(object sender, EventArgs e)
{
dateLabel.Remove();
placeTravelLabel.Remove();
}
Is this possible?
Yes, it is. Try
panel1.Controls.Remove(dateLabel);
panel1.Controls.Remove(placeTravelLabel);
You obviously need to hold the references to them when you create them (i.e. declare them as fields in your class) or mark them somehow (e.g. in Tag property) and enumerate panel1.Controls to find them later.
I think it should also be possible to use closure on local instances by defining button's click event as lambda to avoid declaring those controls as fields. I do not recommend this, as typical flow is more readable and straightforward. Having said that:
Label dateLabel = new Label();
//...
panel1.Controls.Add(dateLabel);
RichTextBox placeTravelLabel = new RichTextBox();
//...
panel1.Controls.Add(placeTravelLabel);
Button clearButton = new Button();
//...
clearButton.Click += new EventHandler((s, e) =>
{
panel1.Controls.Remove(dateLabel);
panel1.Controls.Remove(placeTravelLabel);
});
panel1.Controls.Add(clearButton);
This is pseudo code built up with LinqPad but should give you enough to work with.
I assume that you are working WinForms as there is no tag to say if it is Winforms of WPF, but this is the code that you need that would remove the label for you.
var frm = new Form();
var lbl = new Label();
lbl.Name = "myLable"
frm.Controls.Add(lbl)
frm.Controls.Remove(lbl)
if you ignore the first two lines of the declarations, you simply need `FormName.Controls.Remove(LabelName)
// add a module tab
private void add_mod_Click(object sender, EventArgs e)
{
int TabCount = 0;
int? index = searchIndex(mod_add_textbox.Text);
if (index == null)
{
RichTextBox new_rich = new RichTextBox();
new_rich.Dock = DockStyle.Fill;
TabPage NewPage = new TabPage();
TabCount += 1;
string DocumentText = mod_add_textbox.Text;
NewPage.Name = DocumentText;
NewPage.Text = DocumentText;
NewPage.Controls.Add(new_rich);
mod_tab.TabPages.Add(NewPage);
}
else
{
mod_tab.SelectedIndex = Convert.ToInt32(index);
}
}
private async void btn_file_note_Click(object sender, EventArgs e)
{
using(OpenFileDialog ofd = new OpenFileDialog() { Filter="Text Documents|*.txt", ValidateNames = true, Multiselect = false })
{
if(ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader sr = new StreamReader(ofd.FileName))
{
mod_tab.SelectedTab.Text = await sr.ReadToEndAsync();
}
}
}
}
The problem I am getting is that when I try to open a document it is opening it into the tab name rather than the rich text box inside the tab. I have changed the "mod_tab.SelectedTab" part to the name of the rich text box within the tab however I want it so whichever tab the user has selected it opens in there. Any suggestions? thank you.
You assigned the value to the Text property of selected tab. Instead you should assign the value to Text property of RichTextBox or use Load method of RichTextBox to load content. for example:
this.richTextBox1.Text = ....
Also when you create the tab and RichTextBox dynamically like you ar doing in your code, you can find it this way:
//It means: Find all RichTextBox control which are children of mod_tab.SelectedTab
//And return first of them.
var rtb = this.mod_tab.SelectedTab.Controls.OfType<RichTextBox>().FirstOrDefault();
rtb.Text = ...
Also this way:
//It means get the first child control of mod_tab.SelectedTab
//And convert it to RichTextBox.
var rtb = this.mod_tab.SelectedTab.Controls[0] as RichTextBox;
rtb.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;