I searched for this for A while but I couldn't find the answer, so I hope it's not a duplicate.
I have the following code:
this.Controls.Add(new Label { Location = new Point(10, 10),
AutoSize = true,
Name = "jobNumStatic",
Text = "Job Number:",
Font = new Font(jobNumStatic.Font, FontStyle.Bold) });
I'm trying to change the font to bold. But that code gives the error, The name 'jobNumStatic' does not exist in the current context. Is there any way to make the font bold here?
I also tried:
jobNumStatic.Font = new Font(jobNumStatic.Font, FontStyle.Bold) });
After declaring the Label, and it gives me the same error.
To use a Label's default font as prototype just use the static Label.DefaultFont property:
this.Controls.Add(new Label { Location = new Point(10, 10),
AutoSize = true,
Name = "jobNumStatic",
Text = "Job Number:",
Font = new Font(Label.DefaultFont, FontStyle.Bold) });
jobNumStatic is not a variable in your scope. You provide the string "jobNumStatic" at runtime for the Name property of the newly created Label, but that does not mean you magically have a variable with that name at compile-time.
If you need to access this Label later you may of course declare a member variable:
private Label jobNumStatic;
and assign the created instance to that variable:
jobNumStatic = new Label { Location = new Point(10, 10),
AutoSize = true,
Name = "jobNumStatic",
Text = "Job Number:",
Font = new Font(Label.DefaultFont, FontStyle.Bold) });
this.Controls.Add(jobNumStatic);
Simply use the code below:
Label1.Font = new Font(Font, Size, FontStyle.Bold);
Related
how do i set a buttons name when i created it in c# so i can call it later?
i have a a List of strings (Commands).
i loop over it an create a button for each item in the List.
commands.ForEach(delegate (String i)
{
Button button = new Button()
{
Name = i,
Tag = i,
MaxWidth = 50,
MaxHeight = 50,
BorderBrush = null
};
button.Click += new RoutedEventHandler(button_Click);
this.grid.Children.Add(button);
Uri resourceUri = new Uri("led_Off.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);
BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
button.Background = brush;
});
This loop works fine until i add the line
Name = i
i am in a spot where these buttons were created and i now need to change some of there back ground images.
is there a better way to call them then by there name?
Name should be a valid string: try Name = "button" + i.ToString() (FYI, Button Name cannot be just "1", it's an invalid Name). Also, Tag =i.ToString(). Hope this may help
Don't do it, use data binding and data templating with commands instead.
There is no reason to ever create any UI elements in a loop in WPF.
Edit: Just this.
The font property of richtextbox doesn't seem to be working.
//
// textBox_rawdata
//
this.textBox_rawdata.DetectUrls = false;
this.textBox_rawdata.Font = new System.Drawing.Font("NSimSun", 9F);
this.textBox_rawdata.HideSelection = false;
this.textBox_rawdata.Location = new System.Drawing.Point(22, 43);
this.textBox_rawdata.Name = "textBox_rawdata";
this.textBox_rawdata.Size = new System.Drawing.Size(368, 68);
this.textBox_rawdata.TabIndex = 2;
this.textBox_rawdata.Text = "AAAAAA";
I want the font of the richtextbox to be NSimSun, 9pt. As you can see in the picture, The first few A's are preset and the last 3 A's are typed in by me. The issues is, the preset characters and any characters generated by the program are correctly displayed as NSimSun, 9pt. But as soon as I start typing in there, the font changes. (Like the last 3 A's)
How can I make the font NSimSun, 9pt for all text?
This might work for you.
this.textBox_rawdata.SelectionFont = new System.Drawing.Font("Tahoma", 12, System.Drawing.FontStyle.Bold)
if you want your font type, size and style to be set once you run your code put this in designer:
this.textBox_rawdata.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Try to set SelectionFont property of richtextbox to System.Drawing.Font("NSimSun", 9F) also.
From MSDN it is:
A Font that represents the font to apply to the current text selection or to text entered after the insertion point.
I'm trying to invoke a Callisto Flyout when right-tapping on a grid in a GridView (the ultimate goal is to allow the user to change a value and store that in an ApplicationDataContainer). I first tried it with a sample that creates a menu that I found online -- that works, but I don't want a menu.So I tried changing it up from a menu to a StackPanel with a TextBlock and a TextBox on it. This code, though:
private void ItemView_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
var flyOut = new Flyout {PlacementTarget = sender as UIElement, Placement = PlacementMode.Mouse};
var sp = new StackPanel {MinWidth = 240, MinHeight = 80, Orientation = Orientation.Horizontal};
var tblk = new TextBlock {MinWidth = 60, MinHeight = 72};
sp.Children.Add(tblk);
TextBox tb = new TextBox {MinWidth = 120, MinHeight = 72};
sp.Children.Add(tb);
flyOut.Content = sp;
flyOut.IsOpen = true;
UpdateLayout();
}
...crashes and takes me to this line in App.g.i.cs:
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
BTW, I may eventually move this code from the RightTapped event to be in its presumably "proper" place in the Charms Settings, but I reckon this problem needs to be solved in either case.
UPDATE
I tried to go a different route with this by moving the flyout from "in place" to the Windows 8 Settings panel:
public ItemsPage()
{
InitializeComponent();
SettingsPane.GetForCurrentView().CommandsRequested += OnSettingsPaneCommandRequested;
}
private void ItemView_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
SettingsPane.Show();
}
private void OnSettingsPaneCommandRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
{
args.Request.ApplicationCommands.Add(new SettingsCommand("commandSetSection1Name",
"Change the name of Section 1", SetAndSaveSectionNames));
args.Request.ApplicationCommands.Add(new SettingsCommand("commandSetSection2Name",
"Change the name of Section 2", SetAndSaveSectionNames));
args.Request.ApplicationCommands.Add(new SettingsCommand("commandSetSection3Name",
"Change the name of Section 3", SetAndSaveSectionNames));
args.Request.ApplicationCommands.Add(new SettingsCommand("commandSetSection4Name",
"Change the name of Section 4", SetAndSaveSectionNames));
args.Request.ApplicationCommands.Add(new SettingsCommand("commandSetSection5Name",
"Change the name of Section 5", SetAndSaveSectionNames));
args.Request.ApplicationCommands.Add(new SettingsCommand("commandSetSection6Name",
"Change the name of Section 6", SetAndSaveSectionNames));
}
private void SetAndSaveSectionNames(IUICommand command)
{
var flyOut = new Flyout(); // flyOut is a Callisto control
var sp = new StackPanel {MinWidth = 240, MinHeight = 80, Orientation = Orientation.Horizontal};
var tblk = new TextBlock {MinWidth = 60, MinHeight = 72};
sp.Children.Add(tblk);
TextBox tb = new TextBox {MinWidth = 120, MinHeight = 72};
sp.Children.Add(tb);
flyOut.Content = sp;
flyOut.IsOpen = true;
UpdateLayout();
}
However, the same thing happens - I can get to the call to SetAndSaveSectionNames() just fine by right-clicking one of the grids in my GridView (ItemView_RightTapped), and then selecting one of
the "Change the name of Section N" TextBlocks or whatever they are on the Settings panel, but then: crasho!
If a fella wants to have dozens of settings in the Settings panel, how will that work - it seems there's not room for much more than the six I added - will it sprout a ViewBox or ScrollBox or something at some point to accommodate this?
The exception is probably NotImplementedException because the Callisto code has not implemented the PlacementMode.Mouse option. I had this issue today.
See: https://github.com/timheuer/callisto/blob/master/src/Callisto/Controls/Flyout/Flyout.cs
case PlacementMode.Mouse:
throw new NotImplementedException("Mouse PlacementMode is not implemented.");
In my code, i create a label with the following:
Label namelabel = new Label();
namelabel.Location = new Point(13, 13);
namelabel.Text = name;
this.Controls.Add(namelabel);
The string called name is defined before this, and has a length of around 50 characters. However, only the first 15 are displayed in the label on my form. I tried messing with the MaximumSize of the label but to no avail.
Try adding the AutoSize property:
namelabel.AutoSize = true;
When you place a label on a form with the design editor, this property defaults to true, but if you create the label in code like you did, the default is false.
Try the property AutoSize = true;
MSDN refs
Another way is using the MeasureString method of the Graphics class
Graphics e = nameLabel.CreateGraphics();
SizeF stringSize = new SizeF();
stringSize = e.MeasureString(name, namelabel.Font);
nameLabel.Width = (int)stringSize.Width;
You could use the property Label.AutoSize to automatically adjust the width of your label to properly fit all the contents stored in Label.Text.
It's worth mentioning that when creating the label using the design editor this property defaults to true, but when you programmatically creates a label on your own the property defaults to false.
namelabel.AutoSize = true;
Of course you could also manually set the width of your label using something as the below to calculate the required width.
Graphics namelabel_g = namelabel.CreateGraphics ();
namelabel.Width = namelabel_g.MeasureString (
namelabel.Text, namelabel.Font
);
Documentation regarding the use of Label.AutoSize use can be found on msdn:
msdn.microsoft.com - Label.AutoSize Property (System.Windows.Forms)
Documentation regarding Graphics.MeasureString can be found here:
msdn.microsoft.com - Graphics.MeasureString Method (String, Font) (System.Drawing)
panel_saved.Controls.Add(
new Label
{
Location = new Point(1, 2),
Size = new System.Drawing.Size(43, 18),
BorderStyle = BorderStyle.FixedSingle,
Text = "yourdata"
});
Can anyone tell me how can set default Font Name , Font Size , Font Color.. of FontDialog;
FontDialog dlg = new FontDialog();
dlg.ShowColor = true;
if (dlg.ShowDialog() != DialogResult.OK) return;
The dlg.ShowDialog() ; method should show Font name that I choose insted of "microsoft san serif"
You just need to set the Font property before calling ShowDialog.
For example:
dlg.Font = new Font("Consolas", 10);
//or
dlg.Font = myCurrentlySelectedFont;
It's also worth pointing out that when getting the font name from the font dialog, you want the value: fontDlg.Font.Name, or fontDlg.Font.FontFamily.Name.
This value will correctly allow you to set the font name as above before showing the dialogue.