If I created a textbox dynamically like this:
private void Form1_Load(object sender, EventArgs e)
{
TextBox tb=new TextBox();
...
this.Controls.Add(tb);
}
and if I have a button on my form and I want to read the text of the textbox from within the button click event handler
private void button1_Click(object sender, EventArgs e)
{
if(**tb.text**=="something") do something;
}
The problem is that I can't find the textbox control in the button handler.
Thank you in advance
you have to declare texbox out of the method,it has to be global. then you can reach textbox object
TextBox tb;
private void Form1_Load(object sender, EventArgs e)
{
tb=new TextBox();
...
this.Controls.Add(tb);
}
Declare TextBox as your Form class private member.
You could iterate through the Controls collection of an enumerable object like a TabControl, assuming this is a Windows form. Here's some from a project I'm working on, adapted for TextBox:
foreach (TabPage t in tcTabs.TabPages)
{
foreach (Control c in t.Controls)
{
MessageBox.Show(c.Name); // Just shows the control's name.
if (c is TextBox) // Is this a textbox?
{
if (c.Name == "txtOne") // Does it have a particular name?
{
TextBox tb = (TextBox) c; // Cast the Control as a TextBox
tb.Text = "test"; // Then you can access the TextBox control's properties
}
}
}
}
private TextBox tb = new TextBox();
private void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(tb);
}
Related
I am sorry for my dumb question but I have a class that is an UIView and it has a button that will change the data in my ViewController. How can I access the click button that is in that class so the data in my ViewController can change?
Thank you for your time guys.
You could define an EventHandler in your UIView and invoked in ViewController when you clikc the button .
in your UIView
public event EventHandler ButtonClickEvent;
//...
button.TouchUpInside += Button_TouchUpInside;
private void Button_TouchUpInside(object sender, EventArgs e)
{
EventHandler handler = ButtonClickEvent;
handler?.Invoke(sender, e);
}
in ViewController
yourUIView.ButtonClickEvent += ButtonClickHandler;
private void ButtonClickHandler(object sender, EventArgs e)
{
// change the data here
}
Before the user is allowed to save an Excel workbook that is "invalid", I would like to inform them that they have managed to damage it and give them the opportunity to abandon all modifications.
By pressing a few times the [TAB] key, I was able to generate most of the code below:
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WithBeforeSave
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
}
void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
MessageBox.Show("How do I put the proper dialog here?");
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
}
Therefore, all I need is the C# equivalent to this:
This example prompts the user for a yes or no response before saving the workbook.
TIA
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WithBeforeSave
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
}
void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
if (DialogResult.No == MessageBox.Show("Are you sure you want to save the DEFECTIVE, INVALID workbook?", "Example", MessageBoxButtons.YesNo))
{
Cancel = true;
MessageBox.Show("Save is canceled.");
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
}
That's it. I found the answer here:
This example prompts the user for a yes or no response before saving any workbook.
How can i know If user click Enter in keyboard in RichEditBox?
this code does not work
private void Editor_KeyDown(object sender, KeyRoutedEventArgs e)
{
var dia = new MessageDialog(e.Key + "");
dia.ShowAsync();
}
also This code does not work
private void Editor_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.OriginalKey == (VirtualKey)(char)13)
{
NumberEditor.Text += Convert.ToString(_LineNum) + Environment.NewLine;
++_LineNum;
}
}
How can i get line of row in RichEditBox and how can i change text from code in RichEditBox?
I want to make an Editor and any help is appreciated.
Regards
Look this method:
private void ChangeLine()
{
var textRange = MyRichEditBox.Document.GetRange(MyRichEditBox.Document.Selection.StartPosition, MyRichEditBox.Document.Selection.StartPosition);
textRange.Expand(TextRangeUnit.Line);
//Change line size.
textRange.CharacterFormat.Size = 30;
//Center the paragraph
textRange.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//this will change text of the range
textRange.Text = "My new text";
}
From Document you have to get ITextRange. After that you can expand it to TextRangeUnit.Line and get whole line. Now you can change the style and text of the line.
Try this code for KeyDownEvent:
public sealed partial class TextEditPage : Page
{
private readonly KeyEventHandler _keyDownHandler;
//Constructor
public TextEditPage()
{
this.InitializeComponent();
this.Unloaded += OnUnloaded;
//Add keydown event
this._keyDownHandler = OnKeyDown;
RtfBox.AddHandler(KeyDownEvent, this._keyDownHandler, true);
}
private void OnKeyDown(KeyRoutedEventArgs e)
{
//enter your code here
}
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
{
this.RemoveHandler(KeyDownEvent, _keyDownHandler);
}
}
Don't forget to remove handler in Unloaded.
Im using a textbox to accept barcode input and then the enter key to do stuff with that input. But its sending the enter key once for each number entered. Is there a way to have the enter key sent only once? Or maybe supress the other enters?
private void txtBscanned_KeyDown(object sender, KeyEventArgs e)
{
this.txtBscanned.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandler);
}
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(txtBscanned.Text);
}
}
Why are you assigning a keydown event on the textbox itself? You should assign the event on the constructor or using the designer.
public Form1()
{
InitializeComponent();
txtBscanned.KeyDown += new KeyEventHandler(this.OnKeyDownHandler);
}
[DefaultEvent("MyCustomEvent")]
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public delegate void onButtonClick(object sender, Myeventargs e);
public event onButtonClick btnHandler;
protected void ASPxButton1_Click(object sender, EventArgs e)
{
if (btnHandler != null)
{
btnHandler(this,e);
}
}
}
this code can work with
public partial class jscript : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XFButton1.MYClick += new EventHandler(XFButton1_MYClick);
}
void XFButton1_MYClick(object sender, EventArgs e)
{
ASPxLabel1.Text = "Hello";
}
but the problem is i want it automatically generate a new event when i double click on the button. can i done this by using a web user control ?
too bad to tell you that web user control may not able to do like that... pls read this http://dotnet-framework-aspnet.itgroups.info/32/9/thread-796040.html.