Displaying a text box - c#

I've searched the questions for an answer but couldn't quite find a clear cut example. I am trying to display a simple text box in C#. I am working with C#, ArcMap and ArcObjects. I have created a toolbar that has a button in it. Upon clicking the button, I just need a text box to appear on the page. So far, this is what I've got, but nothing is producing when I click my button. Thanks for your help in advance.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace Map
{
public class ArcGISAddin4 : ESRI.ArcGIS.Desktop.AddIns.Button
{
public ArcGISAddin4()
{
}
protected override void OnClick()
{
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Text = "My First Text Box";
dynamicTextBox.Name = "First Text Box";
dynamicTextBox.Enabled = true;
}
protected override void OnUpdate()
{
}
}
}

You must add the TextBox to the surrounding container (the form for example). Otherwise the program won't know where it's supposed to be displayed.

You should add dynamicTextBox to a specific container such as form.
Such as this:
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Text = "My First Text Box";
dynamicTextBox.Name = "First Text Box";
dynamicTextBox.Enabled = true;
this.Contols.Add(dynamicTextBox);//this is a pseudo code

Related

Adding TextBoxes to a list in another class

I created three text boxes calls HighOffSetX, HighOffSetY, and HigOffSetZ.
I want to add them to a list called HighOffSets. The problem that I am running into is how to add the text boxes into a list in another class.
When I try to add HighOffSetX to my list I get an error that HighOffSetX is not recognized. What do I do so my class recognizes the text boxes? I do not have a lot of experience in GUIs with C#. I looked on Google and stack and could not find an answer that helped me out. Here is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UCAGui
{
public class CaliberationGUI: Form
{
List<TextBox> HighOffSets = new List<TextBox>();
public CaliberationGUI()
{
HighOffSets.Add(this.HighOffSetX); //This line gives me an error
}
}
}
I changed the names of the text boxes to HighOffSetX, HighOffSetY, and HigOffSetZ. Here is the generated code from ConfigForm.Designer.cs
this.HighOffSetX.Location = new System.Drawing.Point(60, 28);
this.HighOffSetX.Name = "HighOffSetX";
this.HighOffSetX.Size = new System.Drawing.Size(100, 20);
this.HighOffSetX.TabIndex = 5;
If you want me to post the generated code C# created when I added the text boxes let me know.
You have declared your textbox in a different form.You can't access that textbox without an instance of ConfigForm.Create a new instance of your second Form, or pass it via the constructor:
public class CaliberationGUI: Form
{
private ConfigForm configForm;
List<TextBox> HighOffSets = new List<TextBox>();
public CaliberationGUI(ConfigForm config)
{
InitializeComponent();
this.configForm = config;
HighOffSets.Add(this.configForm.HighOffSetX);
}
}

Make links readonly in richtextbox c#

I have a richtextbox in c# and I want to make the links that appear as readonly. Right now I can move my cursor into it and edit it. Is there any way to make it readonly?
You can set this property of a RichTextBox to make the whole text read-only
ReadOnly = true
If you would like to protect the links only but leave other text editable, please try to insert the following whether under Form1_Load or under any method you may create
You'll need to add RichTextBox.Find(string str); from the object browser
MatchCollection mc = Regex.Matches(richTextBox1.Text, #"(www[^ \s]+|http[^ \s]+)([\s]|$)", RegexOptions.IgnoreCase); // Create a new MatchCollection and match from richTextBox1.Text
for (int collection = 0; collection < mc.Count; collection++) // increase collection for every string in mc
{
if (richTextBox1.Find(mc[collection].Value, RichTextBoxFinds.None) > -1) // Find the mc value
{
richTextBox1.SelectionProtected = true; // Protect the value
}
}
So the form would look like this
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MatchCollection mc = Regex.Matches(richTextBox1.Text, #"(www[^ \s]+|http[^ \s]+)([\s]|$)", RegexOptions.IgnoreCase);
for (int collection = 0; collection < mc.Count; collection++)
{
if (richTextBox1.Find(mc[collection].Value, RichTextBoxFinds.None) > -1)
{
richTextBox1.SelectionProtected = true;
}
}
}
}
}
Thanks,
Have a great day :)
You can change it in your code like this:
richTextBox1.ReadOnly = true;
Or you could go to your design view, check the properties for your richtextbox and set the ReadOnly attribute to true.
You should capture the change event, in such a way that you reset every change a user would like to make to the link and set it back to the original link. Save the positions of the links and update the positions if the user deletes or adds a character.
I would like to share my solution...I did try everything I found on the internet but seems I can't get exactly 100% like I want(to make a richtexbox as readonly). Then I start looking for an alternative which finally I get one to do exactly like I want.
Sometime we need to show the value with a style on it, thats why we choose richtextbox at the 1st time, then it become an issue when we unable to make it as ReadOnly. The different is I am not using the richtextbox anymore but I change it to label. Depending on how your program work, you might need to have 2 control (richtextbox & label) to hold the same value which will be switched(visible true/false) base on your requirement.
See my example here to get a ReadOnly richtextbox look alike control :
<div id="History">
<asp:Label ID="lblLACA27" runat="server" CssClass="ctlLabel"></asp:Label>
</div>
And a piece of CSS code :
#History
{
height: 100px;
float: left;
overflow: auto;
overflow-x: hidden;
}
The DIV tag which hold the LABEL will act like multiline textbox/richtextbox with scrollbar visible on it. Thats it & lets continue programming. Hope this will help someone later.

change font style in c#

hi there I want to change the font style of a label in an aspx page, through choosing options provided by a dorpdownlist. None of these methods work. I know I might be on the right path, but just can't seem to get it right. Please enlighten me. Thanks
In my code behind:
public void button1_Click(object sender, EventArgs e)
{
var select= drop1.SelectedItem.Value;
if(select == "Times New Roman")
{
// I tried doing all of these:
label1.Font= new Font("Times New Roman", label1.Font.Size);
//or
label1.Font.Name ="Times New Roman";
//or
Label new1 = new Label("Times New Roman");
Label1.Font= new1;
}
}
You're better off using jquery
Bind an event handler to the onchange event on the select dropdown and according to the value then change the css class. This has the benefits of a - not being server side and avoiding a hit on the server b - easier c - cleaner
edit : Something like this could be adapted
jQuery onchange/onfocus select box to display an image?
Do you need to make it this way? It's not a 'nice' solution anyway. It's much better to assign an css class. I haven't seen this way before... but I would say that it's comming from WinForms.
Use CssClass instead:
label1.CssClass = "SomeClass";
And define styling in your stylesheet:
.SomeClass { font-family: "Times New Roman"; font-size: 1.2em; }
here is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Font.Name = "Verdana";
}
}
}
and it is working, i just need you to make sure that before you run the application you set a fontname to your label because the fontname is empty when you put it in your page,(it doesnt work if you dont set a fontname initially) you need to set it then yuse the code i wrte above.open the properties window click the label and click font then choose a name for font name
On web we do not create new Font this works for desktop programming and the new Font are made on server not on client.
The Label contains the .Font but not to set a new font, but to actually create a new inline style, and the object is the FontInfo (not the Font).
From MSDN FontInfo here is an example:
// Note that myLabel.Font is a FontInfo object.
myLabel.Font.Bold = true;
myLabel.Font.Italic = false;
myLabel.Font.Name = "verdana";
myLabel.Font.Overline = false;
myLabel.Font.Size = 10;
myLabel.Font.Strikeout = false;
myLabel.Font.Underline = true;
// Write information on the FontInfo object to the myLabel label.
myLabel.Text = myLabel.Font.ToString();
The final render of this contains inline style to give this properties to the text that is inside a span or a div. Is better of course to give a global css class, but some times you need to interfere with inline style.

WP7 silverlight custom control using popup

I'm creating a custom datepicker, I have a textbox, once clicked it opens a calendar within a popup.
What I want to do is change the size of the popup so it shows my whole calendar, but I can't manage to change it..., I've tried using Height, Width, MinHeight, MinWidth... but it doesn't work, the popup keep showing with a fixed size.
The thing is that my popup's parent property isn't evaluated since it has expression issues (according to debugger), so I'm sure my popup's parent isn't the main screen( say layout grid).
How can I for example make my popup open within a specific context ?
This part of my code isn't XAML, it's C# code only and it looks like:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
namespace CalendarBranch.components
{
public class wpDatePicker:TextBox
{
private CalendarPopup calendar;
private Popup popup;
public wpDatePicker()
{
this.calendar = new CalendarPopup();
this.popup = new Popup();
this.popup.Child = this.calendar;
this.popup.Margin = new Thickness(0);
this.MouseLeftButtonUp += new MouseButtonEventHandler(wpDatePicker_MouseLeftButtonUp);
this.calendar.onDateSelect += new EventHandler(onDateSelected);
this.IsReadOnly = true;
}
protected void wpDatePicker_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.popup.Height = this.calendar.Height;
this.popup.Width = this.calendar.Width;
this.popup.HorizontalAlignment = HorizontalAlignment.Center;
this.popup.VerticalAlignment = VerticalAlignment.Center;
this.popup.HorizontalOffset = 0;
this.popup.VerticalOffset = 0;
this.popup.MinHeight = this.calendar.Height;
this.popup.MinWidth = this.calendar.Width;
this.popup.IsOpen = true;
}
private void onDateSelected(Object sender, EventArgs ea) {
this.Text = this.calendar.SelectedValue.ToShortDateString();
this.popup.IsOpen = false;
}
}
}
PS: the class Calendar is simply a UserControl that contains a grid with multiple columns, HyperLinkButtons and TextBlocks, so nothing special.
Thank you in advance guys ;)
Cheers
Miloud B.
Popup control resizes itself to fit the content inside of it. For example, if you set the child of Popup to be StackPanel with width/height set to 100, the popup will be 100x100.
So it's really important to set the sizes not of your popup, but of your inner panel. Try wrapping your content into the stackpanel and assign necessary width/height there.

Windows DataGridView _RowCommand

My background is pretty much ASP.Net, and I was asked to develop a small windows application. I tried to use a grid to present and select data, and I tough that the equivalent in windows forms to the ASP.Net's GridView was DataGridView. I'm not sure yet if that is the case, basically, in ASP.Net, you have the _RowCommand event associated to the grid, that is triggered after a Commandbutton is clicked. I noticed also that there is no such thing as a DataKeyNames property, so I don't know how to pass the current row key to the button clicked. Any help will be appreciated, thanks!
I forgot to mention: My grid has two DataGridViewButton type of columns, And I don't know the event that I need to code on to perform the selected command
The event you are looking for is the CellClick event - with the DataGridViewButtonColumn you do not actually associate event handlers to the particular buttons as you would with other buttons on your form.
From the event arguments returned by the cell click event you can then work out which row and column the click was in and from that what action you want to take.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewButton cell = (DataGridViewButtonCell)
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
// Any additional logic you want
}
Beyond that however, I think you would really benefit from taking a step back and thinking about the differences between the winforms and webforms coding paradigms.
With webforms a lot of the way you code is dictated by the fact that everything is stateless. This is not the case with winforms, you have state and you can access most controls when you need to find out information about them.
For this reason, retrieving information like the currently selected cell in a DataGridView is trivial with winforms.
Also, in most cases with winforms you do not need specific buttons to edit or delete - you can edit directly in the grid, and use in inbuilt delete functionality (select the row and press the delete key).
A good place the get you past some of the bumps might be the DataGridView FAQ. It's an amazing resource for learing about the DataGridView
DataGridView.CurrentRow gets the selected row. Is that what you need?
If you are databound, you should have (via .CurrentRow) access to all the properties, by casting .CurrentRow.DataBoundItem to whatever type you need. Otherwise, just look at the cells, or set a .Tag against the rows when you add them.
Here's an example showing a data-bound DataGridView and a couple of buttons, pulling out data for the selected row:
using System;
using System.ComponentModel;
using System.Windows.Forms;
class Person
{
public string Name { get; set; }
[DisplayName("Eye Color")]
public string EyeColor { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
using (var form = new Form())
using (var grid = new DataGridView { Dock = DockStyle.Fill})
using (var btn1 = new Button { Dock = DockStyle.Bottom, Text = "Button 1"})
using (var btn2 = new Button { Dock = DockStyle.Bottom, Text = "Button 2" })
{
btn1.Click += delegate
{
form.Text = "Button 1 clicked";
if (grid.CurrentRow != null)
{
form.Text += ": " + ((Person)grid.CurrentRow.DataBoundItem).Name;
}
};
btn2.Click += delegate
{
form.Text = "Button 2 clicked";
if (grid.CurrentRow != null)
{
form.Text += ": " + ((Person)grid.CurrentRow.DataBoundItem).Name;
}
};
form.Controls.Add(btn1);
form.Controls.Add(btn2);
form.Controls.Add(grid);
var data = new BindingList<Person>
{
new Person { Name = "Fred", EyeColor = "green"},
new Person { Name = "Barney", EyeColor = "brown"},
new Person { Name = "Wilma", EyeColor = "blue"},
new Person { Name = "Betty", EyeColor = "blue"},
};
grid.DataSource = data;
Application.Run(form);
}
}
}
There are other ways of handling the click events, and usually much of the above would be done via the designer rather than like this (but it is very hard to show designer code in a snippet).

Categories

Resources