I'm trying to improvise a chat for my web site. I have 2 TextBoxes (one for the subject of the question and other for the content of the question). After a certain button is clicked I update a Label with the results.
The position of the Label is not where I want it to be. How can I adjust the position of the Label?
You need to set the Bounds of the label to change it's position & size (assuming your referring to WinForms). Alternatively you can set the Location property directly.
You can also use Location property to set the location of your label on your form.
private void button_Click(object sender, RoutedEventArgs e)
{
textblock_name.Margin = new Thickness(338, 57, 0, 0);
}
you can use above code to change position dynamically
Related
When I Click specific cell in DataGridView in my Winform project, let say ColumnIndex 5 , then I want to popup or open a litle form (not MessageBox) in the same position as Clicked Cell. Right now When I click specific cell, then my form opens in center of Screen. But I wont to open iy in the same position or Location of that Clicked Cell. I don't know how, but I think I need to get location or position of Clicked cell and by some how integrate to popup form.
This is my code and Test form opens in the middle of screen when I click ColmnIndex 5
private void dgvComputersAdgv_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 5)
{
Test ts = new Test();
ts.ShowDialog();
}
}
Thank you in advance !
It appears it could be the forms StartPosition property setting.
The default choices appear to be Manual, CenterScreen, WindowsDefaultLocation, WindowsDefaultBounds, and CenterParent. Click the in an empty area of the form you want to adjust the StartPosition in Design view. Try out CenterParent or Manual to see if that gets you closer. Otherwise, you will need to update the FormName.Designer.cs - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen to this.Position = System.Windows.Forms.FormStartPosition.custom variable that holds the location........
I have a Panel with AutoScroll=true. I'd like to manually scroll the panel's VerticalScroll. I've tried both setting VerticalScroll.Value and Panel.ScrollControlIntoView(...).
However, both cases yielded the following result: The scrollbar does appear to have scrolled to the value but the panel's contents remain unmoved. Scrolling upwards shows an empty panel.
I'm trying to do this during startup. If I scroll directly to the control after a delay (from a thread), it works (though setting the scrollbar value doesn't).
Is there a better (synchronous) way of achieving what I'm looking for?
Use Shown event, for example:
private void Form1_Shown(object sender, EventArgs e)
{
this.panel1.ScrollControlIntoView(this.button1);
//Or if you need a special location:
//this.panel1.AutoScrollPosition = new Point(100, 100);
}
Issue
I have a simple ui, where user enters a value click on the "ok" button, the SubmitDateHandler click handler should then insert a label to the form with the value from the textbox. Then when this is repeated again, Add another label underneath the existing label. how to dynamically add multiple labels in a new line?
my code, at the moment is able to add one label, but not the second one.
private void SubmitDatebtn_Click(object sender, EventArgs e)
{
Label dateLabel = new Label();
dateLabel.Text = this.Controls.Find("Datetxt", true)[0].Text + Environment.NewLine;
this.LatestScoresPanel.Controls.Add(dateLabel);
}
Make sure to use FlowLayOutPanel, you can specify either veritcal or horizontal layout with it. Add the label to that like:
this.flowLayOutPanel.Controls.Add(dataLabel);
Make sure to specify FlowDirection either in code or design time.
flowLayoutPanel.FlowDirection = FlowDirection.TopDown;
I'm trying to center my checkboxes in a TableLayoutPanel, but they always end up looking left-aligned due to the nature of the checkbox control. See picture below:
I want each rows checks to be left-aligned, but for it to appear more centered. Something like the following:
I've checked around online, and I can center the checkboxes by setting AnchorStyles.None which is not what I want, because then the checkboxes are not aligned. I have them set to Dock.Fill so you can click anywhere in the cell to activate the checkbox.
I'm currently just padding my table to achieve a similar effect, but it's by far not an acceptable solution long-term. Also, padding the cells will line break the checkbox text without taking up all the available space on the row (since some of the row is being eaten by padding). The same goes for using a spacer-cell on the left of the table, not an ideal solution.
Does anyone have any ideas? Thanks!
This may work for you:
Set all the ColumnStyles of your TableLayoutPanel as .SizeType = SizeType.AutoSize.
Set your TableLayoutPanel.AutoSize = true and TableLayoutPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
Add this code to center your checkboxes (as well as your TableLayoutPanel) dynamically:
//SizeChanged event handler of your tableLayoutPanel1
private void tableLayoutPanel1_SizeChanged(object sender, EventArgs e){
//We just care about the horizontal position
tableLayoutPanel1.Left = (tableLayoutPanel1.Parent.Width - tableLayoutPanel1.Width)/2;
//you can adjust the vertical position if you need.
}
UPDATE
As for your added question, I think we have to change some things:
Set your CheckBox AutoSize to false. The solution before requires it to be true.
Add more code (beside the code above):
int checkWidth = CheckBoxRenderer.GetGlyphSize(yourCheckBox.CreateGraphics(),System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal).Width;
//TextChanged event handler of your CheckBoxes (used for all the checkBoxes)
private void checkBoxes_TextChanged(object sender, EventArgs e){
UpdateCheckBoxSize((CheckBox)sender);
}
//method to update the size of CheckBox, the size is changed when the CheckBox's Font is bolded and AutoSize = true.
//However we set AutoSize = false and we have to make the CheckBox wide enough
//to contain the bold version of its Text.
private void UpdateCheckBoxSize(CheckBox c){
c.Width = TextRenderer.MeasureText(c.Text, new Font(c.Font, FontStyle.Bold)).Width + 2 * checkWidth;
}
//initially, you have to call UpdateCheckBoxSize
//this code can be placed in the form constructor
foreach(CheckBox c in tableLayoutPanel1.Controls.OfType<CheckBox>())
UpdateCheckBoxSize(c);
//add this to make your CheckBoxes centered even when the form containing tableLayoutPanel1 resizes
//This can also be placed in the form constructor
tableLayoutPanel1.Parent.SizeChanged += (s,e) => {
tableLayoutPanel1.Left = (tableLayoutPanel1.Parent.Width - tableLayoutPanel1.Width)/2;
};
Instead of having the checkboxes in cells, having each one inside a panel all inside a groupbox will allow the checkboxes to fill each panel and have a click able area around them. then with the groupbox dock set to fill and the panels' anchors set to top,bottom they all stay centered.
I am really confused about this. Please consider below scenario.
Scenario:
I have a C# Winform app with few Textbox controls. Now when I input data in these text boxes, for example, "THIS IS MY SAMPLE TEXTBOX", which overlaps the Textbox's visible area and displaying as "AMPLE TEXTBOX". But I want the text to be displayed from the starting position like "THIS IS MY S" and then if needed, overlaps. How can I do this? I have tried below but no luck. Please help. Thanks.
(sender as TextBox).TextAlign = HorizontalAlignment.Left;
Edit
I am also using AutoCompleteMode.Suggest so that when I press any key, corresponding list will be displayed similar to dropdownlist. But the first item of this list is selected by default, which I do not want. Can you please suggest in this also. Thanks.
Final Solution
I am using this to solve the issue
(sender as TextBox).TextAlign = HorizontalAlignment.Left;
(sender as TextBox).Select(0, 0);
Thanks to #Har Har.
I found the solution, To position the cursor at the beginning of the contents of a TextBox control, call the Select method and specify the selection start position of 0, and a selection length of 0.
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Hello this is a sample application";
textBox1.Select(0, 0);
}
It will show the cursor position at 0 index, It's working.