i have a Textbox and some RadioButtons in C#.
Now i want to set a Text to a disabled Textbox.
What i tried:
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
TextBox1.Text = "****";
TextBox1.Enabled = false;
}
This way i cant see the Text.
If i enable the Textbox, the TextBox shows me the String (****)
What can i do to set a Text to a disabled Textbox?
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
textBox1.Enabled = true;
textBox1.Text = "*****";
textBox1.ReadOnly = true;
textBox1.Enabled = false;
this.Invalidate(); //to perform form re-draw
}
You can change the PasswordChar property based on whether or not the textbox is enabled:
TextBox1.PasswordChar = TextBox1.Enabled ? '\0' : '*';
The \0 character will show the contents in plain text.
See this answer for similar results.
Related
Okay. So, I'm making my own internet browser and I have tabs. But, I'm trying to make it a real time url updater while if you click on any link it will show up in the textbox of the url.
It will not work.
Here is the tab button.
private void button8_Click(object sender, EventArgs e)
{
WebBrowser Browser = new WebBrowser();
tabControl1.TabPages.Add("New Page");
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
Browser.Name = "Web Browser";
Browser.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browser);
((WebBrowser)(tabControl1.SelectedTab.Controls[0])).GoHome();
And here is where my textbox isn't getting the right url.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
button1.Enabled = true;
textBox1.Enabled = true;
//textBox1.Text = Browser.Url.ToString();
((WebBrowser) (this.tabControl1.SelectedTab.Controls[0])).Url.ToString();
}
In order for this to work, you need to get all link elements of the web page you've just loaded and assign a custom function to the HtmlElement.Click event of that element.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
button1.Enabled = true;
textBox1.Enabled = true;
var linkElements = Browser.Document.GetElementsByTagName("a");
foreach(HtmlElement link in linkElements)
{
link.Click += (s, args) =>
{
// a link is being clicked
// get the url the link is pointing to using the href attribute of the element
textBox1.Text = link.GetAttribute("href");
}
}
}
I have a textBox that the user should fill it. Default text of the textBox is blank. I want that if the user enter some text in it, buttons will be enable.
private void txtLicense_TextChanged(object sender, EventArgs e)
{
if (txtEconomic.Text != "")
btnInsert.Enabled = true;
}
but in this code, if the user enter some text and then erase it, it dosent work. I mean the buttons will be enable ...
how can I do that?
thanks
just do btnInsert.Enabled = false;
private void txtLicense_TextChanged(object sender, EventArgs e)
{
if (txtEconomic.Text != "")
btnInsert.Enabled = true;
else
btnInsert.Enabled = false;
}
Problem : You don't have any logic to disable the Button.
Solution : You need to add else block to disable the Button.
Suggestion: i would suggest you to use String Method String.IsNullOrEmpty() to check wether your textbox input string is Null or Empty.
if (!String.IsNullOrEmpty(txtEconomic.Text))
btnInsert.Enabled = true;
else
btnInsert.Enabled = false;
private void txtLicense_TextChanged(object sender, EventArgs e)
{
if (txtEconomic.Text.Length > 0)
btnInsert.Enabled = true;
else
btnInsert.Enabled = false;
}
I'm trying to do the following: I write link to textbox and it displays in a linklabel and after I click linklabel it goes to that url written in it? Everything goes well but pressing linklabel doesn't go to url.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
label2.Text = textBox1.Text;
linkLabel1.Text = textBox2.Text;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.checkbox = checkBox1.Checked;
Properties.Settings.Default.textbox = textBox1.Text;
Properties.Settings.Default.label = label2.Text;
Properties.Settings.Default.linkLabel = linkLabel1.Text;
Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkbox;
textBox1.Text = Properties.Settings.Default.textbox;
label2.Text = Properties.Settings.Default.label;
linkLabel1.Text = Properties.Settings.Default.linkLabel;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
}
You have to start a process with the text of the LinkLabel, which should be a valid URL. This code will open the URL in the default browser:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(linkLabel1.Text);
}
More info on the Process Class.
If the default browser is IE and you want to open it in Chrome for example you'll have to provide the necessary info:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Users\<UserName>\AppData\Local\Google\Chrome\chrome.exe";
p.StartInfo.Arguments = linkLabel1.Text;
p.Start();
}
Of course you'll have to do validation to check if the text of the LinkLabel is a valid URL.
You should use following code to add a link to LinkLabel as:
// Add a link to the LinkLabel.
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = textbox2.Text;
linkLabel1.Links.Add(link);
I a have simple login form and I have set it's accept button property to "OK" button,
and I have a textbox "username" which I set it's KeyDown event to do some processing.
the Ok has enabled set to false.
btnOk.Enbled = false;
this.AcceptButton = btnOk;
txtUsername.KeyDown += new KeyEventHandler(KeyDownHandle);
when I hit enter in the username textbox I do some processing and then set accept button enabled to true.
private void KeyDownHandle(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// some processing
btnOk.Enabled = true;
txtPassword.Focus();
}
}
then I write password in password textbox and hit enter so "OK.Click" is triggered.
but the problem is the keyDown is not working because "accept button".
what can I do to solve this?
Edit: Just want to say the problem is resolved if I set acceptButton to "none" but that's not what I'm looking for.
Why don't you remove this.AcceptButton = btnOk from the constrctor (I suppose) and put it in the KeyDownHandler, that way btnOk will accept Enter key only after it is enabled and after the username has been inserted. so the code should be like this:
btnOk.Enabled = false;
txtUsername.KeyDown += new KeyEventHandler(textBox1_KeyDown);
btnOk.Click += new EventHandler(button1_Click);
private void KeyDownHandle(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// some processing
btnOk.Enabled = true;
this.AcceptButton = btnOk;
txtPassword.Focus();
}
}
I have the following issue:
I create a TextBox dynamically in my web page, its value is "initialVal" in the beginning.
Now I need to make a postback (not callback) to the server, and during this operation, I need to compute/change the value of my textbox to other value.
Here's an example:
protected void Page_Load(object sender, EventArgs e)
{
TextBox txtBox = new TextBox();
txtBox.ID = "newButton";
form1.Controls.Add(txtBox);
txtBox.Text = "initialVal";
if (IsPostBack && Session["change"] == null)
{
txtBox.Text = "change";
Session["change"] = true;
}
}
The problem: even if I change the value via code, the textbox will keep the text "initialVal". I feel this is something related to the view state, but I don't understand.
Coudl anyone please help me here?
Thanks.
Everytime you load the page it is running this:
txtBox.Text = "initialVal";
You should wrap this in a check for postback:
if (!Page.IsPostback)
{
txtBox.Text = "initialVal";
}
That said, onLoad is the wrong event to do the creation, for it to be valid in the early enough in the page lifecycle, use OnInit.
See this article on MSDN.
Here is the final code from #user2890888:
public partial class WebForm1 : System.Web.UI.Page
{
TextBox txtBox = null;
protected void Page_Init(object sender, EventArgs e)
{
txtBox = new TextBox();
txtBox.ID = "newButton";
form1.Controls.Add(txtBox);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtBox.Text = "initialVal";
}
if (IsPostBack && Session["change"] == null)
{
txtBox.Text = "change";
Session["change"] = true;
}
}
}
Create your dynamic textbox creation in !IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack){
TextBox txtBox = new TextBox();
txtBox.ID = "newButton";
form1.Controls.Add(txtBox);
txtBox.Text = "initialVal";
}
if (IsPostBack && Session["change"] == null)
{
txtBox.Text = "change";
Session["change"] = true;
}
}
Thanks and let me know if your issue still pending
Find the TextBox and use it
TextBox txtBox = (TextBox)FindControl("txtBox");
txtBox.Text = "change";
Even now you are having issue and your textBox is not getting the value you needed.
Store your new value for TEXTBOX in a hiddenfield.
I mean ,
if (IsPostBack && Session["change"] == null)
{
hiddenfield1.value = "change";
}
and later in your page script, you can assign back the value in this hiddenfield1 to textbox.
$(document).ready(
{
$('#txtBoxID').value=$('#hiddenfield1').value;
}
);