Readonly property in multiple texbox [duplicate] - c#

This question already has answers here:
Make all Controls on a Form read-only at once
(7 answers)
Closed 4 years ago.
I'm making a Windows Form Application.
Well, C# programming is new to me and maybe this is a silly question, but how I can apply ReadOnly property in multiple textbox elements? I tried this code:
public void DoReadOnly(Control control){
foreach (Control c in control.Controls){
if (c.Controls != null && c.Controls.Count > 0){
DoReadOnly(c);
}
else if (c is TextBox){
(c as TextBox).ReadOnly = true;
}
}
}
public void getData(){
DoReadOnly(this.Form);
}
The trouble is that I don't know which parameter I should put when I'm call doReadOnly's function. Visual Studio doesn't recognize this.Form like a valid argument.

To call use this.
DoReadOnly(this)
If the method is on Form class

Pass only 'this', that object is your current form
public void getData(){
DoReadOnly(this);
}

Use DoReadOnly(this); instead of DoReadOnly(this.Form);
another thing, why getDate if you are going to put or change a propity, use setData

Related

Understanding why List<string> is changed like it was passed as an reference C# [duplicate]

This question already has answers here:
Passing a List into a method, modify the list within the method without affecting 'original'
(5 answers)
Closed 2 years ago.
Maybe someone can help me, I'm really stumped.
I have a class called TextFunc which contains 2 functions.
The first function read a filepath and return a List<string>.
The second function takes that List<string> as a argument and returns an float[,]. Within this function I remove the first item of the list since I'm not interested in the header.
My problem is that somehow my second function modifies the original List.
So If I display the the first item of the list before the second function it shows what I expect.
After the second function the first item is gone.
I can't figure out why since I'm not using a reference when passing the argument into the second function. I don't even call it the same name or anything within the second function.
My class containing the 2 functions look like this (only keeping the relevant parts):
class TextFunc
{
public static List<string> ParseText(string filePath)
{
List<string> lines = File.ReadAllLines(filePath).ToList();
//do some stuff
return lines;
}
public static float[,] txt2Array(List<string> txtList)
{
txtList.RemoveAt(0);
// do some stuff
return floatArray;
}
}
I call the functions like this from an buttonclick event inside the Form1.cs
public partial class BRkData : Form
{
public BRkData()
{
InitializeComponent();
}
private void BRkData_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
List<string> txtFile = TextFunc.ParseText(#"C:\org_data.res");
MessageBox.Show(txtFile[0]); // here it displays what I expect
float[,] floatArr = TextFunc.txt2Array(txtFile);
MessageBox.Show(txtFile[0]); // here the first item of the list is gone?
}
}
I even tried making a copy of the list inside the txt2Array function but it does not matter. Somehow it's like I send a reference to the list to this function without knowing it.
Think of it like this. There is a man called List (reference type object) who knows a list of good pubs. You have his phone number on a piece of paper.
Calling a method is like giving the phone number to someone else. They have it written on a different bit of paper, but the same man answers the phone.
Passing using the ref keyword would mean that you give the other person the same bit of paper. They could cross out the original phone number and put a new one in, and give it back to you.
I didn't know I had to use the ToList command when I tried to copy it inside the function.
List<string> txtList = txtList_org.ToList();
Thank you for clarifying and pointing me in the right direction!

How to write to a textbox that is in another Form? [duplicate]

This question already has answers here:
Passing a value from one form to another form
(9 answers)
Writing to a textbox on a separate form (C#)
(4 answers)
Closed 5 years ago.
I have a .NET application which has 2 forms: inside Form1 there is all the application stuff, while inside LogForm there is only a readonly textbox. I want to print some text to this textbox inside LogForm from Form1, while Form1 is performing all the work.
I open my LogForm via the
LogForm logForm = new LogForm();
logForm.Show();
But then? How can I do that?
You must have the reference to this TextBox.
Put your access modifier to public in your visual studio form designer and access your TextBox by logForm1.YourTextBox.Text += "new line \r\n";
You can make your LogForm to accept Arguments on initialization:
string ValueFromForm1 = null;
public LogForm(string input)
{
ValueFromForm1 = input;
}
The on Form_Load set the value of textbox:
TextBox1.Text = ValueFromForm1 ;
Create a public variable in Form1 and call in LogForm
Form 1
public static string logformtext;
logformtext="Required text"; //Value which you want to pass to LogForm
LogForm
TextBox1.Text=Form1.logformtext;
Either you can set the text in the constructor of LogForm:
public LogForm(string text)
{
InitializeComponent();
textBox1.Text = text;
}
or you can set the modifier of the TextBox to Internal (or even Public) on the Designer and then access it from Form1 like this:
logForm.textBox1.Text = "Your text";
But keep in mind that while your program is working, the text will not show up on your LogForm, unless you repaint it, or use a BackgroundWorker to have the work be done in a different thread.

List Trigger Event Error [duplicate]

This question already has answers here:
C#: Inheritance Problem with List<T>
(4 answers)
Closed 8 years ago.
i have a code:
public class _clientSockets<Socket> : List<Socket>
{
public event EventHandler OnAdd;
public void Add(Socket s)
{
if (OnAdd != null)
OnAdd(this, null);
base.Add(s);
}
}
that every client that will try to connect to the server will be stored on the list. And i tried to call it using this code:
_clientSockets<Socket> s = new _clientSockets<Socket>();
s.OnAdd += new EventHandler(clientAlerted);
s.Add(socket);
But the problem is got an error something like:
" MainWindow._clientSockets.Add(Socket)' hides inherited member 'System.Collections.generic.List.Add(Socket)'. Use the new keyword if hiding was intended. "
How can i solve this stuff? :)
How can i solve this?
Either don't call your method Add or don't inherit from List. You may find that composition works better than inheritance here, since clients could just treat your class as a plain List<T> and bypass your "new" Add method.

custom single line messagebox [duplicate]

This question already has answers here:
How to create a custom MessageBox?
(4 answers)
Closed 9 years ago.
I'm trying to make a custom message box for my application. The problem is, I want to code it in a way so that I can use it as regular message box.
MyCustomBox("My Message");
intead of doing
FormMessage frm = new FormMessage();
frm.message = "My Message";
frm.show();
How can I accomplish this? Thanks!
You can add a static method to FormMessage class
public static void ShowBox(string message)
{
using (FormMessage frm = new FormMessage())
{
frm.Message = message;
frm.ShowDialog();
}
}
And then
FormMessage.ShowBox("My Message");
Create the form with the appropriate controls, etc. Then add a static method to the class that handles all the messy bits - creating an instance (if necessary), setting properties, etc.
I wish I could write more on this, but it's pretty simple stuff. Just call MyCustomBox.ShowMessage() or whatever you call the static method.

Curious as how to get a console inside of a windows form [duplicate]

This question already has answers here:
Embedding a DOS console in a windows form
(3 answers)
Closed 10 years ago.
Here's an axample
Just curious, I'm new with C#, but I decided to use it to develop my server for my Java game simply because I wanted a nice server-gui, but I'm starting to realize this would be just as easy to do with Swing...
I did a similar project over the weekend, I used this link to forward all Console data to a textbox. You could use any similar control with minor tweaks.
public class TextBoxStreamWriter : TextWriter
{
TextBox _output = null;
public TextBoxStreamWriter(TextBox output)
{
_output = output;
}
public override void Write(char value)
{
base.Write(value);
_output.AppendText(value.ToString()); // When character data is written, append it to the text box.
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
All of the Write(...) and WriteLine(...) trickle down to Write(char) so it's the only method that you need to override.
My example required the form to also have a public TextBox property which exposed the private TextBox inside the form.
TextWriter consoleRedirect = new Tools.TextBoxStreamWriter(consoleForm.TxtOuputDisplay);
Console.SetOut(consoleRedirect);
Well I already wrote this up before I saw the previous comment. But since I took the time I will post it anyway.
public void WriteLog(TextBox tb ,string log)
{
tb.AppendText(log + "\n");
}
private void button1_Click(object sender, EventArgs e)
{
WriteLog(textBox1, "[App]: This is a log string");
WriteLog(textBox1, "[App]: Another log string");
WriteLog(textBox1, "[App]: Yet another etc etc.");
}
Where textBox1 is a Multi-line textbox with a black backcolor and blue foreground text.
The above comment is a more elegant solution but this will get you by if you want something quick and easy. I dont have enough rep to post an image inline but here is what it looks like. http://imageshack.us/photo/my-images/198/logwindow.jpg/

Categories

Resources