Hi I'm working on Windows application and I'm adding data inside the panel dynamically however data is not showing it completely inside the panel because of font size but I don't want to resize font.
tblLayoutModule.Controls.Add(new Label() {
Text = string.Concat(pModuleName, " ", pVersion),
Padding = new Padding() { Top = 2 },
UseMnemonic = false }
);
Try to add autosize = true could work for you
tblLayoutModule.Controls.Add(
new Label() {
Autosize = true,
Text = string.Concat(pModuleName,"",pVersion),
Padding = new Padding() { Top = 2 },
UseMnemonic = false }
);
Related
Is it possible to give width for a textblock inline element in WPF? I am using Run to add inline elements to text block. I want each run to be of same fixed size.But I couldn't find width property for Run.
For example
TextBlock txtblck = new TextBlock();
txtblck.Inlines.Add(new Run() { Text = "abc" });
txtblck.Inlines.Add(new Run() { Text = "def" });
I want both the Runs to be of same width. Is it possible? Please help me.
Thanks in advance.
You can use InlineUIContainer:
TextBlock txtblck = new TextBlock();
txtblck.Inlines.Add(new InlineUIContainer
{
Child = new TextBlock
{
Text = "abc",
Width = 100
}
});
txtblck.Inlines.Add(new InlineUIContainer
{
Child = new TextBlock
{
Text = "def",
Width = 100
}
});
I'm currently trying to change FlatAppearance.BorderSize, when creating a button via Control.Add(new Button()) method but when using:
Controls.Add (new Button(FlatAppearance.BorderSize = 0,))
it just returns an error saying that FlatAppearance does not exist. The buttons are created one after another listing information about songs. Each Section is created button by button in a FlowLayoutPanel. Is there any work around for removing the border on the button?
selectTrackNo.Connection = DB.connect;
MySqlDataReader trackNoReader = selectTrackNo.ExecuteReader();
while (trackNoReader.Read())
{
flpTrackNo.Controls.Add(new Button
{
Name = "lblTrackNo" + x,
Text = trackNoReader[0] as string,
BackColor = Color.Transparent,
FlatStyle = FlatStyle.Flat,
AutoSize = false,
Dock = DockStyle.Top,
Width = flpArtist.Width,
ForeColor = ColorTranslator.FromHtml("#3c3c3c"),
Font = new Font("Trebuchet MS", 9),
Enabled = true,
TextAlign = ContentAlignment.MiddleLeft,
});
x++;
}
this then repeats for every column in the Form.
Many thanks
- Ross
You will need a couple more squiggly brackets:
this.Controls.Add(new Button() { FlatAppearance = { BorderSize = 0 }});
i have the following FlowDirectionPanel http://prntscr.com/aao6lt as you can see those are achievements for a poker game.When you get a new achievement a yes/no messageBox pops up and it ask's you if you want to go and check out your new acquirement if you press yes I want to be able to automatically navigate to the newly unlocked achievement which are contained in the FlowDirectionPanel which looks like in the image above. Also each achievement is contained in another panel which is children of the flowPanel if you look closer you can see that they have some outline border. I create and add the panels dynamically but they do have names which can help me to navigate to the desired panel. This is how i create them:
public void PanelForAchievements(Form currentForm, FlowLayoutPanel flp, AchivementRequirements achivement)
{
FlowLayoutPanel retFlp = flp;
string pGetAchivementName = #"pGet" + achivement.Name;
string lbAchivementName = #"lb" + achivement.Name;
string lbAchivementRewardName = #"lb" + achivement.Name + #"Reward";
string cbGetAchivementName = #"cbGet" + achivement.Name;
string pbAchivementName = #"pb" + achivement.Name;
var pGetAchivement = new Panel
{
Name = pGetAchivementName,
Size = new Size(retFlp.Width, 100),
BorderStyle = BorderStyle.FixedSingle,
};
currentForm.Controls.Add(pGetAchivement);
var lbAchivement = new Label
{
Name = lbAchivementName,
Location = new Point(pGetAchivement.Location.X, pGetAchivement.Location.Y),
Size = new Size(135, 30),
AutoSize = false,
BorderStyle = BorderStyle.FixedSingle,
Font = new Font("Microsoft Sans Serif", 10F, FontStyle.Regular, GraphicsUnit.Point, (byte)0),
Text = achivement.TitleText,
};
var lbAchivementReward = new Label
{
Name = lbAchivementRewardName,
AutoSize = true,
Top = (pGetAchivement.Height - pGetAchivement.Height) / 2,
Text = achivement.RewardLabelText,
TabIndex = 2,
BorderStyle = BorderStyle.FixedSingle,
Location = new Point(lbAchivement.Location.X, lbAchivement.Location.Y + lbAchivement.Height + 5)
};
var cbGetAchivement = new CheckBox
{
Name = cbGetAchivementName,
AutoCheck = false,
AutoSize = true,
Location = new Point(lbAchivement.Location.X + lbAchivement.Width + 10, lbAchivement.Location.Y),
TabIndex = 1,
UseVisualStyleBackColor = true
};
achivement.IsUnlocked(MainPoker.AllAchievements[achivement.EnumCasted], achivement.Requirement,cbGetAchivement);
var pbAchivement = new PictureBox
{
BorderStyle = BorderStyle.Fixed3D,
Name = pbAchivementName,
Dock = DockStyle.Right,
BackgroundImageLayout = achivement.PictureBoxImageLayout,
//Location = new Point(pGetAchivement.Right, pGetAchivement.Location.Y),
Size = new Size(145, 90),
SizeMode = PictureBoxSizeMode.Zoom,
TabIndex = 9,
TabStop = false,
Image = achivement.PackPreview,
};
pGetAchivement.Controls.Add(lbAchivement);
pGetAchivement.Controls.Add(lbAchivementReward);
pGetAchivement.Controls.Add(cbGetAchivement);
pGetAchivement.Controls.Add(pbAchivement);
retFlp.Controls.Add(pGetAchivement);
achivement.Title = lbAchivement;
achivement.RewardLabel = lbAchivementReward;
achivement.Unlocked = cbGetAchivement;
achivement.Preview = pbAchivement;
}
It's just simple code and this is how i initialize them :
public static RoyalFlush RoyalFlush = new RoyalFlush(1, new Tuple<string, int?>("Royal Card Pack", 100000));
private readonly CreatePanels _createAchivementPanels = new CreatePanels();
foreach (var achi in AchivementRequirements.AchivementList)
{
_createAchivementPanels.PanelForAchievements(this, pAchievementsCards, achi);
//im also doing other stuff here..
//pAchivementsCards is the name of the FlowDirectionPanel
}
Now by the time the yes/no messageBox is shown i already know which achievement is unlocked and also my achievements have classes as seen above public static RoyalFlush RoyalFlush and those classes have property - Name which obviously contains the name of the achievement im using this name to create respective names for each control i create from public static RoyalFlush RoyalFlushfor example :
string pGetAchivementName = #"pGet" + achivement.Name;
p stands for panel and i simply just get the current achievement name using the property achivement.Name and we end up with something like this : pRoyalFlush as a name for our panel. Now that i know the name of the panel and which achievement is being unlocked i need to navigate through my FlowDirectionPanel and find the specific panel and leave the focus there. I have no idea how to do that i will show an example with pictures of what i want if it's not clear by now :
First we unlock new achievement and we get the yes/no mbox : http://prnt.sc/aaodyr
Now we press the Yes button which will redirect us to my new form and show us the achievement FlowDirectionPanel : h.t.t.p.:/./.prntscr.com/aaofft here the program see's that the achievement for Full House is completed and it should show it in the middle of the screen with a nice border just like so : h.t.t.p.:././.prntscr.com/aaoh40
I dont have reputation to post more than 2 links so i had to put some dots in them ..
It's my first question and my native language is not English so excuse me for any mistakes I made.
you may make some foreahc loop, where you compare names, and then, when you find the control you need, scroll your flowlayoutpanel to it. For example, my flowlayoutpanel has 4 buttons, and only 2 of them are visible, and I want to scroll to button4:
foreach (Control c in flowLayoutPanel1.Controls)
{
if ((c as Button).Name == "button4")
{
(c as Button).Focus();
flowLayoutPanel1.ScrollControlIntoView(c);
break;
}
}
Hope I andrestand your question correctly.
I have a panel and 3 labels like so
----------
| Label1 |
| Label2 |
| Label3 |
----------
I want 33% of the panel to be label 1,2, and 3 so that they are equally distributed. Should I just use a TableLayoutPanel to accomplish this, the reason I am using Panel is because I need each set of 3 labels logically in blocks and I will have thousands of them.
The code I used so far to accomplish this is as follows
panel[0] = new Panel {
BackColor = Color.White,
Dock = DockStyle.Fill
};
label2[0] = new Label {
Text = "[label2]",
Dock = DockStyle.Fill,
AutoSize = false,
TextAlign = ContentAlignment.MiddleCenter
};
panel[0].Controls.Add(label2[0]);
label3[0] = new Label {
Text = "[label3]",
Dock = DockStyle.Bottom,
AutoSize = false,
TextAlign = ContentAlignment.BottomCenter
};
panel[0].Controls.Add(label3[0]);
label1[0] = new Label {
Text = "[label1]",
Dock = DockStyle.Top,
AutoSize = false,
TextAlign = ContentAlignment.TopCenter
};
panel[0].Controls.Add(label1[0]);
Right now my current effect is Label2 is slightly bigger than label1 and 3. For my purposes this effect was sort of nice for my purposes but on lower resolutions such as around 1024*768 Label 2 completely disappears or becomes way too small so the inverse happens. I don't understand exactly why this is happening nor do I understand how to distribute them evenly using a standard Panel. I am using thousands of these and I already have these inside a TableLayoutPanel so I didn't think it would be a good idea to make a bunch of TableLayoutPanels inside of TableLayoutPanels as I figure that would be overkill. What is a simple way to distribute 3 Labels equally in a regular Panel? After all I have TextAlign middle bottom top, why can't I use them to accomplish what I am doing since it is only 3 labels?
Hi CodeCamper you can set Anchor of panel to all,and labels to right and left,then u can try this.
private void Form1_Load(object sender, EventArgs e)
{
if (panel1.Height % 3 == 0)
{
SetLabels();
}
else
{
while (panel1.Height % 3 != 0)
{
panel1.Height++;
}
SetLabels();
}
}
private void SetLabels()
{
panel1.Controls["label1"].Location = new Point(1, 1);
panel1.Controls["label1"].Height = (panel1.Height / 3);
panel1.Controls["label1"].Width = panel1.Width;
label1.BackColor = Color.Red;
panel1.Controls["label2"].Location = new Point(1, label1.Height);
panel1.Controls["label2"].Height = label1.Height;
panel1.Controls["label2"].Width = panel1.Width;
label2.BackColor = Color.Purple;
panel1.Controls["label3"].Location = new Point(1, label2.Height + label1.Height);
panel1.Controls["label3"].Height = label1.Height;
panel1.Controls["label3"].Width = panel1.Width;
label3.BackColor = Color.Beige;
}
and all labels AutoSize property set to false.
Set label1 dock to top and label3 dock to bottom.
Then set label2 dock to fill autosize to false and textalign to MiddleLeft
EDIT:
set label.Tag to "makemecentered"
and add this to SizeChangedEvent of a Panel:
foreach (Label lab in (sender as Panel).Controls)
if (lab.Tag != null && lab.Tag.ToString() == "makemecentered")
lab.Top = (int)(sender as Panel).Height / 2 - lab.Height / 2;
EDIT 2:
I really don't know if my ability to read has gone... you wanted to set each of the labels to 33% so
set autosize of the labels to false
set desired textaligns
add panel SizeChanged event
private void panel1_SizeChanged(object sender, EventArgs e)
{
foreach (Label lab in (sender as Panel).Controls)
lab.Height = (int) (sender as Panel).Height/3;
}
and raise it on form create
This is my last edit, promise!
I have a Windows form which contains a grid of PictureBox controls. I want to be able to resize the entire form at runtime and have all of these PictureBox controls dynamically resize (scale) proportionally to accommodate the form's new size. The goal is to avoid having to make separate resource files that would essentially make use of the same .cs file just because I want different sizes.
Trying using a TableLayoutPanel control with all rows and columns in percent mode.
new Form {
Controls = {
new TableLayoutPanel {
Dock = DockStyle.Fill,
ColumnCount = 2,
Controls = {
new Button {Text = "0,0", Dock = DockStyle.Fill},
new Button {Text = "1,0", Dock = DockStyle.Fill},
new Button {Text = "0,1", Dock = DockStyle.Fill},
new Button {Text = "1,1", Dock = DockStyle.Fill}
},
RowStyles = {
new RowStyle(SizeType.Percent) {Height = 1},
new RowStyle(SizeType.Percent) {Height = 1}
},
ColumnStyles = {
new ColumnStyle(SizeType.Percent) {Width = 1},
new ColumnStyle(SizeType.Percent) {Width = 1}
}
}
}
}.ShowDialog();